From: David Lutterkort <[email protected]>
This gives us an opportunity to
* set the count of collections to the number of entries
* generate id's for collections embedded in other objects
---
server/lib/cimi/models/base.rb | 6 ++++++
server/lib/cimi/models/collection.rb | 6 ++++++
server/lib/cimi/models/schema.rb | 4 ++++
3 files changed, 16 insertions(+), 0 deletions(-)
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index 2e66694..d238135 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -166,6 +166,12 @@ class CIMI::Model::Base
def []=(a, v)
@attribute_values[a] = self.class.schema.convert(a, v)
end
+
+ # Prepare to serialize
+ def prepare
+ self.class.schema.collections.map { |coll| coll.name }.each do |n|
+ self[n].id = "#{self.id}/#{n}"
+ end
end
#
diff --git a/server/lib/cimi/models/collection.rb
b/server/lib/cimi/models/collection.rb
index cae5e69..f56efc5 100644
--- a/server/lib/cimi/models/collection.rb
+++ b/server/lib/cimi/models/collection.rb
@@ -27,12 +27,18 @@ module CIMI::Model
if values[:entries]
values[self.class.entry_name] = values.delete(:entries)
end
+ values[self.class.entry_name] ||= []
super(values)
end
def entries
self[self.class.entry_name]
end
+
+ # Prepare to serialize
+ def prepare
+ self.count = self.entries.size
+ self.count = nil if self.count == 0
end
def [](a)
diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index d8ba13b..a8f76f9 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -234,10 +234,12 @@ class CIMI::Model::Schema
end
def to_xml(model, xml)
+ model[name].prepare
xml[xml_name] = @collection_class.schema.to_xml(model[name])
end
def to_json(model, json)
+ model[name].prepare
json[json_name] = @collection_class.schema.to_json(model[name])
end
@@ -286,6 +288,7 @@ class CIMI::Model::Schema
def to_xml(model, xml = nil)
xml ||= OrderedHash.new
@attributes.freeze
+ model.prepare if model.respond_to?(:prepare)
@attributes.each { |attr| attr.to_xml(model, xml) }
xml
end
@@ -301,6 +304,7 @@ class CIMI::Model::Schema
def to_json(model, json = {})
@attributes.freeze
+ model.prepare if model.respond_to?(:prepare)
@attributes.each { |attr| attr.to_json(model, json) }
json
end
--
1.7.7.6