Class: Contentful::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/field.rb

Overview

Constant Summary collapse

KNOWN_TYPES =

Coercions from Contentful Types to Ruby native types

{
  'String'   => StringCoercion,
  'Text'     => TextCoercion,
  'Symbol'   => SymbolCoercion,
  'Integer'  => IntegerCoercion,
  'Number'   => FloatCoercion,
  'Boolean'  => BooleanCoercion,
  'Date'     => DateCoercion,
  'Location' => LocationCoercion,
  'Object'   => ObjectCoercion,
  'Array'    => ArrayCoercion,
  'Link'     => LinkCoercion,
  'RichText' => RichTextCoercion
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Field

Returns a new instance of Field



26
27
28
29
30
31
32
33
34
35
# File 'lib/contentful/field.rb', line 26

def initialize(json)
  @raw = json
  @id = json.fetch('id', nil)
  @name = json.fetch('name', nil)
  @type = json.fetch('type', nil)
  @link_type = json.fetch('linkType', nil)
  @items = json.key?('items') ? Field.new(json.fetch('items', {})) : nil
  @required = json.fetch('required', false)
  @localized = json.fetch('localized', false)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id



24
25
26
# File 'lib/contentful/field.rb', line 24

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items



24
25
26
# File 'lib/contentful/field.rb', line 24

def items
  @items
end

Returns the value of attribute link_type



24
25
26
# File 'lib/contentful/field.rb', line 24

def link_type
  @link_type
end

#localizedObject (readonly)

Returns the value of attribute localized



24
25
26
# File 'lib/contentful/field.rb', line 24

def localized
  @localized
end

#nameObject (readonly)

Returns the value of attribute name



24
25
26
# File 'lib/contentful/field.rb', line 24

def name
  @name
end

#rawObject (readonly)

Returns the value of attribute raw



24
25
26
# File 'lib/contentful/field.rb', line 24

def raw
  @raw
end

#requiredObject (readonly)

Returns the value of attribute required



24
25
26
# File 'lib/contentful/field.rb', line 24

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type



24
25
26
# File 'lib/contentful/field.rb', line 24

def type
  @type
end

Instance Method Details

#coerce(value, configuration) ⇒ Object

Coerces value to proper type



38
39
40
41
42
43
44
45
# File 'lib/contentful/field.rb', line 38

def coerce(value, configuration)
  return value if type.nil?
  return value if value.nil?

  options = {}
  options[:coercion_class] = KNOWN_TYPES[items.type] unless items.nil?
  KNOWN_TYPES[type].new(value, options).coerce(configuration)
end