Class: Arg
- Inherits:
-
Object
- Object
- Arg
- Defined in:
- lib/args.rb
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
-
#initialize(name, desc, validator) ⇒ Arg
constructor
A new instance of Arg.
-
#validate?(value, obj) ⇒ Boolean
return nil on success error message on failure.
Constructor Details
#initialize(name, desc, validator) ⇒ Arg
Returns a new instance of Arg.
79 80 81 82 83 |
# File 'lib/args.rb', line 79 def initialize(name, desc, validator) @name = name @desc = desc @validator = validator end |
Instance Attribute Details
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
77 78 79 |
# File 'lib/args.rb', line 77 def desc @desc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
77 78 79 |
# File 'lib/args.rb', line 77 def name @name end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
77 78 79 |
# File 'lib/args.rb', line 77 def validator @validator end |
Instance Method Details
#validate?(value, obj) ⇒ Boolean
return nil on success error message on failure
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/args.rb', line 88 def validate?(value, obj) vclass = @validator.class if vclass == Regexp test = value.full_match(@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}" elsif vclass == Hash test = @validator.has_key? value on_error = "Invalid choice for #{@name}, must be one of #{@validator.keys}" elsif vclass == String result = [ ] Route.new.send("get_available_" + @validator).each { |hash| result << hash["Name"] } @validator = result test = validate?(value, obj) if (test.class == String) on_error = test test = false end elsif vclass == Symbol if obj on_error, test = obj.send(@validator, value) end else test = false on_error = "unknown type of validator:#{@validator}, class=#{@validator.class}" end if test nil else on_error end end |