Sha256: e81ddf340fa6fb716c1df69a1ec8765194f048592428784f5b28c24051c79614

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

require 'contentful/constants'

module Contentful
  module Resource
    # Include this module into your Resource class to enable it
    # to deal with entry fields (but not asset fields)
    #
    # It depends on system properties being available
    module Fields
      # Returns all fields of the asset
      #
      # @return [Hash] fields for Resource on selected locale
      def fields(wanted_locale = nil)
        wanted_locale = (locale || default_locale) if wanted_locale.nil?
        @fields[wanted_locale.to_s] || {}
      end

      # Returns all fields of the asset with locales nested by field
      #
      # @return [Hash] fields for Resource grouped by field name
      def fields_with_locales
        remapped_fields = {}
        locales.each do |locale|
          fields(locale).each do |name, value|
            remapped_fields[name] ||= {}
            remapped_fields[name][locale.to_sym] = value
          end
        end

        remapped_fields
      end

      # @private
      module ClassMethods
        # No coercions, since no content type available
        def fields_coercions
          {}
        end
      end

      # @private
      def self.included(base)
        base.extend(ClassMethods)
      end

      # @private
      def initialize(object = nil, *)
        super
        extract_fields_from_object! object if object
      end

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

      def locales
        @fields.keys
      end

      private

      def extract_fields_from_object!(object)
        initialize_fields_for_localized_resource(object)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contentful-1.0.1 lib/contentful/resource/fields.rb