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

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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.



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

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)



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

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.



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

def actions
  @actions
end

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#collectionObject (readonly)

Returns the value of attribute collection.



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

def collection
  @collection
end

Class Method Details

.subclass(name) ⇒ Object



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

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



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

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