Sha256: 9c7423c23ff1af6083dce999310d1fc7bc7ca110dc21ecaf684b91ec4b0d67c4

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'

module AppStoreConnect
  module Object
    module Attributes
      extend ActiveSupport::Concern

      class_methods do
        def attributes(&block)
          self::Attributes.class_eval(&block)
        end
      end

      included do
        attr_reader :attributes

        klass = Class.new do |attributes|
          include Object::Properties

          attributes.send(:define_method, :initialize) do |**kwargs|
            self.class.properties.each do |name, options|
              raise ArgumentError, "#{name} required" if options[:required] && !kwargs[name]

              value = kwargs.fetch(name, options[:default])

              instance_variable_set("@#{name}", value)
            end
          end

          def to_h
            {}.tap do |hash|
              self.class.properties.keys.each do |name|
                value = instance_variable_get("@#{name}")

                hash[name] = value unless value.nil?
              end
            end
          end
        end

        const_set('Attributes', klass)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
app_store_connect-0.25.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.23.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.22.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.21.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.20.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.19.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.18.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.17.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.16.0 lib/app_store_connect/object/attributes.rb