Class: DSLCompose::Interpreter::Execution

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl_compose/interpreter/execution.rb,
lib/dsl_compose/interpreter/execution/arguments.rb,
lib/dsl_compose/interpreter/execution/method_calls.rb,
lib/dsl_compose/interpreter/execution/method_calls/method_call.rb

Defined Under Namespace

Classes: Arguments, InvalidDescriptionError, MethodCalls, MethodIsUniqueError, RequiredMethodNotCalledError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, dsl, *args, &block) ⇒ Execution

execute/process a dynamically defined DSL



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dsl_compose/interpreter/execution.rb', line 21

def initialize klass, dsl, *args, &block
  @klass = klass
  @dsl = dsl
  @method_calls = MethodCalls.new
  @arguments = Arguments.new(dsl.arguments, *args)

  # dynamically process the DSL by calling the provided block
  # all methods executions will be caught and processed by the method_missing method below
  if block
    instance_eval(&block)
  end

  # assert that all required methods have been called at least once
  dsl.required_dsl_methods.each do |dsl_method|
    unless @method_calls.method_called? dsl_method.name
      dsl_method_name = dsl_method&.name
      raise RequiredMethodNotCalledError, "The method #{dsl_method_name} is required, but was not called within this DSL"
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (private)

catch and process any method calls within the DSL block



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dsl_compose/interpreter/execution.rb', line 62

def method_missing(method_name, ...)
  # if the method does not exist, then this will raise a MethodDoesNotExistError
  dsl_method = @dsl.dsl_method method_name

  # if the method is unique, then it can only be called once per DSL
  if dsl_method.unique? && @method_calls.method_called?(method_name)
    raise MethodIsUniqueError, "This method `#{method_name}` is unique and can only be called once within this DSL"
  end

  @method_calls.add_method_call(dsl_method, ...)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



18
19
20
# File 'lib/dsl_compose/interpreter/execution.rb', line 18

def arguments
  @arguments
end

#dslObject (readonly)

Returns the value of attribute dsl.



15
16
17
# File 'lib/dsl_compose/interpreter/execution.rb', line 15

def dsl
  @dsl
end

#klassObject (readonly)

Returns the value of attribute klass.



16
17
18
# File 'lib/dsl_compose/interpreter/execution.rb', line 16

def klass
  @klass
end

#method_callsObject (readonly)

Returns the value of attribute method_calls.



17
18
19
# File 'lib/dsl_compose/interpreter/execution.rb', line 17

def method_calls
  @method_calls
end

Instance Method Details

#add_parser_usage_note(note) ⇒ Object

the parser can provide usage notes for how this dsl is being used, these are used to generate documentation



44
45
46
47
48
49
50
51
# File 'lib/dsl_compose/interpreter/execution.rb', line 44

def add_parser_usage_note note
  unless note.is_a?(String) && note.strip.length > 0
    raise InvalidDescriptionError, "The parser usage description `#{note}` is invalid, it must be of type string and have length greater than 0"
  end

  @parser_usage_notes ||= []
  @parser_usage_notes << note.strip
end

#parser_usage_notesObject

return the list of notes which describe how the parsers are using this DSL



54
55
56
57
# File 'lib/dsl_compose/interpreter/execution.rb', line 54

def parser_usage_notes
  @parser_usage_notes ||= []
  @parser_usage_notes
end