Class: Dry::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/rule.rb

Direct Known Subclasses

Dry::Rules::Binary, Dry::Rules::Present

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, errors = Dry::Errors.new, **args) ⇒ Rule

Returns a new instance of Rule.



12
13
14
15
16
# File 'lib/dry/rule.rb', line 12

def initialize(value, errors = Dry::Errors.new, **args)
  @value  = value
  @errors = errors
  @args   = args.symbolize_keys
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/dry/rule.rb', line 10

def args
  @args
end

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/dry/rule.rb', line 10

def errors
  @errors
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/dry/rule.rb', line 10

def value
  @value
end

Instance Method Details

#+(right) ⇒ Object



52
53
54
# File 'lib/dry/rule.rb', line 52

def +(right)
  Dry::Rules::Collection.new(self, errors, args.merge(right: right))
end

#add_errorObject



24
25
26
# File 'lib/dry/rule.rb', line 24

def add_error
  errors.add(key, messages[name.to_s] || 'invalid')
end

#and(right) ⇒ Object Also known as: &



34
35
36
# File 'lib/dry/rule.rb', line 34

def and(right)
  Dry::Rules::And.new(self, errors, args.merge(right: right))
end

#cloneObject



29
30
31
# File 'lib/dry/rule.rb', line 29

def clone
  self.class.new(value, errors.clone, args)
end

#nameObject



19
20
21
# File 'lib/dry/rule.rb', line 19

def name
  args[:name]
end

#or(right) ⇒ Object Also known as: |



46
47
48
# File 'lib/dry/rule.rb', line 46

def or(right)
  Dry::Rules::Or.new(self, errors, args.merge(right: right))
end

#then(right) ⇒ Object Also known as: >



40
41
42
# File 'lib/dry/rule.rb', line 40

def then(right)
  Dry::Rules::Then.new(self, errors, args.merge(right: right))
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/dry/rule.rb', line 57

def valid?
  raise NotImplementedError
end