Class: Webbynode::Option

Inherits:
Object show all
Defined in:
lib/webbynode/option.rb

Direct Known Subclasses

Parameter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Option

Returns a new instance of Option.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/webbynode/option.rb', line 10

def initialize(*args)
  raise "Cannot initialize Parameter without a name" unless args.first
  
  @errors = []
  @value = nil
  @options = args.pop if args.last.is_a?(Hash)
  @options ||= {}

  @original_options = @options.clone
  @options[:required] = false if @options[:required].nil?
  
  @name = args[0]
  if args[1].is_a?(String)
    @desc = args[1]
  else
    @kind = args[1]
    @desc = args[2]
  end
  
  @kind ||= String
  @value = [] if @kind == Array
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



3
4
5
# File 'lib/webbynode/option.rb', line 3

def desc
  @desc
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/webbynode/option.rb', line 3

def errors
  @errors
end

#kindObject (readonly)

Returns the value of attribute kind.



3
4
5
# File 'lib/webbynode/option.rb', line 3

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/webbynode/option.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/webbynode/option.rb', line 3

def options
  @options
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/webbynode/option.rb', line 4

def value
  @value
end

Class Method Details

.name_for(s) ⇒ Object



6
7
8
# File 'lib/webbynode/option.rb', line 6

def Option.name_for(s)
  return $1 if s =~ /^--([\w-]+)(=("[^"]+"|[\w]+))*/
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/webbynode/option.rb', line 75

def array?
  kind == Array
end

#default_valueObject



83
84
85
# File 'lib/webbynode/option.rb', line 83

def default_value
  array? ? [] : nil
end

#in(allowed_values) ⇒ Object



60
61
62
# File 'lib/webbynode/option.rb', line 60

def in(allowed_values)
  allowed_values.include?(self.value)
end

#in_error(allowed_values) ⇒ Object



64
65
66
67
# File 'lib/webbynode/option.rb', line 64

def in_error(allowed_values)
  opts = allowed_values.map { |v| "'#{v}'"}
  "Invalid value '#{value}' for #{self.class.name.split("::").last.downcase} '#{self.name}'. It should be one of #{opts.to_phrase("or")}."
end

#integer(value) ⇒ Object



52
53
54
# File 'lib/webbynode/option.rb', line 52

def integer(value)
  Integer(value) rescue false
end

#integer_error(value) ⇒ Object



56
57
58
# File 'lib/webbynode/option.rb', line 56

def integer_error(value)
  "Invalid value '#{value}' for #{self.class.name.split("::").last.downcase} '#{self.name}'. It should be an integer."
end

#parse(s) ⇒ Object



69
70
71
72
73
# File 'lib/webbynode/option.rb', line 69

def parse(s)
  if s =~ /^--(\w+)(=("[^"]+"|[\w\.]+))*/
    self.value = $3 ? $3.gsub(/"/, "") : true
  end
end

#required?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/webbynode/option.rb', line 87

def required?
  @options[:required] == true
end

#reset!Object



79
80
81
# File 'lib/webbynode/option.rb', line 79

def reset!
  self.value = default_value
end

#takeObject



91
92
93
# File 'lib/webbynode/option.rb', line 91

def take
  @options[:take]
end

#to_sObject



95
96
97
# File 'lib/webbynode/option.rb', line 95

def to_s
  "--#{name.to_s}#{take ? "=#{take.to_s}" : ""}"
end

#valid?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/webbynode/option.rb', line 37

def valid?
  return true if !required? and value.nil?
  @errors = []
  if (validations = @options[:validate])
    if validations.is_a?(Hash)
      validations.each_pair do |key, value|
        @errors << send("#{key}_error", value) unless send(key, value)
      end
    else
      @errors << send("#{validations}_error", value) unless send(validations, value)
    end
  end
  @errors.empty?
end

#validate!Object



33
34
35
# File 'lib/webbynode/option.rb', line 33

def validate!
  raise Webbynode::Command::InvalidCommand, errors.join("\n") unless valid?
end