Class: Ansible::Ruby::DslBuilders::FileLevel

Inherits:
Base
  • Object
show all
Defined in:
lib/ansible/ruby/dsl_builders/file_level.rb

Instance Method Summary collapse

Methods inherited from Base

#jinja, #method_missing, #respond_to_missing?

Constructor Details

#initializeFileLevel

Returns a new instance of FileLevel.



9
10
11
12
13
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 9

def initialize
  @plays = []
  @tasks_builder = nil
  @context = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ansible::Ruby::DslBuilders::Base

Instance Method Details

#_handled_eval(ruby_filename) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 37

def _handled_eval(ruby_filename)
  ruby_code = File.read ruby_filename
  instance_eval ruby_code, ruby_filename
  # error code
  nil
rescue StandardError => error
  only_user_code = error.backtrace_locations
                        .select { |trace| trace.absolute_path == ruby_filename }
                        .map { |trace| format_trace_line(trace) }
  message = "#{error.message}\n****Error Location:****\n#{only_user_code.join("\n")}"
  Exception.new message
end

#_process_method(id) ⇒ Object



70
71
72
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 70

def _process_method(id, *)
  no_method_error id, 'Only valid options are [:task, :handler, :play]'
end

#_resultObject

any order/lazy result :reek:NilCheck - when nil is the simplest way to check this



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 56

def _result
  case @context
  when :playbook
    # TODO: Add a playbook DSL and do this like tasks
    Models::Playbook.new plays: @plays
  when :tasks, :handlers
    @tasks_builder._result
  when nil
    raise 'Must supply at least 1 handler/task/play!'
  else
    raise "Unknown context #{@context}"
  end
end

#format_trace_line(trace) ⇒ Object



50
51
52
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 50

def format_trace_line(trace)
  "#{trace.path}:#{trace.lineno}"
end

#handler(name, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 30

def handler(name, &block)
  _validate_context :handlers
  @context = :handlers
  @tasks_builder ||= Tasks.new(:handlers)
  @tasks_builder.handler name, &block
end

#play(name = nil, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 15

def play(name = nil, &block)
  _validate_context :playbook
  @context = :playbook
  play_builder = Play.new name
  play_builder.instance_eval(&block)
  @plays << play_builder._result
end

#task(name, &block) ⇒ Object



23
24
25
26
27
28
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 23

def task(name, &block)
  _validate_context :tasks
  @context = :tasks
  @tasks_builder ||= Tasks.new(:tasks)
  @tasks_builder.task name, &block
end