Sha256: dee21012b1186334fe84459e536f66294cd41abff25eb9f7b8c843a0681797e1

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Contentful
  module Management
    module Resource
      # Adds the feature to have system properties to a Resource.
      module SystemProperties
        # @private
        # Coercions for System Properties to native types
        SYS_COERCIONS = {
          type: :string,
          id: :string,
          space: nil,
          contentType: nil,
          linkType: :string,
          revision: :integer,
          createdAt: :date,
          updatedAt: :date,
          publishedAt: :date,
          locale: :string
        }.freeze

        attr_reader :sys

        # @private
        def initialize(object = { 'sys' => nil }, *)
          super
          object ||= { 'sys' => nil }
          @sys = extract_from_object object['sys'], :sys
        end

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

        # @private
        module ClassMethods
          # @private
          def sys_coercions
            SYS_COERCIONS
          end
        end

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

          base.sys_coercions.each_key do |name|
            accessor_name = Contentful::Management::Support.snakify(name)
            base.send :define_method, accessor_name do
              sys[name.to_sym]
            end
            base.send :define_method, "#{accessor_name}=" do |value|
              sys[name.to_sym] = value
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
contentful-management-3.10.0 lib/contentful/management/resource/system_properties.rb
contentful-management-3.9.0 lib/contentful/management/resource/system_properties.rb