Module: Such::Convention

Defined in:
lib/such/convention.rb

Defined Under Namespace

Modules: Refinements

Constant Summary collapse

CAPS =
/^[A-Z_]+$/
CAPS1 =
/^[A-Z_]+!$/
CAPS2 =
/^[A-Z_]+[?]$/
LOWER =
/^[a-z_]+$/
LOWER1 =
/^[a-z_]+!$/
LOWER2 =
/^[a-z_]+[?]$/
WORD =
/^\w+$/
WORD1 =
/^\w+!$/
WORD2 =
/^\w+[?]$/

Class Method Summary collapse

Class Method Details

.validate(config) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/such/convention.rb', line 86

def self.validate(config)
  raise 'expected hash' unless config.is_a?Hash
  errors = {}
  config.each do |k,v|
    begin
      validate_kv(k,v)
    rescue
      errors[k] = $!.message
    end
  end
  return errors.empty? ? nil : errors
end

.validate_kv(k, v) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/such/convention.rb', line 59

def self.validate_kv(k,v)
  raise   'unrecognized item symbol'    unless k.item_symbol?
  case k
  when LOWER  # abc
    raise 'unrecognized item hash'      unless v.item_hash? or
                                       (v.is_a?Symbol and LOWER.match?v)
  when CAPS   # ABC
    raise 'unrecognized item array'     unless v.item_array?
  when WORD   # Abc
    raise 'unrecognized item tangible'  unless v.item_tangible?
  when LOWER1 # abc!
    raise 'unrecognized bang array'     unless v.item_bang_array?
  when CAPS1  # ABC!
    raise 'unrecognized explicit array' unless v.item_explicit_array?
  when WORD1  # Abc!
    raise 'unrecognized items'          unless v.items?
  when LOWER2 # abc?
    raise 'unrecognized boolean'        unless v.item_boolean?
  when CAPS2  # ABC?
    raise 'unrecognized boolean|nil'    unless v.nil? or v.item_boolean?
  when WORD2  # Abc?
    raise 'unrecognized item value|nil' unless v.nil? or v.item_tangible?
  else
    raise 'should not happen'
  end
end