Class: Contentful::DynamicEntry

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

Constant Summary collapse

KNOWN_TYPES =
{
  'String'   => :string,
  'Text'     => :string,
  'Symbol'   => :string,
  'Integer'  => :integer,
  'Float'    => :float,
  'Boolean'  => :boolean,
  'Date'     => :date,
  'Location' => Location
}

Constants included from Resource::SystemProperties

Resource::SystemProperties::SYS_COERCIONS

Constants included from Resource

Resource::COERCIONS

Instance Attribute Summary

Attributes included from Resource::SystemProperties

#sys

Attributes included from Resource

#client, #default_locale, #properties, #request

Class Method Summary collapse

Methods included from Resource::Fields

#fields, #initialize, #inspect

Methods included from Resource::SystemProperties

included, #initialize, #inspect

Methods included from Resource

#array?, #fields, #initialize, #inspect, #nested_locale_fields?, #reload, #sys

Class Method Details

.create(content_type) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/contentful/dynamic_entry.rb', line 18

def self.create(content_type)
  unless content_type.is_a? ContentType
    content_type = ContentType.new(content_type)
  end

  fields_coercions = Hash[
                     content_type.fields.map do |field|
                       [field.id.to_sym, KNOWN_TYPES[field.type]]
                     end
  ]

  Class.new DynamicEntry do
    content_type.fields.each do |f|
      define_method Support.snakify(f.id).to_sym do
        fields[f.id.to_sym]
      end
    end

    define_singleton_method :fields_coercions do
      fields_coercions
    end

    define_singleton_method :content_type do
      content_type
    end

    define_singleton_method :to_s do
      "Contentful::DynamicEntry[#{content_type.id}]"
    end

    define_singleton_method :inspect do
      "Contentful::DynamicEntry[#{content_type.id}]"
    end
  end
end