Class: CIMI::Model::Collection
- Defined in:
- lib/cimi/models/collection.rb
Constant Summary
Constants inherited from Resource
Class Attribute Summary collapse
-
.embedded ⇒ Object
Returns the value of attribute embedded.
-
.entry_name ⇒ Object
Returns the value of attribute entry_name.
Attributes inherited from Resource
Class Method Summary collapse
Instance Method Summary collapse
- #[](a) ⇒ Object
- #[]=(a, v) ⇒ Object
- #entries ⇒ Object
- #filter_attributes(attr_list) ⇒ Object
-
#initialize(values = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#prepare ⇒ Object
Prepare to serialize.
Methods inherited from Resource
<<, add_attributes!, all_uri, base_schema, base_schema_cloned?, clone_base_schema, #filter_by, #filter_by_arr_index, #filter_by_arr_range, from_json, from_xml, inherited, parse, resource_uri, #to_json, to_json, to_xml, #to_xml
Methods included from Schema::DSL
#array, #collection, #hash, #href, #resource_attr, #scalar, #struct, #text
Constructor Details
#initialize(values = {}) ⇒ Collection
Returns a new instance of Collection.
26 27 28 29 30 31 32 |
# File 'lib/cimi/models/collection.rb', line 26 def initialize(values = {}) if values[:entries] values[self.class.entry_name] = values.delete(:entries) end values[self.class.entry_name] ||= [] super(values) end |
Class Attribute Details
.embedded ⇒ Object
Returns the value of attribute embedded.
20 21 22 |
# File 'lib/cimi/models/collection.rb', line 20 def @embedded end |
.entry_name ⇒ Object
Returns the value of attribute entry_name.
20 21 22 |
# File 'lib/cimi/models/collection.rb', line 20 def entry_name @entry_name end |
Class Method Details
.generate(model_class, opts = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cimi/models/collection.rb', line 76 def self.generate(model_class, opts = {}) model_name = model_class.name.split("::").last scope = opts[:scope] || CIMI::Model coll_class = Class.new(CIMI::Model::Collection) scope.const_set(:"#{model_name}Collection", coll_class) coll_class.entry_name = model_name.underscore.pluralize.to_sym coll_class. = opts[:embedded] entry_schema = model_class.schema coll_class.instance_eval do text :id scalar :href text :count scalar :href if opts[:embedded] array self.entry_name, :schema => entry_schema, :xml_name => model_name array :operations do scalar :rel, :href end end coll_class end |
.xml_tag_name ⇒ Object
72 73 74 |
# File 'lib/cimi/models/collection.rb', line 72 def self.xml_tag_name "Collection" end |
Instance Method Details
#[](a) ⇒ Object
55 56 57 58 |
# File 'lib/cimi/models/collection.rb', line 55 def [](a) a = entry_name if a == :entries super(a) end |
#[]=(a, v) ⇒ Object
60 61 62 63 |
# File 'lib/cimi/models/collection.rb', line 60 def []=(a, v) a = entry_name if a == :entries super(a, v) end |
#entries ⇒ Object
34 35 36 |
# File 'lib/cimi/models/collection.rb', line 34 def entries self[self.class.entry_name] end |
#filter_attributes(attr_list) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/cimi/models/collection.rb', line 65 def filter_attributes(attr_list) self[self.class.entry_name] = entries.map do |e| e.filter_attributes(attr_list) end self end |
#prepare ⇒ Object
Prepare to serialize
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/cimi/models/collection.rb', line 39 def prepare self.count = self.entries.size if self.class. ["id", "href"].each { |a| self[a] = nil if self[a] == "" } # Handle href and id, which are really just aliases of one another unless self.href || self.id raise "Collection #{self.class.name} must have one of id and href set" end if self.href && self.id && self.href != self.id raise "id and href must be identical for collection #{self.class.name}, id = #{id.inspect}, href = #{href.inspect}" end self.href ||= self.id self.id ||= self.href end end |