Class: Oci8Simple::Cli

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/oci8_simple/cli.rb

Overview

Description

A very thin wrapper around Oci8Simple::Client that handles ARGV / options and formats the output in a manner suitable for printing on the console

Usage

cli = Oci8Simple::Cli.new
cli.run "select id, name from foos" # "3, Bacon\n5, Cheese Puffs\n..."

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Command

included

Constructor Details

#initialize(env = nil) ⇒ Cli

Returns a new instance of Cli.



13
14
15
# File 'lib/oci8_simple/cli.rb', line 13

def initialize(env=nil)
  self.env = env
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/oci8_simple/cli.rb', line 11

def client
  @client
end

#envObject

Returns the value of attribute env.



11
12
13
# File 'lib/oci8_simple/cli.rb', line 11

def env
  @env
end

Class Method Details

.run_from_argvObject



37
38
39
40
41
42
43
44
# File 'lib/oci8_simple/cli.rb', line 37

def self.run_from_argv
  o = parse_options(self.usage)
  if(ARGV[0].nil?)
    puts o
  else
    puts self.new(@options[:environment]).run(ARGV[0], @options)
  end
end

.usageObject



33
34
35
# File 'lib/oci8_simple/cli.rb', line 33

def self.usage
  "Usage: #{$0} [-e ENV] \"SQL\""
end

Instance Method Details

#format(arr, options) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/oci8_simple/cli.rb', line 21

def format(arr, options)
  if(options[:hash])
    arr.map{|row| row.map{|k,v| "#{k}: #{v}"}.join("\n")}.join("\n\n")
  else
    arr.map{|row| row.join(", ")}.join("\n")
  end
end

#run(sql, options = {}) ⇒ Object



17
18
19
# File 'lib/oci8_simple/cli.rb', line 17

def run(sql, options={})
  format(client.run(sql, options), options)
end