Class: Contentful::Field

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

Overview

Constant Summary

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
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Field

Returns a new instance of Field



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

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



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

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items



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

def items
  @items
end

Returns the value of attribute link_type



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

def link_type
  @link_type
end

#localizedObject (readonly)

Returns the value of attribute localized



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

def localized
  @localized
end

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

#rawObject (readonly)

Returns the value of attribute raw



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

def raw
  @raw
end

#requiredObject (readonly)

Returns the value of attribute required



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

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

#coerce(value) ⇒ Object

Coerces value to proper type



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

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

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