Module: TMS::CollectionResource::InstanceMethods

Includes:
Base
Defined in:
lib/tms_client/collection_resource.rb

Instance Attribute Summary collapse

Attributes included from Base

#client, #errors, #href, #new_record

Instance Method Summary collapse

Methods included from Base

included

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



8
9
10
# File 'lib/tms_client/collection_resource.rb', line 8

def collection
  @collection
end

Instance Method Details

#build(attributes = nil) ⇒ Object



31
32
33
34
35
36
# File 'lib/tms_client/collection_resource.rb', line 31

def build(attributes=nil)
  thing = instance_class(self.class).new(client, self.href, attributes || {})
  thing.new_record = true
  self.collection << thing
  thing
end

#getObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/tms_client/collection_resource.rb', line 20

def get
  response = client.get(href)
  initialize_collection_from_items(response.body)
  #setup page links from header
  links = LinkHeader.parse(response.headers['link']).to_a.collect do |a|
    {a[1][0].last => a[0]}
  end
  parse_links(links)
  self
end

#initialize(client, href, items = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/tms_client/collection_resource.rb', line 10

def initialize(client, href, items=nil)
  super(client, href)
  if items
    initialize_collection_from_items(items)
  else
    self.collection = []
  end

end

#to_jsonObject



38
39
40
# File 'lib/tms_client/collection_resource.rb', line 38

def to_json
  @collection.map(&:to_json)
end

#to_sObject



42
43
44
# File 'lib/tms_client/collection_resource.rb', line 42

def to_s
  "<#{self.class.inspect} href=#{self.href} collection=#{self.collection.inspect}>"
end