Module: Reportinator::Helpers

Included in:
Base, ModelReport
Defined in:
lib/reportinator/helpers.rb

Instance Method Summary collapse

Instance Method Details

#merge_hash(target, source) ⇒ Object



3
4
5
6
7
# File 'lib/reportinator/helpers.rb', line 3

def merge_hash(target, source)
  target = target.present? ? target : {}
  source = source.present? ? source : {}
  merge_hash!(target, source)
end

#merge_hash!(target, source) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/reportinator/helpers.rb', line 9

def merge_hash!(target, source)
  raise "Target: #{target} is not a hash" unless target.instance_of?(Hash)
  raise "Source: #{source} is not a hash" unless source.instance_of?(Hash)
  target.merge(source) do |key, old_value, new_value|
    if old_value.instance_of?(Hash) && new_value.instance_of?(Hash)
      merge_hash!(old_value, new_value)
    elsif new_value.present?
      new_value
    else
      old_value
    end
  end
end

#symbolize_attributes(target) ⇒ Object



23
24
25
26
27
# File 'lib/reportinator/helpers.rb', line 23

def symbolize_attributes(target)
  raise "Missing attributes" unless target.respond_to? :attributes
  raise "Invalid attributes" unless target.attributes.instance_of? Hash
  target.attributes.transform_keys { |key| key.to_sym }
end