Class: DSLCompose::Reader
- Inherits:
-
Object
- Object
- DSLCompose::Reader
- Defined in:
- lib/dsl_compose/reader.rb,
lib/dsl_compose/reader/execution_reader.rb,
lib/dsl_compose/reader/execution_reader/arguments_reader.rb
Overview
This class is a decorator for DSL executions.
When a dynamically defined DSL is executed on a class, it creates an execution object which represents the argument values, and any methods and subsequent method argument values which were provided. This class provides a clean and simple API to access those vaues
Defined Under Namespace
Classes: DSLNotFound, ExecutionReader, NoDSLExecutionFound
Instance Method Summary collapse
-
#all_executions ⇒ Object
Returns an array of ExecutionReaders to represent each time the DSL was used on the provided class or ancestors of the provided class.
-
#ancestor_executions ⇒ Object
Returns an array of ExecutionReaders to represent each time the DSL was used on the ancestors of the provided class, but not on the provided class itself.
-
#dsl_used? ⇒ Boolean
Returns true if dsl has been executed once or more on the provided class, otherwise returns false.
-
#dsl_used_on_ancestors? ⇒ Boolean
Returns true if dsl has been executed once or more on one of the ancestors of the provided class, otherwise returns false.
-
#dsl_used_on_class_or_ancestors? ⇒ Boolean
Returns true if dsl has been executed once or more on the provided class or any of its ancestors, otherwise returns false.
-
#executions ⇒ Object
Returns an array of ExecutionReaders to represent each time the DSL was used on the provided class.
-
#initialize(klass, dsl_name) ⇒ Reader
constructor
given a class and the DSL name, finds and creates a reference to the DSL and the ancestor class where the DSL was originally defined.
-
#last_execution ⇒ Object
Returns an ExecutionReader class which exposes a simple API to access the arguments, methods and method arguments provided when using this DSL.
-
#last_execution! ⇒ Object
A wrapper for last_execution which raises an error if no execution exists.
Constructor Details
#initialize(klass, dsl_name) ⇒ Reader
given a class and the DSL name, finds and creates a reference to the DSL and the ancestor class where the DSL was originally defined
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dsl_compose/reader.rb', line 19 def initialize klass, dsl_name # a reference to the class and dsl_name which we want to read values for @klass = klass @dsl_name = dsl_name # Move up through this classes ancestors until we find the class which defined # the DSL with the provided name. When we reach the top of the ancestor chain we # exit the loop. while klass # if we find a DSL with this name, then store a reference to the DSL and the # ancestor class where it was defined if DSLs.class_dsl_exists?(klass, dsl_name) @dsl = DSLs.class_dsl(klass, dsl_name) @dsl_defining_class = klass # stop once we find the DSL break end # the DSL was not found here, so traverse up the provided classes hierachy # and keep looking for where this DSL was initially defined klass = klass.superclass end # if no DSL was found, then raise an error if @dsl.nil? && @dsl_defining_class.nil? raise DSLNotFound, "No DSL named `#{dsl_name}` was found for class `#{klass}`" end end |
Instance Method Details
#all_executions ⇒ Object
Returns an array of ExecutionReaders to represent each time the DSL was used on the provided class or ancestors of the provided class. The executions will be returned in the order they were executed, which is the earliest ancestor first and if the DSL was used more than once on a class then the order they were used.
111 112 113 114 115 |
# File 'lib/dsl_compose/reader.rb', line 111 def all_executions @dsl_defining_class.dsls.class_dsl_executions(@klass, @dsl_name, true, true).map do |execution| ExecutionReader.new execution end end |
#ancestor_executions ⇒ Object
Returns an array of ExecutionReaders to represent each time the DSL was used on the ancestors of the provided class, but not on the provided class itself. The executions will be returned in the order they were executed, which is the earliest ancestor first and if the DSL was used more than once on a class then the order they were used.
101 102 103 104 105 |
# File 'lib/dsl_compose/reader.rb', line 101 def ancestor_executions @dsl_defining_class.dsls.class_dsl_executions(@klass, @dsl_name, false, true).map do |execution| ExecutionReader.new execution end end |
#dsl_used? ⇒ Boolean
Returns true if dsl has been executed once or more on the provided class, otherwise returns false.
50 51 52 |
# File 'lib/dsl_compose/reader.rb', line 50 def dsl_used? executions.any? end |
#dsl_used_on_ancestors? ⇒ Boolean
Returns true if dsl has been executed once or more on one of the ancestors of the provided class, otherwise returns false.
56 57 58 |
# File 'lib/dsl_compose/reader.rb', line 56 def dsl_used_on_ancestors? ancestor_executions.any? end |
#dsl_used_on_class_or_ancestors? ⇒ Boolean
Returns true if dsl has been executed once or more on the provided class or any of its ancestors, otherwise returns false.
62 63 64 |
# File 'lib/dsl_compose/reader.rb', line 62 def dsl_used_on_class_or_ancestors? all_executions.any? end |
#executions ⇒ Object
Returns an array of ExecutionReaders to represent each time the DSL was used on the provided class.
90 91 92 93 94 |
# File 'lib/dsl_compose/reader.rb', line 90 def executions @dsl_defining_class.dsls.class_dsl_executions(@klass, @dsl_name, true, false).map do |execution| ExecutionReader.new execution end end |
#last_execution ⇒ Object
Returns an ExecutionReader class which exposes a simple API to access the arguments, methods and method arguments provided when using this DSL.
If the dsl has been executed once or more on the provided class, then a reader for the last (most recent) execution will be returned. If the DSL was not executed on the provided class, then we traverse up the classes ancestors and look for the last time it was executed on each ancestor. If no execution of the DSL is found, then nil will be returned
74 75 76 77 78 79 80 81 |
# File 'lib/dsl_compose/reader.rb', line 74 def last_execution execution = @dsl_defining_class.dsls.get_last_dsl_execution(@klass, @dsl_name) if execution.nil? nil else ExecutionReader.new execution end end |
#last_execution! ⇒ Object
A wrapper for last_execution which raises an error if no execution exists
84 85 86 |
# File 'lib/dsl_compose/reader.rb', line 84 def last_execution! last_execution || (raise NoDSLExecutionFound, "No execution of the `#{@dsl_name}` dsl was found on `#{@klass}` or any of its ancestors") end |