class BlockScore::Collection

Collection is a proxy between the parent and the asssociated members where parent is some instance of a resource

Attributes

member_class[R]

@!attribute [r] member_class class which will be used for the embedded resources in the collection

@return [Class]

@api private

parent[R]

@!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

new(parent, member_class) click to toggle source

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

all() click to toggle source

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
create(params = {}) click to toggle source

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
new(params = {}) click to toggle source

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
parent_name() click to toggle source

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
refresh() click to toggle source

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(id) click to toggle source

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() click to toggle source

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

foriegn_key() click to toggle source

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() click to toggle source

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
new_member(instance, &blk) click to toggle source

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
parent_id?(item) click to toggle source

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
register_parent_data() click to toggle source

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_to_parent(item) click to toggle source

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