Class: ManageIQ::API::Client::Resource

Inherits:
Object
  • Object
show all
Includes:
ActionMixin, CustomInspectMixin
Defined in:
lib/manageiq/api/client/resource.rb

Constant Summary collapse

CUSTOM_INSPECT_EXCLUSIONS =
[:@collection].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CustomInspectMixin

#inspect, #pretty_print, #pretty_print_instance_variables

Constructor Details

#initialize(collection, resource_hash) ⇒ Resource

Returns a new instance of Resource.



26
27
28
29
30
31
32
# File 'lib/manageiq/api/client/resource.rb', line 26

def initialize(collection, resource_hash)
  raise "Cannot instantiate a Resource directly" if instance_of?(Resource)
  @collection = collection
  @attributes = resource_hash.except("actions")
  add_href
  fetch_actions(resource_hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



41
42
43
44
45
46
47
48
49
50
# File 'lib/manageiq/api/client/resource.rb', line 41

def method_missing(sym, *args, &block)
  reload_actions unless actions_present?
  if attributes.key?(sym.to_s)
    attributes[sym.to_s]
  elsif action_defined?(sym)
    exec_action(sym, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



22
23
24
# File 'lib/manageiq/api/client/resource.rb', line 22

def actions
  @actions
end

#attributesObject (readonly)

Returns the value of attribute attributes.



20
21
22
# File 'lib/manageiq/api/client/resource.rb', line 20

def attributes
  @attributes
end

#collectionObject (readonly)

Returns the value of attribute collection.



21
22
23
# File 'lib/manageiq/api/client/resource.rb', line 21

def collection
  @collection
end

Class Method Details

.subclass(name) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/manageiq/api/client/resource.rb', line 10

def self.subclass(name)
  name = name.classify

  if const_defined?(name, false)
    const_get(name, false)
  else
    const_set(name, Class.new(self))
  end
end

Instance Method Details

#[](attr) ⇒ Object



34
35
36
37
# File 'lib/manageiq/api/client/resource.rb', line 34

def [](attr)
  attr_str = attr.to_s
  attributes[attr_str] if attributes.key?(attr_str)
end