class Squill::SquillFile

Attributes

description[RW]
name[RW]
sql[RW]

Public Class Methods

new(name) click to toggle source
# File lib/squill/squill_file.rb, line 6
def initialize(name)
  ensure_squill_dir
  @name = name
  if exists_as_squill_file?
    load_from_squill_file
  end
end

Public Instance Methods

delete() click to toggle source
# File lib/squill/squill_file.rb, line 42
def delete
  File.delete(squill_file)
end
exists_as_squill_file?() click to toggle source
# File lib/squill/squill_file.rb, line 22
def exists_as_squill_file?
  File.exist?(squill_file)
end
load_from_squill_file() click to toggle source
# File lib/squill/squill_file.rb, line 14
def load_from_squill_file
  orig_file = File.open(squill_file, "r")
  content = orig_file.read
  @description = content.match(/#Description: (.+?)\n/m)[1]
  @sql = content.match(/#SQL:\n(.+?)#ENDSQL;/m)[1]
  orig_file.close
end
save() click to toggle source
# File lib/squill/squill_file.rb, line 38
def save
  write_file
end
set_sql() click to toggle source
# File lib/squill/squill_file.rb, line 32
def set_sql
  prep_tempfile_for_sql
  set_tempfile
  @sql = read_tempfile_for_sql
end
set_sql_from_file(filename) click to toggle source
# File lib/squill/squill_file.rb, line 26
def set_sql_from_file(filename)
  orig_file = File.open(filename, "r")
  @sql = orig_file.read
  orig_file.close
end
yaml() click to toggle source
# File lib/squill/squill_file.rb, line 46
def yaml
  { name: @name, description: @description, sql: @sql }
end

Private Instance Methods

contents_for_squill_file_write() click to toggle source
# File lib/squill/squill_file.rb, line 83
    def contents_for_squill_file_write
      <<-OUTPUT_FILE
#Name: #{@name}
#Description: #{@description}
#SQL:
#{@sql}
#ENDSQL;
      OUTPUT_FILE
    end
ensure_squill_dir() click to toggle source
# File lib/squill/squill_file.rb, line 97
def ensure_squill_dir
  if !File.directory?(squill_dir)
    Dir.mkdir(squill_dir)
  end
end
prep_tempfile_for_sql() click to toggle source
# File lib/squill/squill_file.rb, line 52
    def prep_tempfile_for_sql
      file = File.open(tempfile, "w")
      file.write <<-SQL_TEMPLATE

#ENDSQL;
# Insert SQL to be added to squill above these commented lines
      SQL_TEMPLATE
      file.close
    end
read_tempfile_for_sql() click to toggle source
# File lib/squill/squill_file.rb, line 62
def read_tempfile_for_sql
  file = File.open(tempfile, "r")
  sql = ""
  file.each_line { |line|
    break if line.match(/^#ENDSQL;/)
    sql << line
  }
  file.close
  sql
end
set_tempfile() click to toggle source
# File lib/squill/squill_file.rb, line 73
def set_tempfile
  system(user_editor, tempfile)
end
squill_dir() click to toggle source
# File lib/squill/squill_file.rb, line 111
def squill_dir
  File.join(File.expand_path('~'),'.squill')
end
squill_file() click to toggle source
# File lib/squill/squill_file.rb, line 107
def squill_file
  File.join(squill_dir, "#{name}.squill")
end
tempfile() click to toggle source
# File lib/squill/squill_file.rb, line 103
def tempfile
  File.join(squill_dir, '.temp')
end
user_editor() click to toggle source
# File lib/squill/squill_file.rb, line 93
def user_editor
  !ENV['EDITOR'].nil? && !ENV['EDITOR'].empty? ? ENV['EDITOR'] : 'vi'
end
write_file() click to toggle source
# File lib/squill/squill_file.rb, line 77
def write_file
  file = File.open(squill_file, "w")
  file.write(contents_for_squill_file_write)
  file.close
end