Created
February 2, 2016 03:35
-
-
Save phil-monroe/debc9780fdabd4ab1cae to your computer and use it in GitHub Desktop.
My attempt at a presenter and page objects for Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestPresenter < Presenter | |
let(:a) { 5 } | |
let(:b) { 3 } | |
let(:c) { a * b } | |
let(:str) { _.to_s } | |
let(:sym) { _.to_sym } | |
let(:to_yaml) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Presenter | |
include ActiveSupport::Callbacks | |
include Rails.application.routes.url_helpers | |
class_attribute :proc_table | |
class_attribute :presenter_table | |
define_callbacks :initialize | |
delegate :to_param, :model_name, :to_key, to: :object | |
attr_reader :object | |
self.proc_table = HashWithIndifferentAccess.new | |
self.presenter_table = HashWithIndifferentAccess.new | |
def self.inherited subclass | |
super | |
subclass.proc_table = self.proc_table.dup | |
subclass.presenter_table = self.presenter_table.dup | |
end | |
def self.let key, presenter=nil, &block | |
block ||= proc { | |
object.send(key.to_sym) | |
} | |
self.proc_table[key.to_sym] = block | |
self.presenter_table[key.to_sym] = presenter if presenter.present? | |
define_method key.to_sym do | |
_cache_or_run(key) | |
end | |
end | |
def self.name_object name | |
define_method name.to_sym do | |
self.object() | |
end | |
end | |
def initialize obj=nil | |
run_callbacks :initialize do | |
@object = obj | |
end | |
end | |
alias_method :_, :object | |
private | |
def _cache_or_run key | |
@_proc_cache ||= HashWithIndifferentAccess.new | |
@_proc_cache[key] ||= _run(key) | |
end | |
def _run key | |
Rails.logger.debug "[#{self.class}] running: #{key}" | |
val = instance_eval(&self.proc_table[key]) if self.proc_table.has_key?(key) | |
if self.presenter_table[key].present? | |
if val.is_a? Array | |
val.map{ |i| self.presenter_table[key].new(i) } | |
else | |
self.presenter_table[key].new(val) | |
end | |
else | |
val | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment