Class: HP::Cloud::FogCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/hpcloud/fog_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, article = 'a') ⇒ FogCollection

Returns a new instance of FogCollection.



30
31
32
33
34
# File 'lib/hpcloud/fog_collection.rb', line 30

def initialize(name, article='a')
  @name = name
  @article = article
  @connection = Connection.instance
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



28
29
30
# File 'lib/hpcloud/fog_collection.rb', line 28

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/hpcloud/fog_collection.rb', line 28

def name
  @name
end

Instance Method Details

#create(item = nil) ⇒ Object



110
111
112
# File 'lib/hpcloud/fog_collection.rb', line 110

def create(item = nil)
  return item
end

#empty?Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/hpcloud/fog_collection.rb', line 105

def empty?
  return true if @items.nil?
  return @items.empty?
end

#filter(arguments = [], multimatch = true) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hpcloud/fog_collection.rb', line 40

def filter(arguments = [], multimatch=true)
  if @ray.nil?
    @ray = []
    @items.each { |x| @ray << create(x) }
  end
  if (arguments.empty?)
    return @ray
  end
  retray = []
  arguments.each { |arg|
    found = []
    @ray.each { |item|
      if matches(arg, item)
        if (multimatch == true)
          found << item
        else
          if found.length == 0
            found << item
          else
            if found[0].respond_to?(:set_error)
              found[0].set_error("More than one #{@name} matches '#{arg}', use the id instead of name.")
            else
              raise HP::Cloud::Exceptions::General.new("More than one #{@name} matches '#{arg}', use the id instead of name.")
            end
          end
        end
      end
    }
    if found.length == 0
      item = create()
      if item.nil?
        raise HP::Cloud::Exceptions::NotFound.new("Cannot find #{@article} #{@name} matching '#{arg}'.")
      end
      if item.respond_to?(:name)
        item.name = arg
      end
      item.set_error("Cannot find #{@article} #{@name} matching '#{arg}'.", :not_found)
      retray << item
    else
      retray += found
    end
  }
  return retray
end

#get(arguments = [], multimatch = true) ⇒ Object



85
86
87
88
89
90
# File 'lib/hpcloud/fog_collection.rb', line 85

def get(arguments = [], multimatch=true)
  if arguments.kind_of?(Array)
    return filter(arguments, multimatch)
  end
  return filter([arguments], false).first
end

#get_array(arguments = []) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hpcloud/fog_collection.rb', line 92

def get_array(arguments = [])
  ray = []
  get(arguments, true).each { |x|
    if x.respond_to?(:to_hash)
      hsh = x.to_hash()
    else
      hsh = Hash[x.attributes.map{ |k, v| [k.to_s, v] }]
    end
    ray << hsh unless hsh.nil?
  }
  return ray
end

#matches(arg, item) ⇒ Object



36
37
38
# File 'lib/hpcloud/fog_collection.rb', line 36

def matches(arg, item)
  return ((arg == item.id.to_s) || (arg == item.name.to_s))
end

#unique(name) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/hpcloud/fog_collection.rb', line 114

def unique(name)
  begin
    get(name)
    raise HP::Cloud::Exceptions::General.new("A #{@name} with the name '#{name}' already exists")
  rescue HP::Cloud::Exceptions::NotFound => e
  end
  return nil
end