Class: DataPitcher::Command

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Command.



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

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

Instance Method Details

#dry_runObject



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

def dry_run
  puts dry_run_log
  false
end

#dry_run_logObject



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

def dry_run_log
  "#\#{@index} command\n  spreadsheet_key: \#{@spreadsheet_key}\n  worksheet_title: \#{@worksheet_title || '(first worksheet)'}\nvalid?: \#{spreadsheet.valid?}\n  sql_path: \#{@sql_path}\nvalid?: \#{executor.valid?}\n  EOS\nend\n"

#executeObject



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

def execute
  @dry_run ? dry_run : run
end

#executorObject



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

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

#runObject



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

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



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

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

#sql_queryObject



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

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