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



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sqltorial/assemble_command.rb', line 8

def execute
  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

#filesObject



30
31
32
# File 'lib/sqltorial/assemble_command.rb', line 30

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

#files_from_fileObject



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

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



26
27
28
# File 'lib/sqltorial/assemble_command.rb', line 26

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

#process_dirObject



22
23
24
# File 'lib/sqltorial/assemble_command.rb', line 22

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