Class: DbPlot

Inherits:
Object
  • Object
show all
Defined in:
lib/dbplot.rb,
lib/dbplot/runner.rb

Defined Under Namespace

Modules: Runner

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DbPlot

Returns a new instance of DbPlot.



25
26
27
28
29
# File 'lib/dbplot.rb', line 25

def initialize(options = {})
  %w(host database username password).proxy(:map).to_sym.each do |setting|
    settings[setting] = options[setting] if options[setting]
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/dbplot.rb', line 8

def data
  @data
end

#debugObject

Returns the value of attribute debug.



8
9
10
# File 'lib/dbplot.rb', line 8

def debug
  @debug
end

#settingsObject

Returns the value of attribute settings.



8
9
10
# File 'lib/dbplot.rb', line 8

def settings
  @settings
end

#stringObject

Returns the value of attribute string.



8
9
10
# File 'lib/dbplot.rb', line 8

def string
  @string
end

Class Method Details

.versionObject



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

def self.version
  "DbPlot v" + version_string
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/dbplot.rb', line 72

def complete?
  @string =~ /;\s*$/
end

#executeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dbplot.rb', line 35

def execute
  return unless complete?
  
  @query = %{SELECT #{select_string} FROM #{@table}}
  
  template = ERB.new(File.read(template_file)).result(binding)
  
  puts template if debug
  
  unless settings[:dry_run]
    `echo \"#{template.gsub('"', '\\"')}\" | r --no-save #{"2>&1 > /dev/null" unless debug}`
  end
  @string = nil
end

#parseObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dbplot.rb', line 50

def parse
  if legal_expression?
    set_defaults
    set_file
    set_color_by
    set_facet_by
  else
    puts "\ncould not parse: \"#{string}\"\n\n"
    @string = nil
  end
  
  self
end

#parse_line(string) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/dbplot.rb', line 64

def parse_line(string)
  return self if string.strip == ""
  @string ||= ""
  @string += " #{string}"
  parse if complete?
  return self
end

#promptObject



77
78
79
# File 'lib/dbplot.rb', line 77

def prompt
  @string.nil? ? 'dbplot> ' : '     -> '
end

#template_fileObject



31
32
33
# File 'lib/dbplot.rb', line 31

def template_file
  settings[:template_file] || File.instance_eval {expand_path(join(dirname(__FILE__), 'dbplot', 'template.r.erb'))}
end