Class: Vagrant::Hmurca::NodeGroupProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/hmurca/config_parser.rb

Overview

Public: Processor that handles node definition groups.

Constant Summary collapse

ARITY =
{
  'name'         => 1,
  'cpus'         => 1,
  'memory'       => 1,
  'forward-port' => 2,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_no, name = nil) ⇒ NodeGroupProcessor

Returns a new instance of NodeGroupProcessor.



14
15
16
17
18
19
20
# File 'lib/vagrant/hmurca/config_parser.rb', line 14

def initialize(line_no, name=nil)
  @name          = name
  @forward_ports = {}

  return unless name
  raise InvalidNodeNameError.new(name, line_no) unless valid_name?(name)
end

Instance Attribute Details

#cpusObject (readonly)

Returns the value of attribute cpus.



12
13
14
# File 'lib/vagrant/hmurca/config_parser.rb', line 12

def cpus
  @cpus
end

#forward_portsObject (readonly)

Returns the value of attribute forward_ports.



12
13
14
# File 'lib/vagrant/hmurca/config_parser.rb', line 12

def forward_ports
  @forward_ports
end

#memoryObject (readonly)

Returns the value of attribute memory.



12
13
14
# File 'lib/vagrant/hmurca/config_parser.rb', line 12

def memory
  @memory
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/vagrant/hmurca/config_parser.rb', line 12

def name
  @name
end

Instance Method Details

#parse_int(v, line_no) ⇒ Object

Internal: Parses given string value and converts it to integer if it’s possible. If given value is not a valid integer, it raises an error.

v - The String value to covnert. line_no - The Integer number of the current parsed line.

Returns converted int value. Raises InvalidIntegerValueError if value can’t be converted.



76
77
78
79
80
# File 'lib/vagrant/hmurca/config_parser.rb', line 76

def parse_int(v, line_no)
  v_int = v.to_i
  raise InvalidIntegerValueError.new(v, line_no) if v_int.to_s != v
  v_int
end

#process(line_no, cmd, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant/hmurca/config_parser.rb', line 22

def process(line_no, cmd, *args)
  case cmd
  when "name"
    n = args.first
    raise InvalidNodeNameError.new(n, line_no) unless valid_name?(n)
    @name = n
  when "cpus"
    @cpus = parse_int(args.first, line_no)
  when "memory"
    @memory = parse_int(args.first, line_no)
  when "forward-port"
    guest, host = parse_int(args[0], line_no), parse_int(args[1], line_no)

    if @forward_ports[host]
      raise NodeForwardPortDuplicatedError.new(host)
    end

    @forward_ports[host] = guest
  else
    return false
  end

  true
end

#valid_name?(name) ⇒ Boolean

Internal: Checks if given node name is valid.

name - The String node name to check.

Return true if node name is valid.

Returns:

  • (Boolean)


63
64
65
# File 'lib/vagrant/hmurca/config_parser.rb', line 63

def valid_name?(name)
  name =~ /^[\w\d\-]+$/
end

#validate!Object

Internal: Validates post-parsing constraints, like presence checking, etc.

Returns itself.



50
51
52
53
54
55
56
# File 'lib/vagrant/hmurca/config_parser.rb', line 50

def validate!
  raise NoNodeNameSpecifiedError unless self.name
  raise NoNodeCpusCountSpecifiedError.new(name) unless self.cpus
  raise NoNodeMemorySizeSpecifiedError.new(name) unless self.memory

  return self
end