Sha256: d2f51adf323d5d1526a70a4965cdbf5014a7acf9c435db45de236a070a43e574

Contents?: true

Size: 1.73 KB

Versions: 33

Compression:

Stored size: 1.73 KB

Contents

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

module Contentful
  module Management
    # Wrapper for Entries with Cached Content Types
    class DynamicEntry < Contentful::Management::Entry
      # Coercions from Contentful Types to Ruby native types
      KNOWN_TYPES = {
        'String' => :string,
        'Text' => :string,
        'Symbol' => :string,
        'Integer' => :integer,
        'Float' => :float,
        'Boolean' => :boolean,
        'Date' => :date,
        'Location' => Location
      }.freeze

      # @private
      def self.define_singleton_properties(entry_class, content_type, client)
        entry_class.class_eval do
          define_singleton_method :content_type do
            content_type
          end

          define_singleton_method :client do
            client
          end

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

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

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

      # @private
      def self.create(content_type, client)
        unless content_type.is_a? ContentType
          content_type = ContentType.new(content_type)
        end

        Class.new DynamicEntry do
          DynamicEntry.define_singleton_properties(self, content_type, client)
          FieldAware.create_fields_for_content_type(self, :class)
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
contentful-management-3.8.0 lib/contentful/management/dynamic_entry.rb
contentful-management-3.7.0 lib/contentful/management/dynamic_entry.rb
contentful-management-3.6.0 lib/contentful/management/dynamic_entry.rb
contentful-management-3.5.0 lib/contentful/management/dynamic_entry.rb
contentful-management-3.4.0 lib/contentful/management/dynamic_entry.rb
contentful-management-3.3.0 lib/contentful/management/dynamic_entry.rb
contentful-management-3.2.0 lib/contentful/management/dynamic_entry.rb
contentful-management-3.1.0 lib/contentful/management/dynamic_entry.rb
contentful-management-3.0.0 lib/contentful/management/dynamic_entry.rb
contentful-management-2.13.1 lib/contentful/management/dynamic_entry.rb
contentful-management-2.13.0 lib/contentful/management/dynamic_entry.rb
contentful-management-2.12.1 lib/contentful/management/dynamic_entry.rb
contentful-management-2.12.0 lib/contentful/management/dynamic_entry.rb
contentful-management-2.11.0 lib/contentful/management/dynamic_entry.rb
contentful-management-2.10.0 lib/contentful/management/dynamic_entry.rb
contentful-management-2.9.1 lib/contentful/management/dynamic_entry.rb
contentful-management-2.9.0 lib/contentful/management/dynamic_entry.rb
contentful-management-2.8.2 lib/contentful/management/dynamic_entry.rb
contentful-management-2.8.1 lib/contentful/management/dynamic_entry.rb
contentful-management-2.8.0 lib/contentful/management/dynamic_entry.rb