Class: Arg

Inherits:
Object
  • Object
show all
Defined in:
lib/args.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, desc, validator) ⇒ Arg

Returns a new instance of Arg.



74
75
76
77
78
# File 'lib/args.rb', line 74

def initialize(name, desc, validator)
  @name = name
  @desc = desc
  @validator = validator
end

Instance Attribute Details

#desc ⇒ Object (readonly)

Returns the value of attribute desc.



72
73
74
# File 'lib/args.rb', line 72

def desc
  @desc
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



72
73
74
# File 'lib/args.rb', line 72

def name
  @name
end

#validator ⇒ Object (readonly)

Returns the value of attribute validator.



72
73
74
# File 'lib/args.rb', line 72

def validator
  @validator
end

Instance Method Details

#validate?(value, obj) ⇒ Boolean

return nil on success error message on failure

Returns:

  • (Boolean)


83
84
85
86
87
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
# File 'lib/args.rb', line 83

def validate?(value, obj)
  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}"
  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"]] = hash["Id"]
    }
    @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