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(dataset, path) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/relaxo/model/properties/composite.rb', line 53

def convert_from_primative(dataset, path)
	type, _, _ = path.rpartition('/')
	
	klass = lookup(type)
	
	klass.fetch(dataset, path)
end

#convert_to_primative(document) ⇒ Object

Raises:

  • (ArgumentError)


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

def convert_to_primative(document)
	raise ArgumentError.new("Document must be saved before adding to relationship") unless document.persisted?
	
	document.paths.first
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