Class: Relaxo::Model::Properties::Polymorphic

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxo/model/properties/composite.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klasses) ⇒ Polymorphic

Returns a new instance of Polymorphic.



30
31
32
33
# File 'lib/relaxo/model/properties/composite.rb', line 30

def initialize(klasses)
	@klasses = klasses
	@lookup = nil
end

Class Method Details

.[](*klasses) ⇒ Object



26
27
28
# File 'lib/relaxo/model/properties/composite.rb', line 26

def self.[] *klasses
	self.new(klasses)
end

Instance Method Details

#convert_from_primative(database, reference) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/relaxo/model/properties/composite.rb', line 55

def convert_from_primative(database, reference)
	# Legacy support for old polymorphic types - to remove.
	if Array === reference
		type, id = reference
	else
		id = reference
	end
	
	attributes = database.get(id).to_hash
	
	klass = lookup(attributes['type'])
	
	klass.fetch(database, attributes)
end

#convert_to_primative(object) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/relaxo/model/properties/composite.rb', line 47

def convert_to_primative(object)
	unless object.saved?
		object.save
	end

	[object.type, object.id]
end

#lookup(type) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/relaxo/model/properties/composite.rb', line 35

def lookup(type)
	unless @lookup
		@lookup = {}
		
		@klasses.each do |klass|
			@lookup[klass.type] = klass
		end
	end
	
	@lookup[type]
end