Method: QuickTravel::Adapter.belongs_to

Defined in:
lib/quick_travel/adapter.rb

.belongs_to(relation_name, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/quick_travel/adapter.rb', line 43

def self.belongs_to(relation_name, options = {})
  define_method relation_name do
    instance_variable_get("@#{relation_name}") || instance_variable_set(
      "@#{relation_name}",
      begin
        attrs = instance_variable_get("@#{relation_name}_attributes")
        return nil unless attrs
        klass = QuickTravel.const_get(options[:class_name] || relation_name.to_s.singularize.classify)
        klass.new(attrs)
      end
    )
  end
end