Class: CabezaDeTermo::JsonSpec::JsonExpression
- Inherits:
-
Object
- Object
- CabezaDeTermo::JsonSpec::JsonExpression
- Defined in:
- lib/cabeza-de-termo/json-spec/expressions/json-expression.rb
Overview
Base class for JsonExpression representing nodes in a json parse tree.
Direct Known Subclasses
JsonAnyOf, JsonAnything, JsonEach, JsonEachField, JsonField, JsonFieldName, JsonList, JsonObject, JsonScalar
Instance Method Summary collapse
-
#add_default_expectations ⇒ Object
Add the default expectations for this expression.
-
#add_expectation(expectation) ⇒ Object
Add an Expectation it to self expression Expectations.
-
#add_expectation_from_library(message) ⇒ Object
Create a new Expectation from the ExpectationsLibrary corresponding to the Message and add it to self expression Expectations.
-
#default_expectations ⇒ Object
Answer the default expectations for this expression.
-
#default_expectations_for(json_expression_type) ⇒ Object
Answer a collection of default Expectations to add to a new expression of type json_expression_type.
-
#expectations_library ⇒ Object
Answer the global ExpectationsLibrary.
-
#expectations_runner ⇒ Object
Get the expectations_runner with the Expectations to run on the value coresponding to self expression of a json object.
-
#initialize(parent_expression) ⇒ JsonExpression
constructor
A new instance of JsonExpression.
-
#is_a_modifier_library_method?(message) ⇒ Boolean
Answer true if the Message is a modifier message.
-
#is_an_expectations_library_method?(message) ⇒ Boolean
Answer true if the Message is an expectation message.
-
#json_spec ⇒ Object
Answer the top most JsonSpec expression.
-
#method_missing(method_name, *args, &block) ⇒ Object
When we receive a message that we don’t understand, check if.
- #new_any_of_expression ⇒ Object
- #new_anything_expression ⇒ Object
-
#new_each_field ⇒ Object
Create a new JsonEachField.
-
#new_expectations_runner ⇒ Object
Answer an ExpectationInterface to hold the Expectations on this expression.
-
#new_expression_for(type) ⇒ Object
Instance creation.
- #new_json_each_expression ⇒ Object
- #new_json_field_name_expression ⇒ Object
- #new_list_expression ⇒ Object
-
#new_named_field_expression(field_name) ⇒ Object
Create a new JsonField named field_name.
- #new_object_expression ⇒ Object
- #new_scalar_expression ⇒ Object
-
#parent_expression ⇒ Object
Get the parent expression in the parse tree.
-
#perform_modifier_from_library(message) ⇒ Object
Create a new modifier from the ExpectationsLibrary corresponding to the Message and allow it to modify self expression.
- #run_modifier(modifier) ⇒ Object
-
#set_expectations_runner(expectations_runner) ⇒ Object
Set the expectations_runner with Expectations to run on the value coresponding to self expression of a json object.
-
#set_parent_expression(parent_expression) ⇒ Object
Set the parent expression in the parse tree.
-
#to_be_as_defined_by(callable) ⇒ Object
Evaluate the Valuable with self expression as its parameter to allow it to add more expectations to self expression.
-
#to_be_as_defined_in(object, method_name) ⇒ Object
Send to object a message named method_name with self expression as its parameter to allow it to add more expectations to self expression.
Constructor Details
#initialize(parent_expression) ⇒ JsonExpression
Returns a new instance of JsonExpression.
14 15 16 17 18 19 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 14 def initialize(parent_expression) @parent_expression = parent_expression @expectations_runner = new_expectations_runner add_default_expectations end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
When we receive a message that we don’t understand, check if
-
it is an expectation message, then add the expected Expectation to self expression
-
it is a modifier message, then run the modifier on self expression
-
if none of the above, bubble the message up to the parent expression, so it can handle it.
92 93 94 95 96 97 98 99 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 92 def method_missing(method_name, *args, &block) = Message.new(method_name.to_sym, *args, &block) return add_expectation_from_library() if is_an_expectations_library_method?() return perform_modifier_from_library() if is_a_modifier_library_method?() super(method_name, *args, &block) end |
Instance Method Details
#add_default_expectations ⇒ Object
Add the default expectations for this expression.
130 131 132 133 134 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 130 def add_default_expectations() default_expectations.each do || .send_to(self) end end |
#add_expectation(expectation) ⇒ Object
Add an Expectation it to self expression Expectations. Answer self expression.
198 199 200 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 198 def add_expectation(expectation) expectations_runner.add(expectation) end |
#add_expectation_from_library(message) ⇒ Object
Create a new Expectation from the ExpectationsLibrary corresponding to the Message and add it to self expression Expectations. Answer self expression.
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 153 def add_expectation_from_library() expectation = expectations_library.() expectation.set_expectation_method(.method_name) expectation.set_json_spec(json_spec) add_expectation(expectation) CdT.bind_block_evaluation_to self, &.block unless .block.nil? self end |
#default_expectations ⇒ Object
Answer the default expectations for this expression. Subclasses should override self methos accondingly.
140 141 142 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 140 def default_expectations() [] end |
#default_expectations_for(json_expression_type) ⇒ Object
Answer a collection of default Expectations to add to a new expression of type json_expression_type.
240 241 242 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 240 def default_expectations_for(json_expression_type) json_spec.default_expectations_for(json_expression_type) end |
#expectations_library ⇒ Object
Answer the global ExpectationsLibrary.
74 75 76 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 74 def expectations_library() json_spec.expectations_library end |
#expectations_runner ⇒ Object
Get the expectations_runner with the Expectations to run on the value coresponding to self expression of a json object.
45 46 47 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 45 def expectations_runner() @expectations_runner end |
#is_a_modifier_library_method?(message) ⇒ Boolean
Answer true if the Message is a modifier message.
121 122 123 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 121 def is_a_modifier_library_method?() expectations_library.has_modifier_for?(.method_name) end |
#is_an_expectations_library_method?(message) ⇒ Boolean
Answer true if the Message is an expectation message.
110 111 112 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 110 def is_an_expectations_library_method?() expectations_library.has_expectation_for?(.method_name) end |
#json_spec ⇒ Object
Answer the top most JsonSpec expression. Implemented by bubbling self message up to the JsonSpec object.
65 66 67 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 65 def json_spec() parent_expression.json_spec end |
#new_any_of_expression ⇒ Object
277 278 279 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 277 def new_any_of_expression() JsonAnyOf.new(self) end |
#new_anything_expression ⇒ Object
289 290 291 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 289 def new_anything_expression() JsonAnything.new(self) end |
#new_each_field ⇒ Object
Create a new JsonEachField.
309 310 311 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 309 def new_each_field() JsonEachField.new(self) end |
#new_expectations_runner ⇒ Object
Answer an ExpectationInterface to hold the Expectations on this expression.
261 262 263 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 261 def new_expectations_runner() ExpectationsRunner.new end |
#new_expression_for(type) ⇒ Object
Instance creation
246 247 248 249 250 251 252 253 254 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 246 def new_expression_for(type) return new_scalar_expression if type == :scalar return new_object_expression if type == :object return new_list_expression if type == :list return new_any_of_expression if type == :any_of return new_anything_expression if type == :anything raise "Invalid expected type: #{type.inspect}" end |
#new_json_each_expression ⇒ Object
285 286 287 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 285 def new_json_each_expression() JsonEach.new(self) end |
#new_json_field_name_expression ⇒ Object
281 282 283 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 281 def new_json_field_name_expression() JsonFieldName.new(self) end |
#new_list_expression ⇒ Object
269 270 271 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 269 def new_list_expression() JsonList.new(self) end |
#new_named_field_expression(field_name) ⇒ Object
Create a new JsonField named field_name.
300 301 302 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 300 def new_named_field_expression(field_name) JsonField.new(self, field_name) end |
#new_object_expression ⇒ Object
265 266 267 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 265 def new_object_expression() JsonObject.new(self) end |
#new_scalar_expression ⇒ Object
273 274 275 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 273 def new_scalar_expression() JsonScalar.new(self) end |
#parent_expression ⇒ Object
Get the parent expression in the parse tree.
28 29 30 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 28 def parent_expression() @parent_expression end |
#perform_modifier_from_library(message) ⇒ Object
Create a new modifier from the ExpectationsLibrary corresponding to the Message and allow it to modify self expression. Answer self expression.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 175 def perform_modifier_from_library() modifier = expectations_library .() run_modifier(modifier) CdT.bind_block_evaluation_to self, &.block unless .block.nil? self end |
#run_modifier(modifier) ⇒ Object
186 187 188 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 186 def run_modifier(modifier) modifier.run_on(self) end |
#set_expectations_runner(expectations_runner) ⇒ Object
Set the expectations_runner with Expectations to run on the value coresponding to self expression of a json object.
55 56 57 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 55 def set_expectations_runner(expectations_runner) @expectations_runner = expectations_runner end |
#set_parent_expression(parent_expression) ⇒ Object
Set the parent expression in the parse tree.
35 36 37 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 35 def set_parent_expression(parent_expression) @parent_expression = parent_expression end |
#to_be_as_defined_by(callable) ⇒ Object
Evaluate the Valuable with self expression as its parameter to allow it to add more expectations to self expression. Answer the same that answers the Valuable.
226 227 228 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 226 def to_be_as_defined_by(callable) callable.call(self) end |
#to_be_as_defined_in(object, method_name) ⇒ Object
Send to object a message named method_name with self expression as its parameter to allow it to add more expectations to self expression. Answer the same that answers the message sent.
213 214 215 |
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 213 def to_be_as_defined_in(object, method_name) to_be_as_defined_by( ObjectMethod.new(object, method_name) ) end |