Sha256: fbb5152fa926ad150f838eb5560fb6409d3a969b30e3cf4c0b4cfa77335530f7

Contents?: true

Size: 1.09 KB

Versions: 11

Compression:

Stored size: 1.09 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.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

11 entries across 11 versions & 1 rubygems

Version Path
app_store_connect-0.15.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.14.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.13.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.12.2 lib/app_store_connect/object/attributes.rb
app_store_connect-0.12.1 lib/app_store_connect/object/attributes.rb
app_store_connect-0.12.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.11.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.10.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.9.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.8.0 lib/app_store_connect/object/attributes.rb
app_store_connect-0.7.0 lib/app_store_connect/object/attributes.rb