Class: NxtSchema::Node::Base
- Inherits:
-
Object
- Object
- NxtSchema::Node::Base
- Defined in:
- lib/nxt_schema/node/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#coerced ⇒ Object
readonly
Returns the value of attribute coerced.
-
#coerced_nodes ⇒ Object
readonly
Returns the value of attribute coerced_nodes.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#error_key ⇒ Object
readonly
Returns the value of attribute error_key.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#input ⇒ Object
Returns the value of attribute input.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#node ⇒ Object
Returns the value of attribute node.
-
#output ⇒ Object
Returns the value of attribute output.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #add_error(error) ⇒ Object
- #add_schema_error(error) ⇒ Object
- #call ⇒ Object
-
#initialize(node:, input: Undefined.new, parent:, context:, error_key:) ⇒ Base
constructor
A new instance of Base.
- #merge_errors(node) ⇒ Object
- #root? ⇒ Boolean
- #run_validations ⇒ Object
- #up(levels = 1) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(node:, input: Undefined.new, parent:, context:, error_key:) ⇒ Base
Returns a new instance of Base.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/nxt_schema/node/base.rb', line 4 def initialize(node:, input: Undefined.new, parent:, context:, error_key:) @node = node @input = input @parent = parent @output = nil @error_key = error_key @context = context || parent&.context @coerced = false @coerced_nodes = parent&.coerced_nodes || [] @is_root = parent.nil? @root = parent.nil? ? self : parent.root @errors = ErrorStore.new(self) @locale = node..fetch(:locale) { parent&.locale || 'en' }.to_s @index = error_key resolve_error_key(error_key) end |
Instance Attribute Details
#coerced ⇒ Object
Returns the value of attribute coerced.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def coerced @coerced end |
#coerced_nodes ⇒ Object (readonly)
Returns the value of attribute coerced_nodes.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def coerced_nodes @coerced_nodes end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def context @context end |
#error_key ⇒ Object (readonly)
Returns the value of attribute error_key.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def error_key @error_key end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def errors @errors end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def index @index end |
#input ⇒ Object
Returns the value of attribute input.
22 23 24 |
# File 'lib/nxt_schema/node/base.rb', line 22 def input @input end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def locale @locale end |
#node ⇒ Object
Returns the value of attribute node.
22 23 24 |
# File 'lib/nxt_schema/node/base.rb', line 22 def node @node end |
#output ⇒ Object
Returns the value of attribute output.
22 23 24 |
# File 'lib/nxt_schema/node/base.rb', line 22 def output @output end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def parent @parent end |
#root ⇒ Object
Returns the value of attribute root.
23 24 25 |
# File 'lib/nxt_schema/node/base.rb', line 23 def root @root end |
Instance Method Details
#add_error(error) ⇒ Object
39 40 41 |
# File 'lib/nxt_schema/node/base.rb', line 39 def add_error(error) errors.add_validation_error(message: error) end |
#add_schema_error(error) ⇒ Object
43 44 45 |
# File 'lib/nxt_schema/node/base.rb', line 43 def add_schema_error(error) errors.add_schema_error(message: error) end |
#call ⇒ Object
25 26 27 |
# File 'lib/nxt_schema/node/base.rb', line 25 def call raise NotImplementedError, 'Implement this in our sub class' end |
#merge_errors(node) ⇒ Object
47 48 49 |
# File 'lib/nxt_schema/node/base.rb', line 47 def merge_errors(node) errors.merge_errors(node) end |
#root? ⇒ Boolean
31 32 33 |
# File 'lib/nxt_schema/node/base.rb', line 31 def root? @is_root end |
#run_validations ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/nxt_schema/node/base.rb', line 51 def run_validations return false unless coerced? node.validations.each do |validation| args = [self, input] validation.call(*args.take(validation.arity)) end end |
#up(levels = 1) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/nxt_schema/node/base.rb', line 60 def up(levels = 1) 0.upto(levels - 1).inject(self) do |acc, _| parent = acc.send(:parent) break acc unless parent parent end end |
#valid? ⇒ Boolean
35 36 37 |
# File 'lib/nxt_schema/node/base.rb', line 35 def valid? errors.empty? end |