class Contentful::Management::Field
A ContentType's field schema
Public Class Methods
value_exists?(value)
click to toggle source
@private
# File lib/contentful/management/field.rb, line 52 def self.value_exists?(value) value.respond_to?(:empty?) && !value.empty? || !value.respond_to?(:empty?) && value end
Public Instance Methods
deep_merge!(field)
click to toggle source
Takes a field object of content type Merges existing properties, items and validations of field with new one @private
# File lib/contentful/management/field.rb, line 23 def deep_merge!(field) merge_properties(field.properties) merge_items(field.items) merge_validations(field.validations) end
parse_value(key, value)
click to toggle source
Return parsed value of field object @private
# File lib/contentful/management/field.rb, line 40 def parse_value(key, value) case key when :items value.properties_to_hash if type == 'Array' && value.is_a?(Field) when :validations validations_to_hash(value) if value.is_a?(::Array) else value if self.class.value_exists?(value) end end
properties_to_hash()
click to toggle source
Extract values of field to hash @private
# File lib/contentful/management/field.rb, line 31 def properties_to_hash properties.each_with_object({}) do |(key, value), results| value = parse_value(key, value) results[key] = value if Field.value_exists?(value) end end
Private Instance Methods
index_by_type(validations)
click to toggle source
Build hash with validations
# File lib/contentful/management/field.rb, line 84 def index_by_type(validations) validations.each_with_object({}) { |validation, results| results[validation.type] = validation } end
merge_items(new_items)
click to toggle source
Update items of field object
# File lib/contentful/management/field.rb, line 64 def merge_items(new_items) items.properties.merge!(new_items.properties) if items.respond_to?(:properties) && new_items.respond_to?(:properties) end
merge_properties(new_properties)
click to toggle source
Update properties of field object
# File lib/contentful/management/field.rb, line 59 def merge_properties(new_properties) properties.merge!(new_properties.select { |name, _type| name != :items && name != :validations }) end
merge_validations(new_validations)
click to toggle source
Takes an array with new validations Returns merged existing and new validations
# File lib/contentful/management/field.rb, line 70 def merge_validations(new_validations) return unless new_validations validations_by_type = validations_by_type(validations) new_validations_by_type = validations_by_type(new_validations) validations_by_type.delete_if { |type, _v| new_validations_by_type[type] } self.validations = validations_by_type.values + new_validations_by_type.values end
validations_by_type(validations)
click to toggle source
# File lib/contentful/management/field.rb, line 79 def validations_by_type(validations) validations.is_a?(::Array) ? index_by_type(validations) : {} end
validations_to_hash(validations)
click to toggle source
# File lib/contentful/management/field.rb, line 88 def validations_to_hash(validations) validations.each_with_object([]) do |validation, results| validation_hash = validation.properties_to_hash results << validation.properties_to_hash if Field.value_exists?(validation_hash) end end