class BlockScore::Collection
Collection
is a proxy between the parent and the asssociated members where parent is some instance of a resource
Attributes
@!attribute [r] member_class
class which will be used for the embedded resources in the collection
@return [Class]
@api private
@!attribute [r] parent resource which owns a collection of other resources
@example
person.question_sets.parent # => person
@return [BlockScore::Base] a resource
@api private
Public Class Methods
Sets parent and member_class
then registers embedded ids
@param [BlockScore::Base] parent @param [Class] class of collection members
@return [undefined]
@api private
# File lib/blockscore/collection.rb, line 25 def initialize(parent, member_class) @parent = parent @member_class = member_class register_parent_data end
Public Instance Methods
Syntactic sugar method for returning collection
@example
all # returns collection
@return [self]
@api public
# File lib/blockscore/collection.rb, line 39 def all self end
Initialize a collection member and save it
@example
>> person.question_sets.create => #<BlockScore::QuestionSet:0x3fc67a6007f4 JSON:{ "object": "question_set", "id": "55ef5d5b62386200030001b3", "created_at": 1441750363, ... }
@param params [Hash] params
@return new saved instance of {member_class}
@api public
# 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
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
@example usage
>> person = person = BlockScore::Person.retrieve('55de4af7643735000300000f') >> person.question_sets.new => #<BlockScore::QuestionSet:0x3fc67902f1b4 JSON:{ "person_id": "55de4af7643735000300000f" }>
@param params [Hash] initial params for member
@return instance of {member_class}
@api public
# 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
Name of parent resource
@example
self.parent_name # => 'person'
@return [String]
@api semipublic
# File lib/blockscore/collection.rb, line 93 def parent_name parent.class.resource end
Relaod the contents of the collection
@example usage
person.question_sets.refresh # => [#<BlockScore::QuestionSet...]
@return [self]
@api public
# File lib/blockscore/collection.rb, line 79 def refresh clear register_parent_data self end
Retrieve a collection member by its id
@example usage
person.question_sets.retrieve('55ef5b4e3532630003000178') # => instance of QuestionSet
@param id [String] resource id
@return instance of {member_class} if found @raise [BlockScore::NotFoundError] otherwise
@api public
# 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
Protected Instance Methods
Default params for making an instance of {member_class}
@return [Hash]
@api private
# File lib/blockscore/collection.rb, line 163 def default_params { foriegn_key => parent.id } end
Private Instance Methods
Generate foriegn key name for parent resource
@return [Symbol] resource name as id
@api private
# File lib/blockscore/collection.rb, line 176 def foriegn_key :"#{parent_name}_id" end
ids that belong to the collection
@return [Array<String>]
@api private
# File lib/blockscore/collection.rb, line 235 def ids parent.attributes.fetch(:"#{Util.to_plural(member_class.resource)}", []) end
Initialize a new collection member
@param instance [BlockScore::Base] collection member instance @yield [Member] initialized member
@return [Member] new member
@api private
# File lib/blockscore/collection.rb, line 188 def new_member(instance, &blk) Member.new(parent, instance).tap(&blk) end
Check if ‘parent_id` is defined on `item`
@param item [BlockScore::Base] any resource
@return [Boolean]
@api private
# File lib/blockscore/collection.rb, line 199 def parent_id?(item) parent.id && item.send(foriegn_key) == parent.id end
Fetches embedded ids from parent and adds to self
@return [undefined]
@api private
# File lib/blockscore/collection.rb, line 223 def register_parent_data ids.each do |id| item = member_class.retrieve(id) self << item end end
Register a resource in collection
@param item [BlockScore::Base] a resource
@raise [BlockScore::Error] if no ‘parent_id` @return [BlockScore::Base] otherwise
@api private
# File lib/blockscore/collection.rb, line 211 def register_to_parent(item) fail Error, 'None belonging' unless parent_id?(item) ids << item.id self << item item end