module GiftWrap::Presenter
Public Class Methods
included(base)
click to toggle source
# File lib/gift_wrap/presenter.rb, line 4 def self.included(base) base.extend(ClassMethods) if GiftWrap.config.use_serializers? && defined? ActiveModel::Serializers::JSON base.send(:include, ActiveModel::Serializers::JSON) end end
new(wrapped_object, **options)
click to toggle source
Current options:
-
associations: a hash mapping association names to the presenter class to use when wrapping the association. Used to override the :with setting of a call to ::wrap_association at an instance level.
# File lib/gift_wrap/presenter.rb, line 16 def initialize(wrapped_object, **options) @wrapped_object = wrapped_object @wrapped_association_presenters = options.fetch(:associations, {}) end
Public Instance Methods
attributes()
click to toggle source
For use by ActiveModel::Serializers::JSON in building a default set of values when calling as_json or to_json
# File lib/gift_wrap/presenter.rb, line 43 def attributes self.class.attributes.each.with_object({}) do |msg, attr_hash| attr_hash[msg.to_s] = self.send(msg) end end
wrapped_association_presenter(association_name)
click to toggle source
Used in methods defined by ::wrap_association to determine the presenter class that is used for a particular association name. First checks any instance-specific options for the association name, and falls back to those defined by the :with option passed to any ::wrap_association call for said association_name.
# File lib/gift_wrap/presenter.rb, line 26 def wrapped_association_presenter(association_name) if @wrapped_association_presenters.none? self.class.wrapped_association_defaults.fetch(association_name) do |name| raise NoMethodError.new("No association registered as '#{name}'.") end else @wrapped_association_presenters.fetch( association_name, self.class.wrapped_association_defaults.fetch(association_name) do |name| raise NoMethodError.new("No association registered as '#{name}'.") end) end end