Class: ManageIQ::API::Client::Collection

Inherits:
Object
  • Object
show all
Includes:
ActionMixin, CustomInspectMixin, Enumerable, QueryRelation::Queryable
Defined in:
lib/manageiq/api/client/collection.rb

Constant Summary collapse

CUSTOM_INSPECT_EXCLUSIONS =
[:@client].freeze
ACTIONS_RETURNING_RESOURCES =
%w(create query).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(client, collection_spec) ⇒ Collection

Returns a new instance of Collection.



21
22
23
24
25
26
# File 'lib/manageiq/api/client/collection.rb', line 21

def initialize(client, collection_spec)
  raise "Cannot instantiate a Collection directly" if instance_of?(Collection)
  @client = client
  @name, @href, @description = collection_spec.values_at("name", "href", "description")
  clear_actions
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



97
98
99
100
101
102
103
104
# File 'lib/manageiq/api/client/collection.rb', line 97

def method_missing(sym, *args, &block)
  query_actions unless actions_present?
  if action_defined?(sym)
    exec_action(sym, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



19
20
21
# File 'lib/manageiq/api/client/collection.rb', line 19

def actions
  @actions
end

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/manageiq/api/client/collection.rb', line 14

def client
  @client
end

#descriptionObject (readonly)

Returns the value of attribute description.



18
19
20
# File 'lib/manageiq/api/client/collection.rb', line 18

def description
  @description
end

#hrefObject (readonly)

Returns the value of attribute href.



17
18
19
# File 'lib/manageiq/api/client/collection.rb', line 17

def href
  @href
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/manageiq/api/client/collection.rb', line 16

def name
  @name
end

Class Method Details

.subclass(name) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/manageiq/api/client/collection.rb', line 59

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

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

Instance Method Details

#each(&block) ⇒ Object



28
29
30
# File 'lib/manageiq/api/client/collection.rb', line 28

def each(&block)
  all.each(&block)
end

#find(*args) ⇒ Object

find(#) returns the object find() returns an array of the object find(#, #, …) or find([#, #, …]) returns an array of the objects



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/manageiq/api/client/collection.rb', line 35

def find(*args)
  request_array = args.size == 1 && args[0].kind_of?(Array)
  args = args.flatten
  case args.size
  when 0
    raise "Couldn't find resource without an 'id'"
  when 1
    res = limit(1).where(:id => args[0]).to_a
    raise ManageIQ::API::Client::ResourceNotFound, "Couldn't find resource with 'id' #{args}" if res.blank?
    request_array ? res : res.first
  else
    raise "Multiple resource find is not supported" unless respond_to?(:query)
    query(args.collect { |id| { "id" => id } })
  end
end

#find_by(args) ⇒ Object



51
52
53
# File 'lib/manageiq/api/client/collection.rb', line 51

def find_by(args)
  limit(1).where(args).first
end

#get(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/manageiq/api/client/collection.rb', line 69

def get(options = {})
  options[:expand] = (String(options[:expand]).split(",") | %w(resources)).join(",")
  options[:filter] = Array(options[:filter]) if options[:filter].is_a?(String)
  result_hash = client.get(name, options)
  fetch_actions(result_hash)
  klass = ManageIQ::API::Client::Resource.subclass(name)
  result_hash["resources"].collect do |resource_hash|
    klass.new(self, resource_hash)
  end
end

#optionsObject



91
92
93
# File 'lib/manageiq/api/client/collection.rb', line 91

def options
  @collection_options ||= CollectionOptions.new(client.options(name))
end

#pluck(*attrs) ⇒ Object



55
56
57
# File 'lib/manageiq/api/client/collection.rb', line 55

def pluck(*attrs)
  select(*attrs).to_a.pluck(*attrs)
end

#search(mode, options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/manageiq/api/client/collection.rb', line 80

def search(mode, options)
  options[:limit] = 1 if mode == :first
  result = get(parameters_from_query_relation(options))
  case mode
  when :first then result.first
  when :last  then result.last
  when :all   then result
  else raise "Invalid mode #{mode} specified for search"
  end
end