Module: Definition::Dsl::Comparators
- Included in:
- Definition
- Defined in:
- lib/definition/dsl/comparators.rb
Instance Method Summary collapse
-
#Empty ⇒ Object
Example: Empty.
-
#Equal(expected_value) ⇒ Object
Example: Equal(“value”).
-
#GreaterThan(min_value) ⇒ Object
Example: GreaterThan(5).
-
#GreaterThanEqual(min_value) ⇒ Object
Example: GreaterThanEqual(5).
-
#LessThan(max_value) ⇒ Object
Example: LessThan(5).
-
#LessThanEqual(max_value) ⇒ Object
Example: LessThanEqual(5).
-
#MaxSize(max_size) ⇒ Object
Example: MaxSize(5).
-
#MinSize(min_size) ⇒ Object
Example: MinSize(5).
-
#NonEmpty ⇒ Object
Example: NonEmpty.
-
#NonEmptyString ⇒ Object
Example: NonEmptyString.
-
#Regex(regex, name: :regex) ⇒ Object
Example: Regex.
Instance Method Details
#Empty ⇒ Object
Example: Empty
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/definition/dsl/comparators.rb', line 80 def Empty # rubocop:disable Naming/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 Naming/MethodName Types::Lambda.new(:equal, context: { expected_value: expected_value }) do |value| conform_with(value) if value == expected_value end end |
#GreaterThan(min_value) ⇒ Object
Example: GreaterThan(5)
40 41 42 43 44 |
# File 'lib/definition/dsl/comparators.rb', line 40 def GreaterThan(min_value) # rubocop:disable Naming/MethodName Types::Lambda.new("greater_than", context: { min_value: min_value }) do |value| conform_with(value) if value.is_a?(Numeric) && value > min_value end end |
#GreaterThanEqual(min_value) ⇒ Object
Example: GreaterThanEqual(5)
48 49 50 51 52 |
# File 'lib/definition/dsl/comparators.rb', line 48 def GreaterThanEqual(min_value) # rubocop:disable Naming/MethodName Types::Lambda.new("greater_than_equal", context: { min_value: min_value }) do |value| conform_with(value) if value.is_a?(Numeric) && value >= min_value end end |
#LessThan(max_value) ⇒ Object
Example: LessThan(5)
56 57 58 59 60 |
# File 'lib/definition/dsl/comparators.rb', line 56 def LessThan(max_value) # rubocop:disable Naming/MethodName Types::Lambda.new("less_than", context: { max_value: max_value }) do |value| conform_with(value) if value.is_a?(Numeric) && value < max_value end end |
#LessThanEqual(max_value) ⇒ Object
Example: LessThanEqual(5)
64 65 66 67 68 |
# File 'lib/definition/dsl/comparators.rb', line 64 def LessThanEqual(max_value) # rubocop:disable Naming/MethodName Types::Lambda.new("less_than_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 Naming/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 Naming/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 |
#NonEmpty ⇒ Object
Example: NonEmpty
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/definition/dsl/comparators.rb', line 93 def NonEmpty # rubocop:disable Naming/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 |
#NonEmptyString ⇒ Object
Example: NonEmptyString
34 35 36 |
# File 'lib/definition/dsl/comparators.rb', line 34 def NonEmptyString # rubocop:disable Naming/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 Naming/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 |