Module: Delineate::Serialization
- Extended by:
- ActiveSupport::Concern
- Included in:
- AttributeMap
- Defined in:
- lib/delineate/serialization.rb
Instance Method Summary collapse
-
#association_attribute_map(association) ⇒ Object
Access the map of an association defined in this map.
- #attribute_value(record, name) ⇒ Object
-
#map_attributes_for_write(attrs, options = nil) ⇒ Object
Given the specified api attributes hash, translates the attribute names to the corresponding model attribute names.
- #model_association(name) ⇒ Object
- #serializable_association_names(includes = nil) ⇒ Object
-
#serializable_attribute_names(includes = nil) ⇒ Object
Values for includes param: nil = include all attributes [] = do not include optional attributes […] = include the specified optional attributes.
Instance Method Details
#association_attribute_map(association) ⇒ Object
Access the map of an association defined in this map. Will throw an error if the map cannot be found and resolved.
38 39 40 41 42 |
# File 'lib/delineate/serialization.rb', line 38 def association_attribute_map(association) assoc = @associations[association] validate(assoc_attr_map(assoc), assoc[:klass_name]) assoc_attr_map(assoc) end |
#attribute_value(record, name) ⇒ Object
27 28 29 30 |
# File 'lib/delineate/serialization.rb', line 27 def attribute_value(record, name) model_attr = model_attribute(name) model_attr == :type ? record.read_attribute(:type) : record.send(model_attr) end |
#map_attributes_for_write(attrs, options = nil) ⇒ Object
Given the specified api attributes hash, translates the attribute names to the corresponding model attribute names. Recursive translation on associations is performed. API attributes that are defined as read-only are removed.
Input can be a single hash or an array of hashes.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/delineate/serialization.rb', line 49 def map_attributes_for_write(attrs, = nil) raise "Cannot process map #{@klass_name}:#{@name} for write because it has not been resolved" if !resolve (attrs.is_a?(Array) ? attrs : [attrs]).each do |attr_hash| raise ArgumentError, "Expected attributes hash but received #{attr_hash.inspect}" if attr_hash.is_not_a?(Hash) attr_hash.dup.symbolize_keys.each do |k, v| if (assoc = @associations[k]) map_association_attributes_for_write(assoc, attr_hash, k) else if @write_attributes.has_key?(k) attr_hash.rename_key!(k, @write_attributes[k]) if @write_attributes[k] != k else attr_hash.delete(k) end end end end attrs end |
#model_association(name) ⇒ Object
32 33 34 |
# File 'lib/delineate/serialization.rb', line 32 def model_association(name) @associations[name][:options][:model_attr] || name end |
#serializable_association_names(includes = nil) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/delineate/serialization.rb', line 18 def serializable_association_names(includes = nil) return @associations.keys if includes.nil? @associations.inject([]) do |assoc_names, assoc| assoc_names << assoc.first if !(option = assoc.last[:options][:optional]) || includes.include?(assoc.first) || includes.include?(option) assoc_names end end |
#serializable_attribute_names(includes = nil) ⇒ Object
Values for includes param:
nil = include all attributes
[] = do not include optional attributes
[...] = include the specified optional attributes
9 10 11 12 13 14 15 16 |
# File 'lib/delineate/serialization.rb', line 9 def serializable_attribute_names(includes = nil) attribute_names = @attributes.keys.reject {|k| @attributes[k][:access] == :w} return attribute_names if includes.nil? attribute_names.delete_if do |key| (option = @attributes[key][:optional]) && !includes.include?(key) && !includes.include?(option) end end |