Sha256: 341ded6bd1b2b9a637b0be61813f2f4bfaa4c7c1e54928a61f37456e29ef5f9e

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require_relative 'resource'
require_relative 'resource/fields'
require_relative 'location'

module Contentful
  class DynamicEntry < Entry
    KNOWN_TYPES = {
      'String'   => :string,
      'Text'     => :string,
      'Symbol'   => :string,
      'Integer'  => :integer,
      'Float'    => :float,
      'Boolean'  => :boolean,
      'Date'     => :date,
      'Location' => Location,
    }

    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{ |field|
          [field.id.to_sym, KNOWN_TYPES[field.type]]
        }
      ]

      Class.new DynamicEntry do
        content_type.fields.each{ |f|
          define_method Support.snakify(f.id).to_sym do
            fields[f.id.to_sym]
          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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contentful-0.1.0 lib/contentful/dynamic_entry.rb