Module: Gorillib::Model::Validate

Defined in:
lib/gorillib/model/validate.rb

Constant Summary collapse

VALID_NAME_RE =
/\A[A-Za-z_][A-Za-z0-9_]*\z/

Class Method Summary collapse

Class Method Details

.hashlike!(val) ⇒ Object

Raises:



12
13
14
15
# File 'lib/gorillib/model/validate.rb', line 12

def hashlike!(val)
  return true if val.respond_to?(:[]) && val.respond_to?(:has_key?)
  raise ArgumentError, "#{block_given? ? yield : 'value'} should be something that behaves like a hash: #{val.inspect}", caller
end

.identifier!(name) ⇒ Object

Raises:

  • (TypeError)


7
8
9
10
# File 'lib/gorillib/model/validate.rb', line 7

def identifier!(name)
  raise TypeError,     "can't convert #{name.class} into Symbol", caller unless name.respond_to? :to_sym
  raise ArgumentError, "Name must start with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]", caller unless name =~ VALID_NAME_RE
end

.included_in!(desc, val, colxn) ⇒ Object

Raises:



17
18
19
# File 'lib/gorillib/model/validate.rb', line 17

def included_in!(desc, val, colxn)
  raise ArgumentError, "#{desc} must be one of #{colxn.inspect}: got #{val.inspect}", caller unless colxn.include?(val)
end