Class: HardBoiled::Presenter

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.extract_options!
  @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
  options = args.extract_options!
  params = options[:params]
  value =
    if options[:nil]
      nil
    else
      if static = args.shift
        static
      else
        object = options[:parent] ? parent_subject : subject
        method_name = options[: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, options), options), options
    end
  self
end

Instance Attribute Details

#parent_subjectObject (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

#subjectObject (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_hashObject



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

Parameters:

  • name (Symbol)

    the identifier for a trait, like :profile

  • (Hash{:only => Array, :except => Array})

Returns:

  • (true, false)

    dependening on in- or exclusion of this trait



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