Class: Senko::Validator::EvaluationContext
- Inherits:
-
Object
- Object
- Senko::Validator::EvaluationContext
- Defined in:
- lib/senko/validator.rb
Constant Summary collapse
- EMPTY_EVALUATED =
{}.freeze
Instance Method Summary collapse
- #dynamic_target(anchor) ⇒ Object
- #evaluated_items_by_location ⇒ Object
- #evaluated_properties_by_location ⇒ Object
- #evaluation_empty? ⇒ Boolean
- #fork(track_evaluation: true) ⇒ Object
-
#initialize(dynamic_scopes = []) ⇒ EvaluationContext
constructor
A new instance of EvaluationContext.
- #item_evaluated?(location, index) ⇒ Boolean
- #mark_item(location, index) ⇒ Object
- #mark_property(location, name) ⇒ Object
- #merge(child) ⇒ Object
- #pop_dynamic_scope ⇒ Object
- #property_evaluated?(location, name) ⇒ Boolean
- #push_dynamic_scope(anchors) ⇒ Object
- #track_evaluation? ⇒ Boolean
Constructor Details
#initialize(dynamic_scopes = []) ⇒ EvaluationContext
Returns a new instance of EvaluationContext.
51 52 53 |
# File 'lib/senko/validator.rb', line 51 def initialize(dynamic_scopes = []) @dynamic_scopes = dynamic_scopes.dup end |
Instance Method Details
#dynamic_target(anchor) ⇒ Object
102 103 104 105 |
# File 'lib/senko/validator.rb', line 102 def dynamic_target(anchor) scope = @dynamic_scopes.find { |candidate| candidate.key?(anchor) } scope&.fetch(anchor) end |
#evaluated_items_by_location ⇒ Object
59 60 61 |
# File 'lib/senko/validator.rb', line 59 def evaluated_items_by_location @evaluated_items_by_location || EMPTY_EVALUATED end |
#evaluated_properties_by_location ⇒ Object
55 56 57 |
# File 'lib/senko/validator.rb', line 55 def evaluated_properties_by_location @evaluated_properties_by_location || EMPTY_EVALUATED end |
#evaluation_empty? ⇒ Boolean
89 90 91 92 |
# File 'lib/senko/validator.rb', line 89 def evaluation_empty? (!@evaluated_properties_by_location || @evaluated_properties_by_location.empty?) && (!@evaluated_items_by_location || @evaluated_items_by_location.empty?) end |
#fork(track_evaluation: true) ⇒ Object
107 108 109 |
# File 'lib/senko/validator.rb', line 107 def fork(track_evaluation: true) track_evaluation ? self.class.new(@dynamic_scopes) : NoopEvaluationContext.new(@dynamic_scopes) end |
#item_evaluated?(location, index) ⇒ Boolean
82 83 84 85 86 87 |
# File 'lib/senko/validator.rb', line 82 def item_evaluated?(location, index) return false unless @evaluated_items_by_location items = @evaluated_items_by_location.fetch(location, nil) items ? items.key?(index) : false end |
#mark_item(location, index) ⇒ Object
71 72 73 |
# File 'lib/senko/validator.rb', line 71 def mark_item(location, index) evaluated_items_for_write[location][index] = true end |
#mark_property(location, name) ⇒ Object
63 64 65 |
# File 'lib/senko/validator.rb', line 63 def mark_property(location, name) evaluated_properties_for_write[location][name.to_s] = true end |
#merge(child) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/senko/validator.rb', line 111 def merge(child) child.evaluated_properties_by_location.each do |location, properties| evaluated_properties_for_write[location].update(properties) end child.evaluated_items_by_location.each do |location, items| evaluated_items_for_write[location].update(items) end end |
#pop_dynamic_scope ⇒ Object
98 99 100 |
# File 'lib/senko/validator.rb', line 98 def pop_dynamic_scope @dynamic_scopes.pop end |
#property_evaluated?(location, name) ⇒ Boolean
75 76 77 78 79 80 |
# File 'lib/senko/validator.rb', line 75 def property_evaluated?(location, name) return false unless @evaluated_properties_by_location properties = @evaluated_properties_by_location.fetch(location, nil) properties ? properties.key?(name.to_s) : false end |
#push_dynamic_scope(anchors) ⇒ Object
94 95 96 |
# File 'lib/senko/validator.rb', line 94 def push_dynamic_scope(anchors) @dynamic_scopes << anchors end |
#track_evaluation? ⇒ Boolean
67 68 69 |
# File 'lib/senko/validator.rb', line 67 def track_evaluation? true end |