Module: DHS::Problems::Nested::Base
Instance Method Summary collapse
-
#nest(messages, scope = nil) ⇒ Object
Filters base errors by scope and reduces key by given scope name; returns plain array if end of tree is reached.
-
#reached_leaf?(messages) ⇒ Boolean
Identifies if the end of nested errors tree is reached.
-
#remove_scope(messages, scope) ⇒ Object
Removes scope from given messages’ key.
-
#translate_rails_to_api_scope(scope) ⇒ Object
Translates rails like accessors for collections like first, second, last to api error paths.
Instance Method Details
#nest(messages, scope = nil) ⇒ Object
Filters base errors by scope and reduces key by given scope name; returns plain array if end of tree is reached
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dhs/problems/nested/base.rb', line 9 def nest(, scope = nil) scope = translate_rails_to_api_scope(scope) return unless scope = .select do |key, _| key.match(/^#{scope}/) end # if only one key and this key has no dots, exit with plain if reached_leaf?() .first[1] else remove_scope(, scope) end end |
#reached_leaf?(messages) ⇒ Boolean
Identifies if the end of nested errors tree is reached
24 25 26 27 |
# File 'lib/dhs/problems/nested/base.rb', line 24 def reached_leaf?() .keys.length == 1 && !.first[0].match(/\./) end |
#remove_scope(messages, scope) ⇒ Object
Removes scope from given messages’ key
30 31 32 33 34 35 |
# File 'lib/dhs/problems/nested/base.rb', line 30 def remove_scope(, scope) .each_with_object({}) do |element, hash| key = element[0].to_s.gsub(/^#{scope}\./, '') hash[key.to_sym] = element[1] end end |
#translate_rails_to_api_scope(scope) ⇒ Object
Translates rails like accessors for collections like first, second, last to api error paths
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dhs/problems/nested/base.rb', line 39 def translate_rails_to_api_scope(scope) case scope when :first 0 when :second 1 when :last return values.length - 1 if .present? 0 else scope end end |