Sha256: d913ec9e52ad9df02321bd69dba4dab508cab04c842648d6b7e360caf82cc2b8

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module WCC::Contentful::Test::Attributes
  DEFAULTS = {
    String: 'test',
    Int: 0,
    Float: 0.0,
    DateTime: Time.at(0),
    Boolean: false,
    Json: -> { OpenStruct.new },
    Coordinates: -> { OpenStruct.new }
  }.freeze

  class << self
    def [](key)
      DEFAULTS[key]
    end

    ##
    # Get a hash of default values for all attributes unique to the given Contentful model.
    def defaults(const)
      unless const < WCC::Contentful::Model
        raise ArgumentError, "#{const} is not a subclass of WCC::Contentful::Model"
      end

      const.content_type_definition.fields.each_with_object({}) do |(name, f), h|
        h[name.to_sym] = h[name.underscore.to_sym] = default_value(f)
      end
    end

    ##
    # Gets the default value for a contentful IndexedRepresentation::Field.
    # This comes from the 'content_type_definition' of a contentful model class.
    def default_value(field)
      return [] if field.array
      return unless field.required

      val = DEFAULTS[field]
      return val.call if val.respond_to?(:call)

      val
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wcc-contentful-0.4.0.pre.rc lib/wcc/contentful/test/attributes.rb
wcc-contentful-0.4.0.pre.beta lib/wcc/contentful/test/attributes.rb