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, called_from, *args, &block) ⇒ Execution

execute/process a dynamically defined DSL



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

def initialize klass, dsl, called_from, *args, &block
  @klass = klass
  @dsl = dsl
  @called_from = called_from
  @method_calls = MethodCalls.new
  @arguments = Arguments.new(dsl.arguments, called_from, *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.new("The method #{dsl_method_name} is required, but was not called within this DSL", called_from)
    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



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dsl_compose/interpreter/execution.rb', line 64

def method_missing(method_name, ...)
  called_from = caller(1..1).first

  # 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.new("This method `#{method_name}` is unique and can only be called once within this DSL", called_from)
  end

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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

#called_fromObject (readonly)

Returns the value of attribute called_from.



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

def called_from
  @called_from
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.



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

def klass
  @klass
end

#method_callsObject (readonly)

Returns the value of attribute method_calls.



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

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



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

def add_parser_usage_note note
  unless note.is_a?(String) && note.strip.length > 0
    raise InvalidDescriptionError.new("The parser usage description `#{note}` is invalid, it must be of type string and have length greater than 0", called_from)
  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



56
57
58
59
# File 'lib/dsl_compose/interpreter/execution.rb', line 56

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