Sha256: 6944deaca4ea71c9546b806bae77303aec357d4822680d4ba180aba5cc13595b

Contents?: true

Size: 1.12 KB

Versions: 9

Compression:

Stored size: 1.12 KB

Contents

module Contentful
  module Management
    module Resource
      # Adds metadata logic for [Resource] classes
      module Metadata
        # Returns the metadata hash
        attr_reader :_metadata

        # @private
        def initialize(object = nil, *)
          super
          @_metadata = {}
          extract_metadata_from_object! object if object
        end

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

        private

        def extract_metadata_from_object!(object)
          return unless object.key?('metadata')
          object['metadata'].each do |key, value|
            @_metadata[key.to_sym] = if key == 'tags'
                                       coerce_tags(value)
                                     else
                                       value
                                     end
          end
        end

        def coerce_tags(tags)
          tags.map { |tag| Contentful::Management::Link.new(tag) }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
contentful-management-3.8.0 lib/contentful/management/resource/metadata.rb
contentful-management-3.7.0 lib/contentful/management/resource/metadata.rb
contentful-management-3.6.0 lib/contentful/management/resource/metadata.rb
contentful-management-3.5.0 lib/contentful/management/resource/metadata.rb
contentful-management-3.4.0 lib/contentful/management/resource/metadata.rb
contentful-management-3.3.0 lib/contentful/management/resource/metadata.rb
contentful-management-3.2.0 lib/contentful/management/resource/metadata.rb
contentful-management-3.1.0 lib/contentful/management/resource/metadata.rb
contentful-management-3.0.0 lib/contentful/management/resource/metadata.rb