Sha256: b7aa6e89f0b40f6ed24d2ccdb5cb1d8cc8ce86f59f66b16710296ddc2e16f405

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

require "ostruct"

module Paddle
  class Object < OpenStruct
    def initialize(attributes)
      super to_ostruct(attributes)
    end

    def to_ostruct(obj)
      if obj.is_a?(Hash)
        OpenStruct.new(obj.map { |key, val| [ key, to_ostruct(val) ] }.to_h)
      elsif obj.is_a?(Array)
        obj.map { |o| to_ostruct(o) }
      else # Assumed to be a primitive value
        obj
      end
    end

    def update(**params)
      method_missing :update unless klass.respond_to? :update

      primary_attributes = klass.method(:update).parameters.select { |(type, name)| type == :keyreq }.map(&:last) # Identified by whatever is a required named parameter.

      primary_attributes.each do |attrib|
        # ? Should we need to handle blank strings here?
        params[attrib] = self[attrib] || self["#{attrib}_id"] # Handling for customer (customer_id) and id.
      end

      klass.public_send(:update, **params).each_pair { |key, val| self[key] = val } # Updating self

      self
    end

    private

    def klass
      self.class
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
paddle-2.7.0 lib/paddle/object.rb
paddle-2.6.0 lib/paddle/object.rb
paddle-2.5.2 lib/paddle/object.rb
paddle-2.5.1 lib/paddle/object.rb
paddle-2.5.0 lib/paddle/object.rb
paddle-2.4.1 lib/paddle/object.rb
paddle-2.4.0 lib/paddle/object.rb
paddle-2.3.0 lib/paddle/object.rb