Module: Definition::Dsl::Comparators

Included in:
Definition
Defined in:
lib/definition/dsl/comparators.rb

Instance Method Summary collapse

Instance Method Details

#EmptyObject

Example: Empty



80
81
82
83
84
85
86
87
88
89
# File 'lib/definition/dsl/comparators.rb', line 80

def Empty # rubocop:disable Style/MethodName
  Types::Lambda.new(:empty) do |value|
    case value
    when String, Array, Hash
      conform_with(value) if value.empty?
    else
      value
    end
  end
end

#Equal(expected_value) ⇒ Object

Example: Equal(“value”)



72
73
74
75
76
# File 'lib/definition/dsl/comparators.rb', line 72

def Equal(expected_value) # rubocop:disable Style/MethodName
  Types::Lambda.new(:equal, context: { expected_value: expected_value }) do |value|
    conform_with(value) if value == expected_value
  end
end

#GreaterThen(min_value) ⇒ Object

Example: GreaterThen(5)



40
41
42
43
44
# File 'lib/definition/dsl/comparators.rb', line 40

def GreaterThen(min_value) # rubocop:disable Style/MethodName
  Types::Lambda.new("greater_then", context: { min_value: min_value }) do |value|
    conform_with(value) if value.is_a?(Numeric) && value > min_value
  end
end

#GreaterThenEqual(min_value) ⇒ Object

Example: GreaterThenEqual(5)



48
49
50
51
52
# File 'lib/definition/dsl/comparators.rb', line 48

def GreaterThenEqual(min_value) # rubocop:disable Style/MethodName
  Types::Lambda.new("greater_then_equal", context: { min_value: min_value }) do |value|
    conform_with(value) if value.is_a?(Numeric) && value >= min_value
  end
end

#LessThen(max_value) ⇒ Object

Example: LessThen(5)



56
57
58
59
60
# File 'lib/definition/dsl/comparators.rb', line 56

def LessThen(max_value) # rubocop:disable Style/MethodName
  Types::Lambda.new("less_then", context: { max_value: max_value }) do |value|
    conform_with(value) if value.is_a?(Numeric) && value < max_value
  end
end

#LessThenEqual(max_value) ⇒ Object

Example: LessThenEqual(5)



64
65
66
67
68
# File 'lib/definition/dsl/comparators.rb', line 64

def LessThenEqual(max_value) # rubocop:disable Style/MethodName
  Types::Lambda.new("less_then_equal", context: { max_value: max_value }) do |value|
    conform_with(value) if value.is_a?(Numeric) && value <= max_value
  end
end

#MaxSize(max_size) ⇒ Object

Example: MaxSize(5)



8
9
10
11
12
13
14
15
16
17
# File 'lib/definition/dsl/comparators.rb', line 8

def MaxSize(max_size) # rubocop:disable Style/MethodName
  Types::Lambda.new(:max_size, context: { max_size: max_size }) do |value|
    case value
    when String, Enumerable
      conform_with(value) if value.size <= max_size
    else
      value
    end
  end
end

#MinSize(min_size) ⇒ Object

Example: MinSize(5)



21
22
23
24
25
26
27
28
29
30
# File 'lib/definition/dsl/comparators.rb', line 21

def MinSize(min_size) # rubocop:disable Style/MethodName
  Types::Lambda.new(:min_size, context: { min_size: min_size }) do |value|
    case value
    when String, Enumerable
      conform_with(value) if value.size >= min_size
    else
      value
    end
  end
end

#NonEmptyObject

Example: NonEmpty



93
94
95
96
97
98
99
100
101
102
# File 'lib/definition/dsl/comparators.rb', line 93

def NonEmpty # rubocop:disable Style/MethodName
  Types::Lambda.new(:non_empty) do |value|
    case value
    when String, Array, Hash
      conform_with(value) unless value.empty?
    else
      value
    end
  end
end

#NonEmptyStringObject

Example: NonEmptyString



34
35
36
# File 'lib/definition/dsl/comparators.rb', line 34

def NonEmptyString # rubocop:disable Style/MethodName
  Types::And.new(:non_empty_string, Type(String), MinSize(1))
end

#Regex(regex, name: :regex) ⇒ Object

Example: Regex



106
107
108
109
110
111
112
113
114
115
# File 'lib/definition/dsl/comparators.rb', line 106

def Regex(regex, name: :regex) # rubocop:disable Style/MethodName
  Types::Lambda.new(name, context: { regex: regex.inspect }) do |value|
    case value
    when String
      conform_with(value) unless regex.match(value).nil?
    else
      value
    end
  end
end