Sha256: b93940dc72ac881d710d38619d14119ec37110eb01b49be7d0cc8e0ee288ad93

Contents?: true

Size: 1.1 KB

Versions: 12

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.each_key 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

12 entries across 12 versions & 1 rubygems

Version Path
app_store_connect-0.38.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.37.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.36.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.35.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.34.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.33.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.32.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.30.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.29.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.28.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.27.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.26.0 lib/app_store_connect/object/attributes.rb