Class: DataPitcher::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/data_pitcher/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(spreadsheet_key, sql_path, dry_run: true, index: 1) ⇒ Command

Returns a new instance of Command.



3
4
5
6
7
8
# File 'lib/data_pitcher/command.rb', line 3

def initialize(spreadsheet_key, sql_path, dry_run: true, index: 1)
  @spreadsheet_key = spreadsheet_key
  @sql_path = sql_path
  @dry_run = dry_run
  @index = index
end

Instance Method Details

#dry_runObject



14
15
16
17
# File 'lib/data_pitcher/command.rb', line 14

def dry_run
  puts dry_run_log
  false
end

#dry_run_logObject



44
45
46
47
48
49
50
51
52
# File 'lib/data_pitcher/command.rb', line 44

def dry_run_log
  <<-EOS
##{@index} command
  spreadsheet_key: #{@spreadsheet_key}
valid?: #{spreadsheet.valid?}
  sql_path: #{@sql_path}
valid?: #{executor.valid?}
  EOS
end

#executeObject



10
11
12
# File 'lib/data_pitcher/command.rb', line 10

def execute
  @dry_run ? dry_run : run
end

#executorObject



36
37
38
# File 'lib/data_pitcher/command.rb', line 36

def executor
  @executor ||= DataPitcher::Executor.new(sql_query)
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/data_pitcher/command.rb', line 19

def run
  unless spreadsheet.valid?
    puts "[ERROR] ##{@index} command skipped: DataPitcher can not access to spreadsheet (#{@spreadsheet_key})"
    return false
  end
  unless executor.valid?
    puts "[ERROR] ##{@index} command skipped: SQL is invalid (#{@sql_path})"
    return false
  end
  spreadsheet.replace_worksheet_with_query(sql_query)
  true
end

#spreadsheetObject



32
33
34
# File 'lib/data_pitcher/command.rb', line 32

def spreadsheet
  @spreadsheet ||= DataPitcher::Spreadsheet.new(@spreadsheet_key)
end

#sql_queryObject



40
41
42
# File 'lib/data_pitcher/command.rb', line 40

def sql_query
  @sql_query ||= File.read(@sql_path)
end