Class: Reform::Composition
- Inherits:
-
Object
- Object
- Reform::Composition
- Defined in:
- lib/reform/form.rb
Overview
Keeps composition of models and knows how to transform a plain hash into a nested hash.
Class Method Summary collapse
- .map(options) ⇒ Object
-
.map_from(representer) ⇒ Object
Specific to representable.
- .model_for_property(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(models) ⇒ Composition
constructor
A new instance of Composition.
-
#nested_hash_for(attrs) ⇒ Object
TODO: make class method?.
Constructor Details
#initialize(models) ⇒ Composition
Returns a new instance of Composition.
110 111 112 113 114 |
# File 'lib/reform/form.rb', line 110 def initialize(models) models.each do |name, obj| instance_variable_set(:"@#{name}", obj) end end |
Class Method Details
.map(options) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/reform/form.rb', line 66 def map() @attr2obj = {} # {song: ["title", "track"], artist: ["name"]} .each do |mdl, meths| create_accessors(mdl, meths) attr_reader mdl # FIXME: unless already defined!! meths.each { |m| @attr2obj[m.to_s] = mdl } end end |
.map_from(representer) ⇒ Object
Specific to representable.
78 79 80 81 82 83 84 85 86 |
# File 'lib/reform/form.rb', line 78 def map_from(representer) = {} representer.representable_attrs.each do |cfg| [cfg.[:on]] ||= [] [cfg.[:on]] << cfg.name end map end |
.model_for_property(name) ⇒ Object
88 89 90 |
# File 'lib/reform/form.rb', line 88 def model_for_property(name) @attr2obj.fetch(name.to_s) end |
Instance Method Details
#nested_hash_for(attrs) ⇒ Object
TODO: make class method?
100 101 102 103 104 105 106 107 108 |
# File 'lib/reform/form.rb', line 100 def nested_hash_for(attrs) {}.tap do |hsh| attrs.each do |name, val| obj = self.class.model_for_property(name) hsh[obj] ||= {} hsh[obj][name.to_sym] = val end end end |