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

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

Overview

Adds fields logic for [Resource] classes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Create accessors for content type, asset, entry objects.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/contentful/management/resource/fields.rb', line 37

def self.included(base)
  base.fields_coercions.each_key do |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] = {} unless @fields[locale]
        @fields[locale][name.to_sym] = value
      end
    end
  end
end

Instance Method Details

#fields(wanted_locale = nil) ⇒ Hash

Returns the fields hash for the specified locale

Parameters:

  • wanted_locale (String) (defaults to: nil)

Returns:

  • (Hash)

    localized fields



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

def fields(wanted_locale = nil)
  wanted_locale = internal_resource_locale if wanted_locale.nil?
  @fields.fetch(wanted_locale.to_s, {})
end