module GiftWrap::Presenter::ClassMethods
Public Instance Methods
Declares one or more messages (method names) to be attributes.
# File lib/gift_wrap/presenter.rb, line 84 def attribute(*names) names.flatten.each do |name| attributes << name end end
Contains the of messages (which are used as hash keys) to send to self and collect when building an attributes hash.
# File lib/gift_wrap/presenter.rb, line 55 def attributes @attributes ||= Set.new end
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.
# 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
Contains the list of methods which will be delegated to the wrapped object rather than defined on presenter class itself.
# File lib/gift_wrap/presenter.rb, line 62 def unwrapped_methods @unwrapped_methods ||= Set.new end
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.
# 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
Defines a private method name by which the wrapped object may be referenced internally.
# File lib/gift_wrap/presenter.rb, line 75 def wrapped_as(reference) define_method(reference) do @wrapped_object end send(:private, reference) end
Contains the default settings for building any associations. These may be overridden on a per-intance basis.
# File lib/gift_wrap/presenter.rb, line 69 def wrapped_association_defaults @wrapped_association_defaults ||= {} end