Class: Og::RefersTo

Inherits:
Relation show all
Defined in:
lib/og/relation/refers_to.rb

Direct Known Subclasses

BelongsTo, HasOne

Instance Attribute Summary

Attributes inherited from Relation

#options

Instance Method Summary collapse

Methods inherited from Relation

#[], #[]=, enchant, #initialize, #method_missing, #resolve_options, #resolve_target

Constructor Details

This class inherits a constructor from Og::Relation

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Og::Relation

Instance Method Details

#enchantObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/og/relation/refers_to.rb', line 7

def enchant
	self[:foreign_key] = "#{foreign_name || target_singular_name}_#{target_pk}"

	owner_class.module_eval %{
		attr_accessor :#{target_singular_name}
		prop_accessor :#{foreign_key}, #{target_pkclass}

		def #{target_singular_name}(reload = false)
			unless reload
				@#{target_singular_name} = #{target_class}[@#{foreign_key}] unless @#{target_singular_name}
				return @#{target_singular_name}
			else
				return #{target_class}[@#{foreign_key}]
			end
		end

		def #{target_singular_name}=(obj)
			@#{foreign_key} = obj.#{target_pk}
		end
	}
end