Module: Remarkable::DSL::Matches
- Defined in:
- lib/remarkable/dsl/matches.rb
Instance Attribute Summary collapse
-
#and then call allow_nil? and allow_blank?. Assertions should ⇒ Object
For each instance under the collection declared in
arguments
, this method will call each method declared incollection_assertions
.
Instance Method Summary collapse
-
#matches?(subject) ⇒ Boolean
For each instance under the collection declared in
arguments
, this method will call each method declared incollection_assertions
.
Instance Attribute Details
#and then call allow_nil? and allow_blank?. Assertions should ⇒ Object
For each instance under the collection declared in arguments
, this method will call each method declared in collection_assertions
.
As an example, let’s assume you have the following matcher:
arguments :collection => :attributes
assertions :allow_nil?, :allow_blank?
For each attribute in @attributes, we will set the instance variable return true if it pass or false if it fails. When it fails, it will use I18n API to find the proper failure message:
expectations:
allow_nil?: allowed the value to be nil
allow_blank?: allowed the value to be blank
Or you can set the message in the instance variable @expectation in the assertion method if you don’t want to rely on I18n API.
This method also call the methods declared in single_assertions. Which work the same way as assertions, except it doesn’t loop for each value in the collection.
It also provides a before_assert callback that you might want to use it to manipulate the subject before the assertions start.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/remarkable/dsl/matches.rb', line 32 def matches?(subject) @subject = subject run_before_assert_callbacks (self.class.matcher_single_assertions) && assert_matcher_for(instance_variable_get("@#{self.class.matcher_arguments[:collection]}") || []) do |value| instance_variable_set("@#{self.class.matcher_arguments[:as]}", value) (self.class.matcher_collection_assertions) end end |
Instance Method Details
#matches?(subject) ⇒ Boolean
For each instance under the collection declared in arguments
, this method will call each method declared in collection_assertions
.
As an example, let’s assume you have the following matcher:
arguments :collection => :attributes
assertions :allow_nil?, :allow_blank?
For each attribute in @attributes, we will set the instance variable return true if it pass or false if it fails. When it fails, it will use I18n API to find the proper failure message:
expectations:
allow_nil?: allowed the value to be nil
allow_blank?: allowed the value to be blank
Or you can set the message in the instance variable @expectation in the assertion method if you don’t want to rely on I18n API.
This method also call the methods declared in single_assertions. Which work the same way as assertions, except it doesn’t loop for each value in the collection.
It also provides a before_assert callback that you might want to use it to manipulate the subject before the assertions start.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/remarkable/dsl/matches.rb', line 32 def matches?(subject) @subject = subject run_before_assert_callbacks (self.class.matcher_single_assertions) && assert_matcher_for(instance_variable_get("@#{self.class.matcher_arguments[:collection]}") || []) do |value| instance_variable_set("@#{self.class.matcher_arguments[:as]}", value) (self.class.matcher_collection_assertions) end end |