Class: HardBoiled::Presenter
- Inherits:
-
Object
- Object
- HardBoiled::Presenter
- Defined in:
- lib/hard-boiled/presenter.rb
Overview
This class pretty much resembles what Thoughtbot did in [FactoryGirl’s DefinitionProxy](github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/definition_proxy.rb) although it just reduces a ‘class` to a simple `Hash`
Defined Under Namespace
Classes: MissingFilterError
Constant Summary collapse
- UNPROXIED_METHODS =
%w(__send__ __id__ nil? respond_to? class send object_id extend instance_eval initialize block_given? raise)
Instance Attribute Summary collapse
-
#parent_subject ⇒ Object
readonly
Returns the value of attribute parent_subject.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(subject, parent = nil) ⇒ Presenter
constructor
A new instance of Presenter.
- #to_hash ⇒ Object
Constructor Details
#initialize(subject, parent = nil) ⇒ Presenter
Returns a new instance of Presenter.
24 25 26 27 28 |
# File 'lib/hard-boiled/presenter.rb', line 24 def initialize subject, parent = nil @subject = subject @parent_subject = parent @hash = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args, &block) ⇒ Object (private)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/hard-boiled/presenter.rb', line 35 def method_missing id, *args, &block = args. params = [:params] value = if [:nil] nil else if static = args.shift static else object = [:parent] ? parent_subject : subject method_name = [:from] || id if params object.__send__ method_name, *params else object.__send__ method_name end end end @hash[id] = if block_given? if value.kind_of? Array value.map do |v| self.class.define(v, self.subject, &block) end else self.class.define(value, self.subject, &block) end else __set_defaults __format_value(__apply_filters(value, ), ), end self end |
Instance Attribute Details
#parent_subject ⇒ Object (readonly)
Returns the value of attribute parent_subject.
15 16 17 |
# File 'lib/hard-boiled/presenter.rb', line 15 def parent_subject @parent_subject end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
15 16 17 |
# File 'lib/hard-boiled/presenter.rb', line 15 def subject @subject end |
Class Method Details
.define(object, parent = nil, &block) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/hard-boiled/presenter.rb', line 17 def self.define object, parent = nil, &block # if I could only remove the duplicate `obj` obj = new(object, parent) obj.instance_eval(&block) obj.to_hash end |
Instance Method Details
#to_hash ⇒ Object
30 31 32 |
# File 'lib/hard-boiled/presenter.rb', line 30 def to_hash @hash end |