Class: SQLtorial::AssembleCommand

Inherits:
Escort::ActionCommand::Base
  • Object
show all
Includes:
Sequelizer
Defined in:
lib/sqltorial/assemble_command.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



9
10
11
# File 'lib/sqltorial/assemble_command.rb', line 9

def execute
  global_options[:watch] ? watch : process
end

#filesObject



44
45
46
# File 'lib/sqltorial/assemble_command.rb', line 44

def files
  path.directory? ? Pathname.glob('*.sql') : files_from_file
end

#files_from_fileObject



48
49
50
51
52
# File 'lib/sqltorial/assemble_command.rb', line 48

def files_from_file
  path.readlines.map(&:chomp!).select { |l| l !~ /^\s*#/ && !l.empty? }.map do |file_name|
    Pathname.new(file_name)
  end
end

#pathObject



40
41
42
# File 'lib/sqltorial/assemble_command.rb', line 40

def path
  @path ||= Pathname.new(arguments.first || ".")
end

#processObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sqltorial/assemble_command.rb', line 22

def process
  process_dir.chdir do
    preface = Pathname.new(global_options[:preface]) if global_options[:preface]
    File.open(global_options[:output], 'w') do |f|
      f.puts preface.read if preface && preface.exist?
      examples = files.map.with_index do |file, index|
        Escort::Logger.output.puts "Examplizing #{file.to_s}"
        SqlToExample.new(file, db, index + 1).to_str(!global_options[:no_results])
      end
      f.puts(examples.join("\n\n"))
    end
  end
end

#process_dirObject



36
37
38
# File 'lib/sqltorial/assemble_command.rb', line 36

def process_dir
  @process_dir = path.directory? ? path : Pathname.pwd
end

#watchObject



13
14
15
16
17
18
19
20
# File 'lib/sqltorial/assemble_command.rb', line 13

def watch
  listener = Listen.to(path) do |modified, added, removed|
    process
   end
  listener.only(/\.sql$/)
  listener.start
  sleep while listener.processing?
end