Class: HaveAPI::Client::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/client/resource.rb

Overview

An API resource.

Direct Known Subclasses

ResourceInstance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, api, name) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
12
13
14
# File 'lib/haveapi/client/resource.rb', line 7

def initialize(client, api, name)
  @client = client
  @api = api
  @name = name
  @prepared_args = []
  @actions = {}
  @resources = {}
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



4
5
6
# File 'lib/haveapi/client/resource.rb', line 4

def actions
  @actions
end

#prepared_argsObject

Returns the value of attribute prepared_args.



5
6
7
# File 'lib/haveapi/client/resource.rb', line 5

def prepared_args
  @prepared_args
end

#resourcesObject (readonly)

Returns the value of attribute resources.



4
5
6
# File 'lib/haveapi/client/resource.rb', line 4

def resources
  @resources
end

Instance Method Details

#_descriptionObject

Return resource description. Method is prefixed with an underscore to prevent name collision with ResourceInstance attributes.



69
70
71
# File 'lib/haveapi/client/resource.rb', line 69

def _description
  @description
end

#_nameObject

Return resource name. Method is prefixed with an underscore to prevent name collision with ResourceInstance attributes.



62
63
64
# File 'lib/haveapi/client/resource.rb', line 62

def _name
  @name
end

#inspectObject



16
17
18
# File 'lib/haveapi/client/resource.rb', line 16

def inspect
  "#<#{self.class.name} @name=#{@name} @prepared_args=#{@prepared_args} @actions=#{@actions.keys.inspect} @resources=#{@resources.keys.inspect}>"
end

#newObject

Create a new instance of a resource. The created instance is not persistent until ResourceInstance#save is called.



55
56
57
# File 'lib/haveapi/client/resource.rb', line 55

def new
  ResourceInstance.new(@client, @api, self, action: @actions[:create], persistent: false)
end

#setup(description) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/haveapi/client/resource.rb', line 20

def setup(description)
  @description = description

  description[:actions].each do |name, desc|
    action = HaveAPI::Client::Action.new(@client, @api, name, desc, [])
    define_action(action)
    @actions[name] = action
  end

  description[:resources].each do |name, desc|
    r = HaveAPI::Client::Resource.new(@client, @api, name)
    r.setup(desc)
    define_resource(r)
    @resources[name] = r
  end
end

#setup_from_clone(original) ⇒ Object

Copy actions and resources from the original resource and create methods for this instance.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/haveapi/client/resource.rb', line 39

def setup_from_clone(original)
  @description = original._description

  original.actions.each do |name, action|
    define_action(action)
    @actions[name] = action
  end

  original.resources.each do |name, resource|
    define_resource(resource)
    @resources[name] = resource
  end
end