Module: Xero::Associations::HasOne::ClassMethods

Defined in:
lib/xero/associations/has_one.rb

Instance Method Summary collapse

Instance Method Details

#has_one(association_name, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/xero/associations/has_one.rb', line 7

def has_one(association_name, options = {})

  define_method association_name do
    instance_variable_get(:"@#{association_name}")
  end

  define_method "#{association_name}=" do |value|
    if value.is_a?(Hash)
      klass = options[:class_name] || association_name
      value = "Xero::Models::#{klass.to_s.classify}".constantize.
        new(value)
    end
    instance_variable_set(:"@#{association_name}", value)
  end
end