Module: GiftWrap::Presenter::ClassMethods

Defined in:
lib/gift_wrap/presenter.rb

Instance Method Summary collapse

Instance Method Details

#attribute(*names) ⇒ Object

Declares one or more messages (method names) to be attributes.



84
85
86
87
88
# File 'lib/gift_wrap/presenter.rb', line 84

def attribute(*names)
  names.flatten.each do |name|
    attributes << name
  end
end

#attributesObject

Contains the of messages (which are used as hash keys) to send to self and collect when building an attributes hash.



55
56
57
# File 'lib/gift_wrap/presenter.rb', line 55

def attributes
  @attributes ||= Set.new
end

#unwrap_for(*names, attribute: false, **options) ⇒ Object

Declares that one or more received messages (method calls) should be delegated directly to the wrapped object, and which may be optionally declared as attributes.



94
95
96
97
98
99
100
101
# File 'lib/gift_wrap/presenter.rb', line 94

def unwrap_for(*names, attribute: false, **options)
  names = names.flatten
  names.each do |name|
    unwrapped_methods << name
    attributes        << name if attribute
  end
  delegate(*names, to: :@wrapped_object)
end

#unwrapped_methodsObject

Contains the list of methods which will be delegated to the wrapped object rather than defined on presenter class itself.



62
63
64
# File 'lib/gift_wrap/presenter.rb', line 62

def unwrapped_methods
  @unwrapped_methods ||= Set.new
end

#wrap_association(association, with:, as: association, **options) ⇒ Object

Declares that the result of a delegated method call should be wrapped in another presenter, as defined by the :with keyword argument. This results in a method by the name of the first parameter by default, but may be customized with the :as keyword argument. Associations whose method produces an enumerable (ideally an Array) will have each item wrapped in the presenter and collected in an Array which is then returned.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gift_wrap/presenter.rb', line 110

def wrap_association(association, with: , as: association, **options)
  wrapped_association_defaults[as] = with
  define_method(as) do
    presenter_class = wrapped_association_presenter(as)
    associated      = @wrapped_object.send(association)
    memoized_within = "@#{as}"
    instance_variable_get(memoized_within) ||
      instance_variable_set(memoized_within,
        if associated.respond_to?(:each)
          associated.map { |assoc| presenter_class.new(assoc, **options) }
        else
          presenter_class.new(associated, **options)
        end
      )
  end
end

#wrapped_as(reference) ⇒ Object

Defines a private method name by which the wrapped object may be referenced internally.



75
76
77
78
79
80
# File 'lib/gift_wrap/presenter.rb', line 75

def wrapped_as(reference)
  define_method(reference) do
    @wrapped_object
  end
  send(:private, reference)
end

#wrapped_association_defaultsObject

Contains the default settings for building any associations. These may be overridden on a per-intance basis.



69
70
71
# File 'lib/gift_wrap/presenter.rb', line 69

def wrapped_association_defaults
  @wrapped_association_defaults ||= {}
end