module WCC::Contentful::Test::Attributes

Constants

DEFAULTS

Public Class Methods

[](key) click to toggle source
# File lib/wcc/contentful/test/attributes.rb, line 27
def [](key)
  DEFAULTS[key]
end
default_value(field) click to toggle source

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

# File lib/wcc/contentful/test/attributes.rb, line 46
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) click to toggle source

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

# File lib/wcc/contentful/test/attributes.rb, line 33
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