Class: Spectre::Mixin
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
The description of the mixin.
-
#file ⇒ Object
readonly
The file where the mixin is defined.
-
#line ⇒ Object
readonly
The line in the file where the mixin is defined.
-
#params ⇒ Object
readonly
A list of required parameters the mixin uses.
Instance Method Summary collapse
-
#initialize(desc, required, block, file, line) ⇒ Mixin
constructor
A new instance of Mixin.
-
#run(run_context, params) ⇒ Object
Run the mixin with the given parameters in the context of the given
RunContextAll methods of theRunContextare available within the mixin block. -
#with(**params) ⇒ Object
Add execution paramters.
Methods included from Delegate
#instance_eval, #instance_exec, #method_missing, #respond_to_missing?
Constructor Details
#initialize(desc, required, block, file, line) ⇒ Mixin
Returns a new instance of Mixin.
731 732 733 734 735 736 737 738 |
# File 'lib/spectre.rb', line 731 def initialize desc, required, block, file, line @desc = desc @params = required @block = block @file = file @line = line @given = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Spectre::Delegate
Instance Attribute Details
#desc ⇒ Object (readonly)
The description of the mixin. This value has to be unique as it is used for running the mixin.
722 723 724 |
# File 'lib/spectre.rb', line 722 def desc @desc end |
#file ⇒ Object (readonly)
The file where the mixin is defined
727 728 729 |
# File 'lib/spectre.rb', line 727 def file @file end |
#line ⇒ Object (readonly)
The line in the file where the mixin is defined
729 730 731 |
# File 'lib/spectre.rb', line 729 def line @line end |
#params ⇒ Object (readonly)
A list of required parameters the mixin uses. When running the mixin, given params must contain the keys in this list.
725 726 727 |
# File 'lib/spectre.rb', line 725 def params @params end |
Instance Method Details
#run(run_context, params) ⇒ Object
Run the mixin with the given parameters in the context of the given RunContext All methods of the RunContext are available within the mixin block
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 |
# File 'lib/spectre.rb', line 757 def run run_context, params params ||= {} case params when Hash params.merge! @given unless @given.empty? if @params.any? missing_params = @params - params.keys raise "missing params: #{missing_params.join(', ')}" unless missing_params.empty? end params = [params.to_recursive_struct] when Array params = params.map(&:to_recursive_struct) end run_context.instance_exec(*params, &@block) end |
#with(**params) ⇒ Object
Add execution paramters. Available within the block of a mixin execution.
run 'some mixin' do
with some_key: 'a value',
a_number: 42
end
749 750 751 |
# File 'lib/spectre.rb', line 749 def with **params @given.merge! params end |