Module: Sukima::Constraints

Defined in:
lib/sukima/constraints.rb

Class Method Summary collapse

Class Method Details

.format(pattern, value) ⇒ Object



14
15
16
# File 'lib/sukima/constraints.rb', line 14

def format(pattern, value)
  "should match #{pattern.source}" unless value.is_a?(String) && pattern.match?(value)
end

.in(range, value) ⇒ Object



10
11
12
# File 'lib/sukima/constraints.rb', line 10

def in(range, value)
  "should be in #{range}" unless range.include?(value)
end

.length(args, value) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/sukima/constraints.rb', line 18

def length(args, value)
  case args
  when Integer
    "should have length of #{args}" unless value.respond_to?(:length) && value.length == args
  when Range
    "should have length in #{args}" unless value.respond_to?(:length) && args.include?(value.length)
  end
end

.nonnil(_, value) ⇒ Object



27
28
29
# File 'lib/sukima/constraints.rb', line 27

def nonnil(_, value)
  'should not be nil' if value.nil?
end

.type(type, value) ⇒ Object



6
7
8
# File 'lib/sukima/constraints.rb', line 6

def type(type, value)
  "should be #{type}" unless value.is_a?(type)
end