Module: Radiosonde::DSL::Validator
- Included in:
- Context, Context::Alarm
- Defined in:
- lib/radiosonde/dsl/validator.rb
Instance Method Summary collapse
- #_call_once(method_name) ⇒ Object
- #_expected_type(value, *types) ⇒ Object
- #_identify(errmsg) ⇒ Object
- #_required(name, value) ⇒ Object
- #_validate(errmsg) ⇒ Object
Instance Method Details
#_call_once(method_name) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/radiosonde/dsl/validator.rb', line 19 def _call_once(method_name) @called ||= [] if @called.include?(method_name) raise _identify("`#{method_name}` is already defined") end @called << method_name end |
#_expected_type(value, *types) ⇒ Object
29 30 31 32 33 |
# File 'lib/radiosonde/dsl/validator.rb', line 29 def _expected_type(value, *types) unless types.any? {|t| value.kind_of?(t) } raise _identify("Invalid type: #{value}") end end |
#_identify(errmsg) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/radiosonde/dsl/validator.rb', line 39 def _identify(errmsg) if @error_identifier errmsg = "#{@error_identifier}: #{errmsg}" end return errmsg end |
#_required(name, value) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/radiosonde/dsl/validator.rb', line 2 def _required(name, value) invalid = false if value case value when String invalid = value.strip.empty? when Array, Hash invalid = value.empty? end elsif value.nil? invalid = true end raise _identify("`#{name}` is required") if invalid end |
#_validate(errmsg) ⇒ Object
35 36 37 |
# File 'lib/radiosonde/dsl/validator.rb', line 35 def _validate(errmsg) raise _identify(errmsg) unless yield end |