Class: Opencdd::Validator::TypeRule

Inherits:
Rule
  • Object
show all
Defined in:
lib/opencdd/validator/type_rule.rb

Constant Summary collapse

SIMPLE_PREDICATES =
{
  "BOOLEAN_TYPE"              => ->(v) { %w[true false].include?(v.to_s.downcase.strip) },
  "STRING_TYPE"               => ->(v) { v.is_a?(String) || !v.nil? },
  "TRANSLATABLE_STRING_TYPE"  => ->(v) { v.is_a?(String) || structured_pairs?(v) },
  "IRDI_TYPE"                 => ->(v) { !Opencdd::IRDI.parse(v.to_s).nil? },
  "IRDI_STRING_TYPE"          => ->(v) { !Opencdd::IRDI.parse(v.to_s).nil? },
  "ICID_STRING"               => ->(v) { !Opencdd::IRDI.parse(v.to_s).nil? },
  "ICID_STRING_TYPE"          => ->(v) { !Opencdd::IRDI.parse(v.to_s).nil? },
  "DATE_TYPE"                 => ->(v) { date?(v) },
  "DATE_TIME_TYPE"            => ->(v) { date_time?(v) },
  "DATETIME_TYPE"             => ->(v) { date_time?(v) },
  "REAL_TYPE"                 => ->(v) { real?(v) },
  "INTEGER_TYPE"              => ->(v) { integer?(v) },
  "INT_TYPE"                  => ->(v) { integer?(v) },
  "RATIONAL_TYPE"             => ->(v) { rational?(v) },
}.freeze

Constants inherited from Rule

Rule::REFERENCE_VALUE_KINDS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rule

#blank?, #code_column?, #reference_kind?, #skip_blank?, #value_passes?

Class Method Details

.date?(value) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/opencdd/validator/type_rule.rb', line 49

def self.date?(value)
  !!begin
    Date.iso8601(value.to_s)
  rescue ArgumentError, TypeError
    nil
  end
end

.date_time?(value) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/opencdd/validator/type_rule.rb', line 57

def self.date_time?(value)
  !!begin
    Time.iso8601(value.to_s)
  rescue ArgumentError, TypeError
    nil
  end
end

.integer?(value) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/opencdd/validator/type_rule.rb', line 73

def self.integer?(value)
  !!begin
    Integer(value.to_s)
  rescue ArgumentError, TypeError
    nil
  end
end

.rational?(value) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/opencdd/validator/type_rule.rb', line 81

def self.rational?(value)
  !!begin
    Rational(value.to_s)
  rescue ArgumentError, TypeError, ZeroDivisionError
    nil
  end
end

.real?(value) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/opencdd/validator/type_rule.rb', line 65

def self.real?(value)
  !!begin
    Float(value.to_s)
  rescue ArgumentError, TypeError
    nil
  end
end

.structured_pairs?(value) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/opencdd/validator/type_rule.rb', line 44

def self.structured_pairs?(value)
  s = value.to_s.strip
  s.start_with?("{") || s.start_with?("(")
end

Instance Method Details

#applies?(context) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/opencdd/validator/type_rule.rb', line 27

def applies?(context)
  !context.data_type.nil?
end

#call(value, context) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/opencdd/validator/type_rule.rb', line 31

def call(value, context)
  return true if value.nil? || value.to_s.strip.empty?
  token = context.data_type.to_s
  return enum_member?(value, context) if token.start_with?("ENUM_")
  predicate = SIMPLE_PREDICATES[token]
  return true if predicate.nil?
  predicate.call(value) ? true : false
end

#idObject



23
24
25
# File 'lib/opencdd/validator/type_rule.rb', line 23

def id
  "R03"
end

#message(value, context) ⇒ Object



40
41
42
# File 'lib/opencdd/validator/type_rule.rb', line 40

def message(value, context)
  "R03: value #{value.inspect} does not satisfy data type #{context.data_type}"
end