Class: Dslimple::Applier

Inherits:
Object
  • Object
show all
Defined in:
lib/dslimple/applier.rb

Constant Summary collapse

OPERATION_COLORS =
{
  addition: :green,
  modification: :yellow,
  deletion: :red
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, shell, options = {}) ⇒ Applier

Returns a new instance of Applier.



13
14
15
16
17
# File 'lib/dslimple/applier.rb', line 13

def initialize(client, shell, options = {})
  @client = client
  @shell = shell
  @options = options
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/dslimple/applier.rb', line 11

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/dslimple/applier.rb', line 11

def options
  @options
end

#shellObject (readonly)

Returns the value of attribute shell.



11
12
13
# File 'lib/dslimple/applier.rb', line 11

def shell
  @shell
end

Instance Method Details

#apply(queries) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/dslimple/applier.rb', line 56

def apply(queries)
  shell.say('Apply', :bold)
  queries.each do |query|
    show_query(query)
    query.execute(client, client.)
  end
end

#executeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dslimple/applier.rb', line 19

def execute
  dsl = Dslimple::DSL.new(options[:file], options)

  dsl.execute

  expected_zones = dsl.transform
  expected_zones.select! { |zone| options[:only].include?(zone.name) } unless options[:only].empty?

  @buildler = Dslimple::QueryBuilder.new(fetch_zones, expected_zones, options)
  @buildler.execute
  queries = @buildler.filtered_queries(options)

  if queries.empty?
    shell.say('No Changes', :bold)
    return
  end

  show_plan(queries)

  return if options[:dry_run] || !(options[:yes] || shell.yes?("Apply #{queries.size} changes. OK?(y/n) >"))

  apply(queries)
end

#fetch_zonesObject



43
44
45
46
47
# File 'lib/dslimple/applier.rb', line 43

def fetch_zones
  zones = client.all_zones(with_records: true)
  zones.select! { |zone| options[:only].include?(zone.name) } if options[:only].any?
  zones
end

#show_plan(queries) ⇒ Object



49
50
51
52
53
54
# File 'lib/dslimple/applier.rb', line 49

def show_plan(queries)
  shell.say('Changes', :bold)
  queries.each do |query|
    show_query(query)
  end
end

#show_query(query) ⇒ Object



64
65
66
# File 'lib/dslimple/applier.rb', line 64

def show_query(query)
  shell.say("#{shell.set_color(query.operation.to_s[0..2], OPERATION_COLORS[query.operation])} #{query}")
end