Module: ColdStorage::AssociationTree

Defined in:
lib/cold_storage/association_tree.rb

Overview

Normalizes the association trees used by cascade: and with: into a plain Hash of name => nested tree.

:lines                          => { lines: nil }
[:lines, :notes]                => { lines: nil, notes: nil }
[{ lines: [:taxes] }, :notes]   => { lines: [:taxes], notes: nil }

Nested values are left as given; each level normalizes its own.

Class Method Summary collapse

Class Method Details

.normalize(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/cold_storage/association_tree.rb', line 15

def normalize(value)
  case value
  when nil, false     then {}
  when Symbol, String then { value.to_sym => nil }
  when Array          then value.inject({}) { |tree, item| tree.merge(normalize(item)) }
  when Hash           then value.to_h { |name, nested| [name.to_sym, nested] }
  else
    raise ArgumentError, "expected an association name, array or hash, got #{value.inspect}"
  end
end