Class: Reform::Contract::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/reform/result.rb

Overview

Collects all native results of a form of all groups and provides a unified API: #success?, #errors, #messages, #hints. #success? returns validity of the branch.

Defined Under Namespace

Classes: Errors, Pointer

Instance Method Summary collapse

Constructor Details

#initialize(results, nested_results = []) ⇒ Result

DISCUSS: do we like this?



7
8
9
10
# File 'lib/reform/result.rb', line 7

def initialize(results, nested_results = []) # DISCUSS: do we like this?
  @results = results # native Result objects, e.g. `#<Dry::Validation::Result output={:title=>"Fallout", :composer=>nil} errors={}>`
  @failure = (results + nested_results).find(&:failure?) # TODO: test nested.
end

Instance Method Details

#add_error(key, error_text) ⇒ Object



22
23
24
# File 'lib/reform/result.rb', line 22

def add_error(key, error_text)
  CustomError.new(key, error_text, @results)
end

#errors(*args) ⇒ Object



16
# File 'lib/reform/result.rb', line 16

def errors(*args);   filter_for(:errors, *args) end

#failure?Boolean

Returns:

  • (Boolean)


12
# File 'lib/reform/result.rb', line 12

def failure?; @failure  end

#hints(*args) ⇒ Object



20
# File 'lib/reform/result.rb', line 20

def hints(*args);    filter_for(:hints, *args) end

#messages(*args) ⇒ Object



18
# File 'lib/reform/result.rb', line 18

def messages(*args); filter_for(:messages, *args) end

#success?Boolean

Returns:

  • (Boolean)


14
# File 'lib/reform/result.rb', line 14

def success?; !failure? end

#to_resultsObject



26
27
28
# File 'lib/reform/result.rb', line 26

def to_results
  @results
end