Class: SimpleParams::HashBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ HashBuilder

Returns a new instance of HashBuilder.



5
6
7
# File 'lib/simple_params/hash_builder.rb', line 5

def initialize(params)
  @params = params
end

Instance Method Details

#buildObject

TODO: This still needs specs around it, as well as a SIGNIFICANT refactor



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple_params/hash_builder.rb', line 10

def build
  hash = {}
  attributes = @params.attributes
  attributes.each do |attribute|
    raw_attribute = @params.send(attribute)
    if raw_attribute.nil?
      hash[attribute] = nil
    elsif raw_attribute.is_a?(SimpleParams::Params)
      hash[attribute] = @params.send(attribute).to_hash
    elsif raw_attribute.is_a?(Array)
      attribute_array = []
      raw_attribute.each do |r_attr|
        unless r_attr.nil?
          attribute_array << r_attr.to_hash
        end
      end
      hash[attribute] = attribute_array
    else
      hash[attribute] = @params.send(attribute)
    end
  end
  hash
end