Class: Webbynode::Option
Direct Known Subclasses
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #array? ⇒ Boolean
- #default_value ⇒ Object
- #in(allowed_values) ⇒ Object
- #in_error(allowed_values) ⇒ Object
-
#initialize(*args) ⇒ Option
constructor
A new instance of Option.
- #integer(value) ⇒ Object
- #integer_error(value) ⇒ Object
- #parse(s) ⇒ Object
- #required? ⇒ Boolean
- #reset! ⇒ Object
- #take ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
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
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
3 4 5 |
# File 'lib/webbynode/option.rb', line 3 def desc @desc end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
3 4 5 |
# File 'lib/webbynode/option.rb', line 3 def errors @errors end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
3 4 5 |
# File 'lib/webbynode/option.rb', line 3 def kind @kind end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/webbynode/option.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/webbynode/option.rb', line 3 def @options end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'lib/webbynode/option.rb', line 4 def value @value end |
Class Method Details
Instance Method Details
#default_value ⇒ Object
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
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 |
#take ⇒ Object
91 92 93 |
# File 'lib/webbynode/option.rb', line 91 def take @options[:take] end |
#to_s ⇒ Object
95 96 97 |
# File 'lib/webbynode/option.rb', line 95 def to_s "--#{name.to_s}#{take ? "=#{take.to_s}" : ""}" end |
#valid? ⇒ 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 |