Class: DSLCompose::Reader::ExecutionReader

Inherits:
Object
  • Object
show all
Defined in:
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: ArgumentsReader, InvalidExecution, MethodDoesNotExist

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execution) ⇒ ExecutionReader

Returns a new instance of ExecutionReader.

Raises:



20
21
22
23
# File 'lib/dsl_compose/reader/execution_reader.rb', line 20

def initialize execution
  raise InvalidExecution unless execution.is_a? Interpreter::Execution
  @execution = execution
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

Catch and process any method calls, if a DSL method with the same name exists then return a representation of the arguments which were provided to the execution of that method within the DSL.

If the method is marked as unique, then an arguments object will be returned, if the method is not unique, then an array of arguments objects will be returned (one for each time the method was used within this DSL execution).



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dsl_compose/reader/execution_reader.rb', line 51

def method_missing method_name
  # Fetch the method from the DSL (this will raise an error if a method with this name
  # was not defined for this exections DSL).
  dsl_method = @execution.dsl.dsl_method method_name

  # the Arguments object which represents the possible arguments which can be
  # used with this method
  arguments = dsl_method.arguments

  # Fetch the array of method calls, this represents each use of a DSL method within
  # a single use of the DSL
  method_calls = @execution.method_calls.method_calls_by_name(method_name)

  # If the use of this DSL method is only allowed once per DSL execution, then return
  # an ArgumentsReader object which represents the argument values provided when the
  # method was used.
  #
  # If the method was never used, then nil will be returned. We don't have to validate
  # if the method use is required or not here, because that validation already happened
  # when the DSL was used.
  if dsl_method.unique?
    method_call = method_calls.first
    unless method_call.nil?
      ArgumentsReader.new arguments, method_call.arguments
    end

  # If the method call is not unique, then return an array representing the argument
  # values provided for each use of the DSL method
  else
    method_calls.map do |method_call|
      ArgumentsReader.new arguments, method_call.arguments
    end
  end
end

Instance Attribute Details

#executionObject (readonly)

Returns the value of attribute execution.



18
19
20
# File 'lib/dsl_compose/reader/execution_reader.rb', line 18

def execution
  @execution
end

Instance Method Details

#argumentsObject

returns an object which represents the arguments available for this DSL and allows accessing their values via methods



32
33
34
# File 'lib/dsl_compose/reader/execution_reader.rb', line 32

def arguments
  ArgumentsReader.new @execution.dsl.arguments, @execution.arguments
end

#dsl_nameObject

useful when creating a DSL reader which is usable across multiple DSLs



26
27
28
# File 'lib/dsl_compose/reader/execution_reader.rb', line 26

def dsl_name
  @execution.dsl.name
end

#method_called?(method_name) ⇒ Boolean

was a method with the provided name called during the use of this DSL

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/dsl_compose/reader/execution_reader.rb', line 37

def method_called? method_name
  unless @execution.dsl.has_dsl_method? method_name
    raise MethodDoesNotExist, "The method `#{method_name}` does not exist for DSL `#{@execution.dsl.name}`"
  end
  @execution.method_calls.method_called? method_name
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/dsl_compose/reader/execution_reader.rb', line 86

def respond_to_missing? method_name
  method_called? method_name
end