Class: BlockScore::Collection
- Inherits:
-
Array
- Object
- Array
- BlockScore::Collection
- Defined in:
- lib/blockscore/collection.rb,
lib/blockscore/collection/member.rb
Overview
Collection is a proxy between the parent and the asssociated members where parent is some instance of a resource
Defined Under Namespace
Classes: Member
Instance Attribute Summary collapse
-
#parent ⇒ BlockScore::Base
readonly
private
resource which owns a collection of other resources.
Instance Method Summary collapse
-
#all ⇒ self
Syntactic sugar method for returning collection.
-
#create(params = {}) ⇒ Object
Initialize a collection member and save it.
-
#initialize(parent, member_class) ⇒ undefined
constructor
private
Sets parent and member_class then registers embedded ids.
-
#new(params = {}) ⇒ Object
Initializes new #member_class with ‘params`.
-
#parent_name ⇒ String
Name of parent resource.
-
#refresh ⇒ self
Relaod the contents of the collection.
-
#retrieve(id) ⇒ Object
Retrieve a collection member by its id.
Constructor Details
#initialize(parent, member_class) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets parent and member_class then registers embedded ids
25 26 27 28 29 |
# File 'lib/blockscore/collection.rb', line 25 def initialize(parent, member_class) @parent = parent @member_class = member_class register_parent_data end |
Instance Attribute Details
#parent ⇒ BlockScore::Base (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
resource which owns a collection of other resources
15 16 17 |
# File 'lib/blockscore/collection.rb', line 15 def parent @parent end |
Instance Method Details
#all ⇒ self
Syntactic sugar method for returning collection
39 40 41 |
# File 'lib/blockscore/collection.rb', line 39 def all self end |
#create(params = {}) ⇒ Object
Initialize a collection member and save it
113 114 115 116 117 118 119 120 121 |
# File 'lib/blockscore/collection.rb', line 113 def create(params = {}) fail Error, 'Create parent first' unless parent.id assoc_params = default_params.merge(params) instance = member_class.create(assoc_params) new_member(instance) do |member| register_to_parent(member) end end |
#new(params = {}) ⇒ Object
Initializes new #member_class with ‘params`
-
Ensures a parent id is meged into ‘params` (see #default_params).
-
Defines method ‘#save` on new collection member
-
Adds new item to collection
62 63 64 65 66 67 68 69 |
# File 'lib/blockscore/collection.rb', line 62 def new(params = {}) attributes = params.merge(default_params) instance = member_class.new(attributes) new_member(instance) do |member| self << member end end |
#parent_name ⇒ String
Name of parent resource
93 94 95 |
# File 'lib/blockscore/collection.rb', line 93 def parent_name parent.class.resource end |
#refresh ⇒ self
Relaod the contents of the collection
79 80 81 82 83 |
# File 'lib/blockscore/collection.rb', line 79 def refresh clear register_parent_data self end |
#retrieve(id) ⇒ Object
Retrieve a collection member by its id
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/blockscore/collection.rb', line 134 def retrieve(id) each do |item| next unless item.id == id return item end instance = member_class.retrieve(id) new_member(instance) do |member| register_to_parent(member) end end |