Module: WCC::Contentful::Test::Attributes

Defined in:
lib/wcc/contentful/test/attributes.rb

Constant Summary collapse

DEFAULTS =
{
  String: 'test',
  Int: 0,
  Float: 0.0,
  DateTime: Time.at(0).to_s,
  Boolean: false,
  Json: ->(_f) { {} },
  Coordinates: ->(_f) { {} },
  Asset: ->(f) {
           WCC::Contentful::Link.new(
             "fake-#{f.name}-#{SecureRandom.urlsafe_base64[1..6]}",
             :Asset
           ).raw
         },
  Link: ->(f) {
          WCC::Contentful::Link.new(
            "fake-#{f.name}-#{SecureRandom.urlsafe_base64[1..6]}",
            :Link
          ).raw
        }
}.freeze

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



27
28
29
# File 'lib/wcc/contentful/test/attributes.rb', line 27

def [](key)
  DEFAULTS[key]
end

.default_value(field) ⇒ Object

Gets the default value for a contentful IndexedRepresentation::Field. This comes from the ‘content_type_definition’ of a contentful model class.



42
43
44
45
46
47
48
49
50
# File 'lib/wcc/contentful/test/attributes.rb', line 42

def default_value(field)
  return [] if field.array
  return unless field.required

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

  val
end

.defaults(const) ⇒ Object

Get a hash of default values for all attributes unique to the given Contentful model.



33
34
35
36
37
# File 'lib/wcc/contentful/test/attributes.rb', line 33

def defaults(const)
  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