26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/omg-attrs.rb', line 26
def attrs(*attrs)
return list_attrs(attrs) if is_list?
base_attrs, nested_attrs = attrs.partition { |attr| attr.is_a?(Symbol) }
nested_attrs.map! do |attr|
if attr.is_a?(Hash)
nested_attrs(attr)
elsif attr.is_a?(Array) && attr.size == 2 && attr.first.is_a?(Symbol)
nested_attrs([attr].to_h)
elsif attr.is_a?(Array)
attr.map { |a| a.attrs(a) }
else
raise ArgumentError, "Invalid attribute: #{attr}"
end
end
base_attrs = base_attrs.to_h do |attr|
[attr, get(attr)]
end
nested_attrs.reduce(base_attrs, :merge)
end
|