Class: OpenHood::Rake::ConsoleTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/openhood/rake/console.rb

Instance Method Summary collapse

Constructor Details

#initializeConsoleTask

Returns a new instance of ConsoleTask.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openhood/rake/console.rb', line 26

def initialize
  desc "Pseudo console"
  task :console do
    connection
    require "readline"
    require "bond"
    puts "Enable completion..."
    require "bond/completion"

    # read history
    history_file = File.join(ENV["HOME"], ".myrb_history")
    if File.exists?(history_file)
      puts "Restore history..."
      IO.readlines(history_file).each{|e| Readline::HISTORY << e.chomp}
    end

    # load models
    puts "Load models..."
    Dir.glob(File.join(base_path, "models", "*.rb")).each do |m|
      require m
    end

    # eval input
    puts ""
    while (input = Readline.readline('>> ', true)) != "exit"
      begin puts "=> #{eval(input).inspect}"; rescue Exception; puts "Error: #{$!}" end
    end
    puts ""

    # write back to history
    puts "Save history..."
    File.open(history_file, "w") {|f| f.write Readline::HISTORY.to_a.join("\n") }
  end
end

Instance Method Details

#base_pathObject



9
10
11
# File 'lib/openhood/rake/console.rb', line 9

def base_path
  File.expand_path('.')
end

#configObject



17
18
19
20
# File 'lib/openhood/rake/console.rb', line 17

def config
  environment = ENV["RACK_ENV"] || "development"
  @config ||= YAML.load_file(File.join(base_path, "config", "#{environment}.yml"))
end

#connectionObject



22
23
24
# File 'lib/openhood/rake/console.rb', line 22

def connection
  Sequel.connect(config[:db][:uri])
end

#pathObject



13
14
15
# File 'lib/openhood/rake/console.rb', line 13

def path
  File.join(base_path, "migrations")
end