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
-
#initialize(values = {}) ⇒ Collection
constructor
You can initialize collection by passing the Hash representation of the collection or passing another Collection object.
-
#prepare ⇒ Object
Prepare to serialize.
- #select_attributes(attr_list) ⇒ Object
Methods inherited from Resource
add_attributes!, all_uri, #base_id, base_schema, base_schema_cloned?, clone_base_schema, 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
Methods included from Helpers::FilterResourceMethods
#filter_by, #parse_filter_opts
Methods included from Helpers::SelectResourceMethods
#select_by, #select_by_arr_index, #select_by_arr_range
Constructor Details
#initialize(values = {}) ⇒ Collection
You can initialize collection by passing the Hash representation of the collection or passing another Collection object.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cimi/models/collection.rb', line 56 def initialize(values = {}) if values.kind_of?(Hash) if values[:entries] values[self.class.entry_name] = values.delete(:entries) end values[self.class.entry_name] ||= [] super(values) else super end end |
Class Attribute Details
.embedded ⇒ Object
Returns the value of attribute embedded.
21 22 23 |
# File 'lib/cimi/models/collection.rb', line 21 def @embedded end |
.entry_name ⇒ Object
Returns the value of attribute entry_name.
21 22 23 |
# File 'lib/cimi/models/collection.rb', line 21 def entry_name @entry_name end |
Class Method Details
.generate(model_class, opts = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cimi/models/collection.rb', line 27 def 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
23 24 25 |
# File 'lib/cimi/models/collection.rb', line 23 def xml_tag_name 'Collection' end |
Instance Method Details
#[](a) ⇒ Object
89 90 91 92 |
# File 'lib/cimi/models/collection.rb', line 89 def [](a) a = self.class.entry_name if a == :entries super(a) end |
#[]=(a, v) ⇒ Object
94 95 96 97 |
# File 'lib/cimi/models/collection.rb', line 94 def []=(a, v) a = self.class.entry_name if a == :entries super(a, v) end |
#entries ⇒ Object
68 69 70 |
# File 'lib/cimi/models/collection.rb', line 68 def entries self[self.class.entry_name] end |
#prepare ⇒ Object
Prepare to serialize
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cimi/models/collection.rb', line 73 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 |
#select_attributes(attr_list) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/cimi/models/collection.rb', line 99 def select_attributes(attr_list) self[self.class.entry_name] = entries.map do |e| e.select_attributes(attr_list) end self end |