Module: HasOne

Included in:
Klarna::Checkout::Resource
Defined in:
lib/klarna/checkout/concerns/has_one.rb

Instance Method Summary collapse

Instance Method Details

#has_one(association, klass = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/klarna/checkout/concerns/has_one.rb', line 2

def has_one(association, klass = nil)
  attr_accessor association

  define_method "#{association}=" do |new_value|
    inst_var = "@#{association}"
    case new_value
    when klass
      instance_variable_set(inst_var, new_value)
    when Hash
      instance_variable_set(inst_var, klass.new(new_value))
    else
      raise "Unsupported type for relation #{association}: #{new_value.class.to_s}"
    end
  end
end