Class: ParamsReady::Result

Inherits:
AbstractReporter show all
Defined in:
lib/params_ready/result.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary

Attributes inherited from AbstractReporter

#name

Instance Method Summary collapse

Methods inherited from AbstractReporter

#error!, #for_child, #full_path

Constructor Details

#initialize(name) ⇒ Result

Returns a new instance of Result.



28
29
30
31
32
# File 'lib/params_ready/result.rb', line 28

def initialize(name)
  super
  @errors = []
  @children = {}
end

Instance Method Details

#child_ok?(path) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/params_ready/result.rb', line 68

def child_ok?(path)
  name, *path = path
  return ok? if name.nil?
  return true unless @children.key? name

  @children[name].child_ok?(path)
end

#errorObject



76
77
78
79
80
# File 'lib/params_ready/result.rb', line 76

def error
  return nil if ok?

  Result::Error.new(error_messages(' -- '))
end

#error_messages(separator = "\n") ⇒ Object



82
83
84
85
86
# File 'lib/params_ready/result.rb', line 82

def error_messages(separator = "\n")
  errors.flat_map do |scope, errors|
    ["errors for #{scope}"] + errors.map { |err| err.message }
  end.join(separator)
end

#errors(scope = '') ⇒ Object



40
41
42
43
44
45
46
# File 'lib/params_ready/result.rb', line 40

def errors(scope = '')
  scope = full_scope(scope)
  proper = @errors.empty? ? {} : { scope => @errors }
  @children.values.reduce(proper) do |result, child|
    result.merge(child.errors(scope))
  end
end

#full_scope(scope) ⇒ Object



34
35
36
37
38
# File 'lib/params_ready/result.rb', line 34

def full_scope(scope)
  return name if scope.empty?

  "#{scope}.#{name}"
end

#ok?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/params_ready/result.rb', line 60

def ok?
  return false unless @errors.empty?

  @children.values.all? do |child|
    child.ok?
  end
end

#report_error(path, err) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/params_ready/result.rb', line 48

def report_error(path, err)
  raise ParamsReadyError, "Is not Error: #{err}" unless err.is_a? StandardError

  name, *path = path
  if name.nil?
    @errors << err
  else
    @children[name] ||= Result.new(name)
    @children[name].report_error(path, err)
  end
end