Class: Restio::Connector::Resource

Inherits:
Generic show all
Includes:
Util::Identifier
Defined in:
lib/restio/connector/resource.rb

Instance Attribute Summary

Attributes included from Util::Identifier

#rest_identifier

Attributes inherited from Generic

#type, #use_backend_attributes

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(instance, params = {}) ⇒ Resource

Returns a new instance of Resource.



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

def initialize instance, params={}
  @instance = instance
  @type     = :resource
  @klass    = instance.class
end

Instance Method Details

#attributesObject



16
17
18
# File 'lib/restio/connector/resource.rb', line 16

def attributes
  @klass.rest.attributes
end

#createObject



53
54
55
56
57
58
59
# File 'lib/restio/connector/resource.rb', line 53

def create
  run_callbacks :before, __method__
  update_instance Unirest.post @klass.rest.url,
    headers: headers,
    parameters: parameters
  run_callbacks :after, __method__
end

#deleteObject



73
74
75
76
77
78
# File 'lib/restio/connector/resource.rb', line 73

def delete
  run_callbacks :before, __method__
  delete_instance Unirest.delete url,
    headers: headers
  run_callbacks :after, __method__
end

#delete_instance(response) ⇒ Object



49
50
51
52
# File 'lib/restio/connector/resource.rb', line 49

def delete_instance response
  return true if processable response
  false
end

#identifierObject



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

def identifier
  @instance.send @klass.rest.identifier
end

#parametersObject



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

def parameters
  result = attributes.collect do |name|
    [name,value_of(name)]
  end.to_h
  wrap result
end

#readObject



60
61
62
63
64
65
# File 'lib/restio/connector/resource.rb', line 60

def read
  run_callbacks :before, __method__
  update_instance Unirest.get url,
    headers: headers
  run_callbacks :after, __method__
end

#register_attribute(name) ⇒ Object



25
26
27
28
29
# File 'lib/restio/connector/resource.rb', line 25

def register_attribute name
  return false unless @klass.rest.use_backend_attributes
  @klass.rest.add_attribute name.to_sym
  true
end

#set_value(name, value) ⇒ Object



30
31
32
33
34
35
# File 'lib/restio/connector/resource.rb', line 30

def set_value name, value
  value = cast(value)
  register_attribute(name) ?
  @instance.send("#{name}=", value) :
  @instance.instance_variable_set("@#{name}", value)
end

#updateObject



66
67
68
69
70
71
72
# File 'lib/restio/connector/resource.rb', line 66

def update
  run_callbacks :before, __method__
  update_instance Unirest.put url,
    headers: headers,
    parameters: parameters
  run_callbacks :after, __method__
end

#update_instance(response) ⇒ Object



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

def update_instance response
  return unless processable response
  unwrap(response.body).each do |name,value|
    set_value name, value
  end unless empty response
  @instance
end

#value_of(name) ⇒ Object



22
23
24
# File 'lib/restio/connector/resource.rb', line 22

def value_of name
  @instance.instance_variable_get "@#{name}"
end

#with_attributes(&block) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/restio/connector/resource.rb', line 79

def with_attributes &block
  run_callbacks :before, __method__
  @klass.rest.attributes.each do |name|
    yield(name,@instance.send(name))
  end
  run_callbacks :after, __method__
end

#wrappedObject



19
20
21
# File 'lib/restio/connector/resource.rb', line 19

def wrapped
  @klass.rest.wrapped
end