Class: Cuker::AbstractWriter

Inherits:
Object
  • Object
show all
Includes:
IWriter, LoggerSetup
Defined in:
lib/cuker/helpers/writers/abstract_writer.rb

Direct Known Subclasses

CsvWriter, JiraWriter, RubyXLWriter

Constant Summary collapse

NoNewFileMadeError =
Class.new IOError

Instance Attribute Summary collapse

Attributes included from LoggerSetup

#log

Instance Method Summary collapse

Methods included from LoggerSetup

#init_logger, reset_appender_log_levels

Methods included from Interface

#method

Constructor Details

#initializeAbstractWriter

Returns a new instance of AbstractWriter.



28
29
30
31
32
33
34
35
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 28

def initialize
  init_logger
  # @out_dir = output_path
  # FileUtils.mkdir_p(output_path) unless Dir.exist? output_path
  @log.debug "initing AbstractWriter"
  @active_file = nil
  @book = {}
end

Instance Attribute Details

#active_fileObject

Returns the value of attribute active_file.



25
26
27
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 25

def active_file
  @active_file
end

#active_file_nameObject

Active file name



23
24
25
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 23

def active_file_name
  @active_file_name
end

#bookObject

Returns the value of attribute book.



25
26
27
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 25

def book
  @book
end

#extObject

File Extension



20
21
22
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 20

def ext
  @ext
end

#out_dirObject (readonly)

Returns the value of attribute out_dir.



26
27
28
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 26

def out_dir
  @out_dir
end

Instance Method Details

#finishupObject



56
57
58
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 56

def finishup
  @log.debug "closing up #{@name} file if needed"
end

#make_name(name) ⇒ String

Returns name with ext path added.

Returns:

  • (String)

    name with ext path added



66
67
68
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 66

def make_name name
  "#{new_name name}#{@ext}"
end

#make_new_file(name) ⇒ Object



60
61
62
63
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 60

def make_new_file name
  @log.debug "make a new abstract file: #{name}#{@ext}"
  make_name name
end

#make_new_sheet(name = nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 49

def make_new_sheet name = nil
  sheet = new_name name
  @log.debug "make a new abstract sheet: #{sheet}"
  sheet
  # todo: file name collision handle!!
end

#new_name(name) ⇒ Object



70
71
72
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 70

def new_name name
  name.nil? ? "Sheet_#{@book.size + 1}" : name
end

#raise_unless_active_loc(data) ⇒ Object



45
46
47
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 45

def raise_unless_active_loc data
  raise NoNewFileMadeError.new "Please run 'make_new_file' before trying to write: '#{data}'" if @active_file.nil?
end

#write(model, output_file_path) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 74

def write model, output_file_path
  file_name = make_new_file output_file_path
  write_title model.title
  model.data.each {|row| write_new_row row}
  finishup
  file_name
end

#write_new_row(data) ⇒ Object



41
42
43
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 41

def write_new_row data
  raise_unless_active_loc data
end

#write_title(data) ⇒ Object



37
38
39
# File 'lib/cuker/helpers/writers/abstract_writer.rb', line 37

def write_title data
  raise_unless_active_loc data
end