Sha256: a13ad5d2cd109f42aa28722d7e7011bc99efb1dc85ce958b0bff0e85e8c81572
Contents?: true
Size: 1.08 KB
Versions: 1
Compression:
Stored size: 1.08 KB
Contents
require_relative "mapped_attributes/version" module MappedAttributes def set_mapped_attributes(data, opts = {}) @unused_data_fields = data.keys mapping = get_attributes_mapping(opts[:as] || self.class.name.underscore) self.attributes = get_mapped_attributes(data, mapping) end def get_unmapped_attributes @unused_data_fields || [] end private def get_attributes_mapping(namespace) mapping = I18n.t(namespace, scope: 'activerecord.mappings') raise 'Mapping not defined in activerecord.mappings' unless mapping.is_a? Hash mapping end def get_mapped_attributes(data, mapping) mapping.inject({}) do |result, mapped_field| if mapped_field[1].is_a? Hash result[mapped_field[0]] = get_mapped_attributes(data, mapped_field[1]) else labels = Array(mapped_field[1]).map(&:downcase) @unused_data_fields.delete_if{|field| labels.include?(field.downcase)} mapped_data = data.detect{|k,v| labels.include?(k.downcase)} result[mapped_field[0]] = mapped_data[1] if mapped_data end result end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mapped_attributes-1.0.0 | lib/mapped_attributes.rb |