Module: Contentful::Management::Resource::Fields

Included in:
Asset, Entry
Defined in:
lib/contentful/management/resource/fields.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Create accessors for content type, asset, entry objects.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/contentful/management/resource/fields.rb', line 30

def self.included(base)
  base.fields_coercions.keys.each { |name|
    accessor_name = Contentful::Management::Support.snakify(name)
    base.send :define_method, accessor_name do
      fields[name.to_sym]
    end
    base.send :define_method, "#{ accessor_name }_with_locales" do
      fields_for_query[name.to_sym]
    end
    base.send :define_method, "#{ accessor_name }=" do |value|
      fields[name.to_sym] = value
    end
    base.send :define_method, "#{ accessor_name }_with_locales=" do |values|
      values.each do |locale, value|
        @fields[locale] = {} if @fields[locale].nil?
        @fields[locale][name.to_sym] = value
      end
    end
  }
end

Instance Method Details

#fields(wanted_locale = default_locale) ⇒ Object



9
10
11
12
13
# File 'lib/contentful/management/resource/fields.rb', line 9

def fields(wanted_locale = default_locale)
  requested_locale = locale || wanted_locale
  @fields[requested_locale] = {} unless @fields[requested_locale]
  @fields[requested_locale]
end

#initialize(object = nil) ⇒ Object



15
16
17
18
19
# File 'lib/contentful/management/resource/fields.rb', line 15

def initialize(object = nil, *)
  super
  @fields = {}
  extract_fields_from_object! object if object
end

#inspect(info = nil) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/contentful/management/resource/fields.rb', line 21

def inspect(info = nil)
  if fields.empty?
    super(info)
  else
    super("#{ info } @fields=#{ fields.inspect }")
  end
end