Class: Mysqlexport::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/mysqlexport/writer.rb

Direct Known Subclasses

Csv, Json

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Writer

Returns a new instance of Writer.



5
6
7
8
9
# File 'lib/mysqlexport/writer.rb', line 5

def initialize(options = {})
  validate!(options)
  @config = Mysqlexport::Config.new options
  @client = @config.client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/mysqlexport/writer.rb', line 3

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/mysqlexport/writer.rb', line 3

def config
  @config
end

#sqlObject (readonly)

Returns the value of attribute sql.



3
4
5
# File 'lib/mysqlexport/writer.rb', line 3

def sql
  @sql
end

Instance Method Details

#filter_path(path) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/mysqlexport/writer.rb', line 16

def filter_path(path)
  path = ::Dir.pwd.to_s unless path.instance_of? ::String
  file_name = "/#{::Time.now.to_i}_mysqlexport.#{extension}" if ::File.directory?(path)
  file_name = "/#{config.table}.#{extension}" if ::File.directory?(path) && !config.table.nil?
  path += file_name.to_s
  path
end

#to_path(path = config.output_path) ⇒ Object



24
25
26
27
28
29
# File 'lib/mysqlexport/writer.rb', line 24

def to_path(path = config.output_path)
  f = ::File.open(filter_path(path), "w")
  to_file f
  f.close
  nil
end

#to_sObject



31
32
33
34
35
36
# File 'lib/mysqlexport/writer.rb', line 31

def to_s
  s = ::StringIO.new
  to_file s
  s.rewind
  s.read
end

#to_stdoutObject



11
12
13
14
# File 'lib/mysqlexport/writer.rb', line 11

def to_stdout
  to_file $stdout
  nil
end