Module: AttributeCartographer::ClassMethods

Defined in:
lib/attribute_cartographer.rb

Instance Method Summary collapse

Instance Method Details

#map(*args) ⇒ Object



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

def map *args
  @mapper ||= {}

  (from, to), (f1, f2) = args.partition { |a| !(Proc === a) }

  passthrough = ->(v) { v }
  f1 ||= passthrough
  f2 ||= passthrough

  if Array === from
    if f1.arity == 1
      from.each { |k| @mapper[k] = [k, f1] }
    else
      from.each { |k| @mapper[k] = f1 }
    end
  else
    raise AttributeCartographer::InvalidArgumentError if to && f1.arity == 2

    @mapper[from] = (f1.arity == 1 ? [to || from, f1] : f1)
    @mapper[to] = [from, f2] if to && (f1 == f2 || f2 != passthrough)
  end
end