Class: Dry::Validation::Result
- Inherits:
-
Object
- Object
- Dry::Validation::Result
- Includes:
- Enumerable
- Defined in:
- lib/dry/validation/result.rb
Instance Attribute Summary collapse
-
#rule_results ⇒ Object
readonly
Returns the value of attribute rule_results.
Instance Method Summary collapse
- #<<(rule_result) ⇒ Object
- #[](name) ⇒ Object
- #by_name(name) ⇒ Object
- #each(&block) ⇒ Object
- #failures ⇒ Object
-
#initialize(rule_results) ⇒ Result
constructor
A new instance of Result.
- #merge!(other) ⇒ Object
- #successes ⇒ Object
- #to_ary ⇒ Object
- #to_h ⇒ Object
- #with_values(names) {|values| ... } ⇒ Object
Constructor Details
#initialize(rule_results) ⇒ Result
Returns a new instance of Result.
8 9 10 |
# File 'lib/dry/validation/result.rb', line 8 def initialize(rule_results) @rule_results = rule_results end |
Instance Attribute Details
#rule_results ⇒ Object (readonly)
Returns the value of attribute rule_results.
6 7 8 |
# File 'lib/dry/validation/result.rb', line 6 def rule_results @rule_results end |
Instance Method Details
#<<(rule_result) ⇒ Object
32 33 34 |
# File 'lib/dry/validation/result.rb', line 32 def <<(rule_result) rule_results << rule_result end |
#[](name) ⇒ Object
16 17 18 |
# File 'lib/dry/validation/result.rb', line 16 def [](name) to_h[name] end |
#by_name(name) ⇒ Object
41 42 43 |
# File 'lib/dry/validation/result.rb', line 41 def by_name(name) successes.detect { |rule_result| rule_result.name == name } end |
#each(&block) ⇒ Object
12 13 14 |
# File 'lib/dry/validation/result.rb', line 12 def each(&block) rule_results.each(&block) end |
#failures ⇒ Object
49 50 51 |
# File 'lib/dry/validation/result.rb', line 49 def failures rule_results.select(&:failure?) end |
#merge!(other) ⇒ Object
24 25 26 |
# File 'lib/dry/validation/result.rb', line 24 def merge!(other) rule_results.concat(other.rule_results) end |
#successes ⇒ Object
45 46 47 |
# File 'lib/dry/validation/result.rb', line 45 def successes rule_results.select(&:success?) end |
#to_ary ⇒ Object
28 29 30 |
# File 'lib/dry/validation/result.rb', line 28 def to_ary failures.map(&:to_ary) end |
#to_h ⇒ Object
20 21 22 |
# File 'lib/dry/validation/result.rb', line 20 def to_h @to_h ||= each_with_object({}) { |result, hash| hash[result.name] = result } end |
#with_values(names) {|values| ... } ⇒ Object
36 37 38 39 |
# File 'lib/dry/validation/result.rb', line 36 def with_values(names, &block) values = names.map { |name| by_name(name) }.compact.map(&:input) yield(values) if values.size == names.size end |