lib/relaxo/model/properties/composite.rb in relaxo-model-0.18.0 vs lib/relaxo/model/properties/composite.rb in relaxo-model-0.19.0
- old
+ new
@@ -44,59 +44,59 @@
@lookup[type]
end
def convert_to_primative(document)
raise ArgumentError.new("Document must be saved before adding to relationship") unless document.persisted?
-
+
document.paths.first
end
-
+
def convert_from_primative(dataset, path)
type, _, _ = path.rpartition('/')
klass = lookup(type)
klass.fetch(dataset, path)
end
end
-
+
class BelongsTo
def self.[] *klasses
if klasses.size == 1
self.new(klasses[0])
else
Polymorphic.new(klasses)
end
end
-
+
def initialize(klass)
@klass = klass
end
-
+
def convert_to_primative(document)
raise ArgumentError.new("Document must be saved before adding to relationship") unless document.persisted?
document.paths.first
end
-
+
def convert_from_primative(dataset, path)
@klass.fetch(dataset, path)
end
end
class HasOne < BelongsTo
end
-
+
class HasMany < HasOne
def convert_to_primative(documents)
documents.each do |document|
raise ArgumentError.new("Document must be saved before adding to relationship") unless document.persisted?
end
documents.collect{|document| document.paths.first}
end
-
+
def convert_from_primative(dataset, value)
value.collect{|id| @klass.fetch(dataset, id)}
end
end
@@ -113,16 +113,16 @@
end
def initialize(klass)
@klass = Attribute.new(klass)
end
-
+
def convert_to_primative(value)
value.collect do |item|
@klass.convert_to_primative(item)
end
end
-
+
def convert_from_primative(dataset, value)
value.collect do |item|
@klass.convert_from_primative(dataset, item)
end
end