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

#dirObject



42
43
44
# File 'lib/sqltorial/assemble_command.rb', line 42

def dir
  @dir ||= path.directory? ? path : path.dirname
end

#executeObject



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

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

#filesObject



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

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

#files_from_fileObject



54
55
56
57
58
# File 'lib/sqltorial/assemble_command.rb', line 54

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



46
47
48
# File 'lib/sqltorial/assemble_command.rb', line 46

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

#processObject



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

def process
  FileUtils.rm_rf(".sqltorial_cache") if global_options[:ignore_cache]
  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)
      end
      f.puts(examples.join("\n\n"))
    end
  end
end

#process_dirObject



38
39
40
# File 'lib/sqltorial/assemble_command.rb', line 38

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

#watchObject



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

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