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.



11
12
13
14
15
16
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 11

def initialize
  @plays = []
  @tasks_builder = nil
  @context = nil
  @includes = []
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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 44

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

#_process_method(id) ⇒ Object



80
81
82
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 80

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 63

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

#ansible_include(filename, &block) ⇒ Object



40
41
42
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 40

def ansible_include(filename, &block)
  @includes << _ansible_include(filename, &block)
end

#format_trace_line(trace) ⇒ Object



57
58
59
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 57

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

#handler(name, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 33

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



18
19
20
21
22
23
24
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 18

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



26
27
28
29
30
31
# File 'lib/ansible/ruby/dsl_builders/file_level.rb', line 26

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