Class: BlockScore::Collection

Inherits:
Array
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:



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

#parentBlockScore::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

Examples:

person.question_sets.parent # => person

Returns:



15
16
17
# File 'lib/blockscore/collection.rb', line 15

def parent
  @parent
end

Instance Method Details

#allself

Syntactic sugar method for returning collection

Examples:

all # returns collection

Returns:

  • (self)


39
40
41
# File 'lib/blockscore/collection.rb', line 39

def all
  self
end

#create(params = {}) ⇒ Object

Initialize a collection member and save it

Examples:

>> person.question_sets.create
=> #<BlockScore::QuestionSet:0x3fc67a6007f4 JSON:{
  "object": "question_set",
  "id": "55ef5d5b62386200030001b3",
  "created_at": 1441750363,
  ...
  }

Parameters:

  • params (Hash) (defaults to: {})

    params

Returns:

  • new saved instance of #member_class



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

Examples:

usage


>> person = person = BlockScore::Person.retrieve('55de4af7643735000300000f')
>> person.question_sets.new
=> #<BlockScore::QuestionSet:0x3fc67902f1b4 JSON:{
  "person_id": "55de4af7643735000300000f"
}>

Parameters:

  • params (Hash) (defaults to: {})

    initial params for member

Returns:

  • instance of #member_class



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_nameString

Name of parent resource

Examples:

self.parent_name # => 'person'

Returns:

  • (String)


93
94
95
# File 'lib/blockscore/collection.rb', line 93

def parent_name
  parent.class.resource
end

#refreshself

Relaod the contents of the collection

Examples:

usage

person.question_sets.refresh # => [#<BlockScore::QuestionSet...]

Returns:

  • (self)


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

Examples:

usage

person.question_sets.retrieve('55ef5b4e3532630003000178') # => instance of QuestionSet

Parameters:

  • id (String)

    resource id

Returns:

  • instance of #member_class if found

Raises:



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