Class: Contentful::Management::DynamicEntry
- Defined in:
- lib/contentful/management/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
Instance Attribute Summary
Attributes inherited from Entry
Attributes included from Resource::SystemProperties
Attributes included from Resource
#client, #default_locale, #properties, #request
Class Method Summary collapse
Methods inherited from Entry
all, #archive, #archived?, #destroy, #fields_for_query, #fields_from_attributes, find, #locale, #publish, #published?, #save, #unarchive, #unpublish, #update
Methods included from Resource::EntryFields
Methods included from Resource::Fields
#fields, included, #initialize, #inspect
Methods included from Resource::Refresher
Methods included from Resource::SystemProperties
included, #initialize, #inspect
Methods included from Resource
#array?, #fields, #initialize, #inspect, #nested_locale_fields?, #sys
Class Method Details
.create(content_type) ⇒ Object
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/contentful/management/dynamic_entry.rb', line 20 def self.create(content_type) unless content_type.is_a? ContentType content_type = ContentType.new(content_type) end # TODO no support for an empty content type (undefined method map for Field) 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 |field| accessor_name = Support.snakify(field.id) define_method accessor_name do fields[field.id.to_sym] end define_method "#{ accessor_name }_with_locales" do fields_for_query[field.id.to_sym] end define_method "#{ accessor_name }=" do |value| fields[field.id.to_sym] = value end define_method "#{ accessor_name }_with_locales=" do |values| values.each do |locale, value| @fields[locale] = {} if @fields[locale].nil? @fields[locale][field.id.to_sym] = value end 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::Management::DynamicEntry[#{ content_type.id }]" end define_singleton_method :inspect do "Contentful::Management::DynamicEntry[#{ content_type.id }]" end end end |