Class: Dopv::PersistentDisk::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/dopv/persistent_disk.rb

Constant Summary collapse

DISK_DESC_KEYS =
[:id, :name, :node, :pool, :size]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Entry

Returns a new instance of Entry.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dopv/persistent_disk.rb', line 12

def initialize(attrs)
  if attrs.is_a?(Hash) && attrs.keys.sort == DISK_DESC_KEYS
    @node = attrs[:node]
    @name = attrs[:name]
    @id   = attrs[:id]
    @pool = attrs[:pool]
    @size = attrs[:size].to_i
  else
    raise PersistentDiskError, "Invalid disk entry"
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/dopv/persistent_disk.rb', line 10

def id
  @id
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/dopv/persistent_disk.rb', line 10

def name
  @name
end

#nodeObject

Returns the value of attribute node.



10
11
12
# File 'lib/dopv/persistent_disk.rb', line 10

def node
  @node
end

#poolObject

Returns the value of attribute pool.



10
11
12
# File 'lib/dopv/persistent_disk.rb', line 10

def pool
  @pool
end

#sizeObject

Returns the value of attribute size.



10
11
12
# File 'lib/dopv/persistent_disk.rb', line 10

def size
  @size
end

Instance Method Details

#==(other) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/dopv/persistent_disk.rb', line 24

def ==(other)
  case other
  when Entry
    @node == other.node && @name == other.name && @id == other.id && @pool == other.pool
  when Hash
    @node == other[:node] && @name == other[:name] && @id == other[:id] && @pool == other[:pool]
  else
    false
  end
end

#to_hashObject



49
50
51
# File 'lib/dopv/persistent_disk.rb', line 49

def to_hash
  { :name => @name, :id => @id, :pool => @pool, :size => @size }
end

#to_sObject



45
46
47
# File 'lib/dopv/persistent_disk.rb', line 45

def to_s
  "Disk: #{@name}\n  Node: #{@node}\n  Id: #{@id}\n  Pool: #{@pool}\n  Size: #{@size}"
end

#update(attrs = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/dopv/persistent_disk.rb', line 35

def update(attrs={})
  raise PersistentDiskError, "Update attributes must be a hash" unless attrs.is_a?(Hash)
  @node = attrs[:node] if attrs[:node]
  @name = attrs[:name] if attrs[:name]
  @id   = attrs[:id]   if attrs[:id]
  @pool = attrs[:pool] if attrs[:pool]
  @size = attrs[:size].to_i if attrs[:size]
  self
end