Class: CacheCrispies::HashBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_crispies/hash_builder.rb

Overview

Builds out a JSON-ready Hash using the attributes in a Serializer

Instance Method Summary collapse

Constructor Details

#initialize(serializer) ⇒ HashBuilder

Initializes a new instance of CacheCrispies::HashBuilder

Parameters:


10
11
12
13
# File 'lib/cache_crispies/hash_builder.rb', line 10

def initialize(serializer)
  @serializer = serializer
  @condition_results = Memoizer.new
end

Instance Method Details

#callHash

Builds the Hash

Returns:

  • (Hash)

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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