Module: Magento::ModelMapper
- Defined in:
- lib/magento/model_mapper.rb
Class Method Summary collapse
Class Method Details
.map_array(key, values) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/magento/model_mapper.rb', line 37 def self.map_array(key, values) result = [] values.each do |value| if value.is_a?(Hash) class_name = Magento.inflector.camelize(Magento.inflector.singularize(key)) result << map_hash(Object.const_get("Magento::#{class_name}"), value) else result << value end end result end |
.map_hash(model, values) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/magento/model_mapper.rb', line 19 def self.map_hash(model, values) object = model.is_a?(Class) ? model.new : model values.each do |key, value| unless object.respond_to?(key) && object.respond_to?("#{key}=") object.singleton_class.instance_eval { attr_accessor key } end if value.is_a?(Hash) class_name = Magento.inflector.camelize(Magento.inflector.singularize(key)) value = map_hash(Object.const_get("Magento::#{class_name}"), value) elsif value.is_a?(Array) value = map_array(key, value) end object.send("#{key}=", value) end object end |
.to_hash(object) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/magento/model_mapper.rb', line 3 def self.to_hash(object) hash = {} object.instance_variables.each do |attr| key = attr.to_s.delete('@') value = object.send(key) value = to_hash(value) if value.class.name.include?('Magento::') if value.is_a? Array value = value.map do |item| item.class.name.include?('Magento::') ? to_hash(item) : item end end hash[key] = value end hash end |