Class: Paddle::Object

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/paddle/object.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Object

Returns a new instance of Object.



5
6
7
# File 'lib/paddle/object.rb', line 5

def initialize(attributes)
  super to_ostruct(attributes)
end

Instance Method Details

#to_ostruct(obj) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/paddle/object.rb', line 9

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

#update(**params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/paddle/object.rb', line 19

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