Class: Arg
- Inherits:
-
Object
- Object
- Arg
- Defined in:
- lib/args.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#desc ⇒ Object
Returns the value of attribute desc.
-
#name ⇒ Object
Returns the value of attribute name.
-
#next_node ⇒ Object
Returns the value of attribute next_node.
-
#required ⇒ Object
Returns the value of attribute required.
-
#validator ⇒ Object
Returns the value of attribute validator.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(name, hash = { }) ⇒ Arg
constructor
A new instance of Arg.
-
#validate?(value) ⇒ Boolean
return true on success error message on failure.
Constructor Details
#initialize(name, hash = { }) ⇒ Arg
Returns a new instance of Arg.
6 7 8 9 |
# File 'lib/args.rb', line 6 def initialize(name, hash={ }) @name = name hash.each {|k,v| instance_variable_set("@#{k}",v)} end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
4 5 6 |
# File 'lib/args.rb', line 4 def default @default end |
#desc ⇒ Object
Returns the value of attribute desc.
4 5 6 |
# File 'lib/args.rb', line 4 def desc @desc end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/args.rb', line 4 def name @name end |
#next_node ⇒ Object
Returns the value of attribute next_node.
4 5 6 |
# File 'lib/args.rb', line 4 def next_node @next_node end |
#required ⇒ Object
Returns the value of attribute required.
4 5 6 |
# File 'lib/args.rb', line 4 def required @required end |
#validator ⇒ Object
Returns the value of attribute validator.
4 5 6 |
# File 'lib/args.rb', line 4 def validator @validator end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'lib/args.rb', line 4 def value @value end |
Instance Method Details
#validate?(value) ⇒ Boolean
return true on success error message on failure
14 15 16 17 18 19 20 21 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/args.rb', line 14 def validate?(value) vclass = @validator.class if vclass == Regexp test = (value =~ @validator) on_error = "Invalid Format for " + @name elsif vclass == Array test = @validator.include? value on_error = "Invalid choice for " + @name + ", must be one of " + @validator.to_s elsif vclass == Hash test = @validator.has_key? value on_error = "Invalid choice for " + @name + ", must be one of " + @validator.keys.to_s elsif vclass == String result = {} Route.new.send("get_available_" + @validator).each { |hash| result[hash["Name"]] = hash["Id"] } @validator = result test = validate? value if (test.class == String) on_error = test test = false end else test = false on_error = "unknown type of validator:" + @validator.to_s + ", class=" + @validator.class.to_s end if test true else on_error end end |