Class: Philiprehberger::GuardClause::Guard

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/guard_clause/guard.rb

Overview

Guard object that performs validation checks on a value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, soft: false) ⇒ Guard

Returns a new instance of Guard.

Parameters:

  • value (Object)

    the value to guard

  • soft (Boolean) (defaults to: false)

    when true, collect errors instead of raising



9
10
11
12
13
# File 'lib/philiprehberger/guard_clause/guard.rb', line 9

def initialize(value, soft: false)
  @value = value
  @soft = soft
  @errors = []
end

Instance Attribute Details

#errorsArray<String> (readonly)

Returns collected errors (soft mode only).

Returns:

  • (Array<String>)

    collected errors (soft mode only)



19
20
21
# File 'lib/philiprehberger/guard_clause/guard.rb', line 19

def errors
  @errors
end

#valueObject (readonly)

Returns the guarded value.

Returns:

  • (Object)

    the guarded value



16
17
18
# File 'lib/philiprehberger/guard_clause/guard.rb', line 16

def value
  @value
end

Instance Method Details

#gte(n, message = nil) ⇒ Guard

Assert the value is greater than or equal to n

Parameters:

  • n (Numeric)

    the minimum value

  • message (String) (defaults to: nil)

    custom error message

Returns:

  • (Guard)

    self for chaining



64
65
66
67
68
69
# File 'lib/philiprehberger/guard_clause/guard.rb', line 64

def gte(n, message = nil)
  if @value.respond_to?(:>=) && !(@value >= n)
    handle_violation(message || "value must be greater than or equal to #{n}")
  end
  self
end

#lte(n, message = nil) ⇒ Guard

Assert the value is less than or equal to n

Parameters:

  • n (Numeric)

    the maximum value

  • message (String) (defaults to: nil)

    custom error message

Returns:

  • (Guard)

    self for chaining



76
77
78
79
80
81
# File 'lib/philiprehberger/guard_clause/guard.rb', line 76

def lte(n, message = nil)
  if @value.respond_to?(:<=) && !(@value <= n)
    handle_violation(message || "value must be less than or equal to #{n}")
  end
  self
end

#matches(regex, message = nil) ⇒ Guard

Assert the value matches a regex pattern

Parameters:

  • regex (Regexp)

    the pattern to match

  • message (String) (defaults to: nil)

    custom error message

Returns:

  • (Guard)

    self for chaining



88
89
90
91
92
93
# File 'lib/philiprehberger/guard_clause/guard.rb', line 88

def matches(regex, message = nil)
  unless regex.match?(@value.to_s)
    handle_violation(message || "value must match #{regex.inspect}")
  end
  self
end

#not_empty(message = nil) ⇒ Guard

Assert the value is not empty

Parameters:

  • message (String) (defaults to: nil)

    custom error message

Returns:

  • (Guard)

    self for chaining



41
42
43
44
45
46
# File 'lib/philiprehberger/guard_clause/guard.rb', line 41

def not_empty(message = nil)
  if @value.respond_to?(:empty?) && @value.empty?
    handle_violation(message || 'value must not be empty')
  end
  self
end

#not_equal(other, message = nil) ⇒ Guard

Assert the value is not equal to another value

Parameters:

  • other (Object)

    the value to compare against

  • message (String) (defaults to: nil)

    custom error message

Returns:

  • (Guard)

    self for chaining



112
113
114
115
116
117
# File 'lib/philiprehberger/guard_clause/guard.rb', line 112

def not_equal(other, message = nil)
  if @value == other
    handle_violation(message || "value must not be equal to #{other.inspect}")
  end
  self
end

#not_nil(message = nil) ⇒ Guard

Assert the value is not nil

Parameters:

  • message (String) (defaults to: nil)

    custom error message

Returns:

  • (Guard)

    self for chaining



30
31
32
33
34
35
# File 'lib/philiprehberger/guard_clause/guard.rb', line 30

def not_nil(message = nil)
  if @value.nil?
    handle_violation(message || 'value must not be nil')
  end
  self
end

#one_of(arr, message = nil) ⇒ Guard

Assert the value is one of the given options

Parameters:

  • arr (Array)

    the allowed values

  • message (String) (defaults to: nil)

    custom error message

Returns:

  • (Guard)

    self for chaining



100
101
102
103
104
105
# File 'lib/philiprehberger/guard_clause/guard.rb', line 100

def one_of(arr, message = nil)
  unless arr.include?(@value)
    handle_violation(message || "value must be one of #{arr.inspect}")
  end
  self
end

#positive(message = nil) ⇒ Guard

Assert the value is positive

Parameters:

  • message (String) (defaults to: nil)

    custom error message

Returns:

  • (Guard)

    self for chaining



52
53
54
55
56
57
# File 'lib/philiprehberger/guard_clause/guard.rb', line 52

def positive(message = nil)
  if @value.respond_to?(:>) && !(@value > 0)
    handle_violation(message || 'value must be positive')
  end
  self
end

#valid?Boolean

Returns true if no errors were collected.

Returns:

  • (Boolean)

    true if no errors were collected



22
23
24
# File 'lib/philiprehberger/guard_clause/guard.rb', line 22

def valid?
  @errors.empty?
end