Class: CabezaDeTermo::JsonSpec::JsonExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/cabeza-de-termo/json-spec/expressions/json-expression.rb

Overview

Base class for JsonExpression representing nodes in a json parse tree.

Instance Method Summary collapse

Constructor Details

#initialize(parent_expression) ⇒ JsonExpression

Returns a new instance of JsonExpression.

Parameters:

  • JsonExpression

    parent_expression The parent expression in the parse tree.



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.

Parameters:

  • Message

    message The message that self object does not understand.

Returns:

  • mixed Answer whatever the actual handler of the message answered



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 = Message.new(method_name.to_sym, *args, &block)

	return add_expectation_from_library(message) if is_an_expectations_library_method?(message)
	return perform_modifier_from_library(message) if is_a_modifier_library_method?(message)

	super(method_name, *args, &block)
end

Instance Method Details

#add_default_expectationsObject

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 |message|
		message.send_to(self)
	end
end

#add_expectation(expectation) ⇒ Object

Add an Expectation it to self expression Expectations. Answer self expression.

Parameters:

  • Message

    message The message that self object does not understood.

Returns:

  • JsonExpression



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.

Parameters:

  • Message

    message The message that self object does not understood.

Returns:

  • JsonExpression



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(message)
	expectation = expectations_library.new_expectation_from_message(message)

	expectation.set_expectation_method(message.method_name)
	expectation.set_json_spec(json_spec)

	add_expectation(expectation)

	CdT.bind_block_evaluation_to self, &message.block unless message.block.nil?

	self
end

#default_expectationsObject

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.

Parameters:

  • string

    json_expression_type The type of the expression.

Returns:

  • Expectation[]



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_libraryObject

Answer the global ExpectationsLibrary.

Returns:

  • JsonSpec



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_runnerObject

Get the expectations_runner with the Expectations to run on the value coresponding to self expression of a json object.

Returns:

  • expectations_runnerInterface



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.

Parameters:

  • Message

    message The message that self object does not understood.

Returns:

  • (Boolean)

    boolean



121
122
123
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 121

def is_a_modifier_library_method?(message)
	expectations_library.has_modifier_for?(message.method_name)
end

#is_an_expectations_library_method?(message) ⇒ Boolean

Answer true if the Message is an expectation message.

Parameters:

  • Message

    message The message that self object does not understood.

Returns:

  • (Boolean)

    boolean



110
111
112
# File 'lib/cabeza-de-termo/json-spec/expressions/json-expression.rb', line 110

def is_an_expectations_library_method?(message)
	expectations_library.has_expectation_for?(message.method_name)
end

#json_specObject

Answer the top most JsonSpec expression. Implemented by bubbling self message up to the JsonSpec object.

Returns:

  • JsonSpec



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_expressionObject



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_expressionObject



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_fieldObject

Create a new JsonEachField.

Returns:

  • 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_runnerObject

Answer an ExpectationInterface to hold the Expectations on this expression.

Returns:

  • ExpectationInterface



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_expressionObject



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_expressionObject



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_expressionObject



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.

Parameters:

  • string

    field_name The name of the field to create on self JsonObject.

Returns:

  • JsonField



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_expressionObject



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_expressionObject



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_expressionObject

Get the parent expression in the parse tree.

Returns:

  • JsonExpression



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.

Parameters:

  • Message

    message The message that self object does not understood.

Returns:

  • JsonExpression



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(message)
	modifier = expectations_library
						.new_modifier_from_message(message)

	run_modifier(modifier)

	CdT.bind_block_evaluation_to self, &message.block unless message.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.

Parameters:

  • expectations_runnerInterface

    expectations_runner



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.

Parameters:

  • Valuable

    valuable The object to evaluate with self expression as its parameter.

Returns:

  • mixed



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.

Parameters:

  • mixed

    object The object receiving the message.

  • string

    method_name The name of the method to invoke on the object.

Returns:

  • mixed



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