Class: BigqueryMigration::ActionRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/bigquery_migration/action_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path = nil, opts = {}) ⇒ ActionRunner

Returns a new instance of ActionRunner.



10
11
12
13
14
15
16
# File 'lib/bigquery_migration/action_runner.rb', line 10

def initialize(config_path = nil, opts = {})
  @config_path = config_path
  @opts = opts
  config = ConfigLoader.new(@config_path, opts[:vars]).load
  @config = HashUtil.deep_symbolize_keys(config)
  validate_config!
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/bigquery_migration/action_runner.rb', line 8

def config
  @config
end

#config_pathObject (readonly)

Returns the value of attribute config_path.



8
9
10
# File 'lib/bigquery_migration/action_runner.rb', line 8

def config_path
  @config_path
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/bigquery_migration/action_runner.rb', line 8

def opts
  @opts
end

Instance Method Details

#runObject



18
19
20
21
# File 'lib/bigquery_migration/action_runner.rb', line 18

def run
  success, responses = run_actions
  { success: success, dry_run: @opts[:dry_run], actions: responses }
end

#run_actionsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bigquery_migration/action_runner.rb', line 23

def run_actions
  success = true
  responses = []

  @config[:actions].each do |action_config|
    _success, result = Action.new(action_config, @opts).run
    response = action_config.merge({'result' => result})
    responses << response
    unless _success
      success = false
      break
    end
  end

  [success, responses]
end

#validate_config!Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bigquery_migration/action_runner.rb', line 40

def validate_config!
  unless config.is_a?(Hash)
    raise ConfigError, "config file format has to be YAML Hash"
  end

  unless config[:actions]
    raise ConfigError, "config must have `actions` key"
  end

  unless config[:actions].is_a?(Array)
    raise ConfigError, "config[:actions] must be an Array"
  end

  config[:actions].each do |action_config|
    unless action_config[:action]
      raise ConfigError, "Elements of `config[:actions]` must have `action` key"
    end
  end
end