Sha256: 1ec7bc1ede411ad45ebce692dfbff14e3dec7ad20b28662fdc3510fb1eec2195

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Contentful
  module Resource
    # Adds the feature to have system properties to a Resource.
    module SystemProperties
      SYS_COERCIONS = {
        type: :string,
        id: :string,
        space: nil,
        contentType: nil,
        linkType: :string,
        revision: :integer,
        createdAt: :date,
        updatedAt: :date,
        locale: :string,
      }
      attr_reader :sys

      def initialize(object, *)
        super
        @sys = extract_from_object object['sys'], :sys
      end

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

      module ClassMethods
        def sys_coercions
          SYS_COERCIONS
        end
      end

      def self.included(base)
        base.extend(ClassMethods)

        base.sys_coercions.keys.each{ |name|
          base.send :define_method, Contentful::Support.snakify(name) do
            sys[name.to_sym]
          end
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contentful-0.4.0 lib/contentful/resource/system_properties.rb
contentful-0.3.5 lib/contentful/resource/system_properties.rb