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(*args) ⇒ Presenter
constructor
A new instance of Presenter.
- #to_hash ⇒ Object
-
#with_trait(name, &block) ⇒ true, false
Decide whether the given trait is being needed.
Constructor Details
#initialize(*args) ⇒ Presenter
Returns a new instance of Presenter.
26 27 28 29 30 |
# File 'lib/hard_boiled/presenter.rb', line 26 def initialize *args @options = args. @subject, @parent_subject = args @hash = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args, &block) ⇒ Object (private)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/hard_boiled/presenter.rb', line 50 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.
17 18 19 |
# File 'lib/hard_boiled/presenter.rb', line 17 def parent_subject @parent_subject end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
17 18 19 |
# File 'lib/hard_boiled/presenter.rb', line 17 def subject @subject end |
Class Method Details
.define(*args, &block) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/hard_boiled/presenter.rb', line 19 def self.define *args, &block # if I could only remove the duplicate `obj` obj = new(*args) obj.instance_eval(&block) obj.to_hash end |
Instance Method Details
#to_hash ⇒ Object
45 46 47 |
# File 'lib/hard_boiled/presenter.rb', line 45 def to_hash @hash end |
#with_trait(name, &block) ⇒ true, false
Decide whether the given trait is being needed
38 39 40 41 42 43 |
# File 'lib/hard_boiled/presenter.rb', line 38 def with_trait name, &block if (@options[:except].blank? || !@options[:except].include?(name)) && (@options[:only].blank? || @options[:only].include?(name)) self.instance_eval(&block) end end |