class CacheCrispies::HashBuilder
Builds out a JSON-ready Hash using the attributes in a Serializer
Attributes
condition_results[R]
serializer[R]
Public Class Methods
new(serializer)
click to toggle source
Initializes a new instance of CacheCrispies::HashBuilder
@param serializer [CacheCrispies::Base] an instance of a subclass of
CacheCrispies::Base
# File lib/cache_crispies/hash_builder.rb, line 10 def initialize(serializer) @serializer = serializer @condition_results = Memoizer.new end
Public Instance Methods
call()
click to toggle source
Builds the Hash
@return [Hash]
# File lib/cache_crispies/hash_builder.rb, line 18 def call return unless @serializer.model hash = {} serializer.attributes.each do |attrib| deepest_hash = hash next unless show?(attrib) attrib.nesting.each do |key| deepest_hash[key] ||= {} deepest_hash = deepest_hash[key] end value = value_for(attrib) if attrib.key deepest_hash[attrib.key] = value else deepest_hash.merge! value end end hash end
Private Instance Methods
show?(attribute)
click to toggle source
# File lib/cache_crispies/hash_builder.rb, line 49 def show?(attribute) # Memoize conditions so they aren't executed for each attribute in a # show_if block attribute.conditions.all? do |cond| condition_results.fetch(cond.uid) do cond.true_for?(serializer) end end end
value_for(attribute)
click to toggle source
# File lib/cache_crispies/hash_builder.rb, line 59 def value_for(attribute) meth = attribute.method_name target = if meth != :itself && serializer.respond_to?(meth) serializer else serializer.model end # TODO: rescue NoMethodErrors here with something more telling attribute.value_for(target, serializer.options) end