Class: Restio::Connector::Collection

Inherits:
Generic show all
Defined in:
lib/restio/connector/collection.rb

Instance Attribute Summary collapse

Attributes inherited from Generic

#type, #use_backend_attributes, #wrapped

Attributes included from Util::Callback

#callbacks

Attributes included from Util::Url

#base_url

Instance Method Summary collapse

Methods inherited from Generic

#cast, #caster, #caster=, #collection_name, #empty, #name, #processable, #resource_name, #unwrap, #wrap

Methods included from Util::Callback

#register_callback, #run_callbacks

Methods included from Util::Header

#add_header, #headers

Methods included from Util::Url

#url

Constructor Details

#initialize(klass, params = {}) ⇒ Collection

Returns a new instance of Collection.



8
9
10
11
12
# File 'lib/restio/connector/collection.rb', line 8

def initialize klass, params={}
  @klass      = klass
  @attributes = []
  @type       = :collection
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/restio/connector/collection.rb', line 7

def attributes
  @attributes
end

#identifierObject

Returns the value of attribute identifier.



6
7
8
# File 'lib/restio/connector/collection.rb', line 6

def identifier
  @identifier
end

Instance Method Details

#add_attribute(name) ⇒ Object



13
14
15
16
# File 'lib/restio/connector/collection.rb', line 13

def add_attribute name
  @attributes << name
  @klass.send(:attr_accessor,name)
end

#build_instance(response) ⇒ Object



17
18
19
20
# File 'lib/restio/connector/collection.rb', line 17

def build_instance response
  return unless processable response
  self.new_from_rest response.body
end

#build_instance_list(response) ⇒ Object



21
22
23
24
25
26
# File 'lib/restio/connector/collection.rb', line 21

def build_instance_list response
  return unless processable response
  unwrap(response.body).collect do |resource_attributes|
    @klass.new_from_rest resource_attributes
  end
end

#delete(id) ⇒ Object



43
44
45
46
47
48
# File 'lib/restio/connector/collection.rb', line 43

def delete id
  result = @klass.new
  result.send(result.rest.id,id)
  result.rest.delete
  result
end

#firstObject



27
28
29
# File 'lib/restio/connector/collection.rb', line 27

def first
  list.first
end

#lastObject



30
31
32
# File 'lib/restio/connector/collection.rb', line 30

def last
  list.last
end

#listObject



33
34
35
36
# File 'lib/restio/connector/collection.rb', line 33

def list
  build_instance_list Unirest.get url,
    headers: headers
end

#read(id) ⇒ Object



37
38
39
40
41
42
# File 'lib/restio/connector/collection.rb', line 37

def read id
  result = @klass.new
  result.send("#{identifier}=",id)
  result.rest.read
  result
end