Class: XlsxWriter::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsx_writer/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



21
22
23
24
25
26
27
28
29
30
# File 'lib/xlsx_writer/document.rb', line 21

def initialize
  staging_dir = ::UnixUtils.tmp_path 'xlsx_writer'
  ::FileUtils.mkdir_p staging_dir
  @staging_dir = staging_dir
  @sheets = []
  @images = []
  @page_setup = PageSetup.new
  @header_footer = HeaderFooter.new
  @mutex = ::Mutex.new
end

Instance Attribute Details

Returns the value of attribute header_footer.



19
20
21
# File 'lib/xlsx_writer/document.rb', line 19

def header_footer
  @header_footer
end

#imagesObject (readonly)

Returns the value of attribute images.



17
18
19
# File 'lib/xlsx_writer/document.rb', line 17

def images
  @images
end

#page_setupObject (readonly)

Returns the value of attribute page_setup.



18
19
20
# File 'lib/xlsx_writer/document.rb', line 18

def page_setup
  @page_setup
end

#sheetsObject (readonly)

Returns the value of attribute sheets.



16
17
18
# File 'lib/xlsx_writer/document.rb', line 16

def sheets
  @sheets
end

#staging_dirObject (readonly)

Returns the value of attribute staging_dir.



15
16
17
# File 'lib/xlsx_writer/document.rb', line 15

def staging_dir
  @staging_dir
end

Class Method Details

.autoObject



6
7
8
9
10
11
12
# File 'lib/xlsx_writer/document.rb', line 6

def auto
  ::Dir[::File.expand_path('../generators/*.rb', __FILE__)].map do |path|
    XlsxWriter.const_get ::File.basename(path, '.rb').camelcase
  end.reject do |klass|
    klass.const_defined?(:AUTO) and klass.const_get(:AUTO) == false
  end
end

Instance Method Details

#add_image(path, width, height) ⇒ Object

Raises:

  • (::RuntimeError)


50
51
52
53
54
55
# File 'lib/xlsx_writer/document.rb', line 50

def add_image(path, width, height)
  raise ::RuntimeError, "Can't add image, already generated!" if generated?
  image = Image.new self, path, width, height
  images << image
  image
end

#add_sheet(name) ⇒ Object

Raises:

  • (::RuntimeError)


41
42
43
44
45
46
# File 'lib/xlsx_writer/document.rb', line 41

def add_sheet(name)
  raise ::RuntimeError, "Can't add sheet, already generated!" if generated?
  sheet = Sheet.new self, name
  sheets << sheet
  sheet
end

#cleanupObject



74
75
76
77
# File 'lib/xlsx_writer/document.rb', line 74

def cleanup
  ::FileUtils.rm_rf @staging_dir.to_s
  ::FileUtils.rm_f @path.to_s
end

#generateObject



79
80
81
82
# File 'lib/xlsx_writer/document.rb', line 79

def generate
  path
  true
end

#generated?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/xlsx_writer/document.rb', line 84

def generated?
  @generated == true
end

#pathObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xlsx_writer/document.rb', line 57

def path
  @path || @mutex.synchronize do
    @path ||= begin 
      sheets.each(&:generate)
      images.each(&:generate)
      Document.auto.each do |part|
        part.new(self).generate
      end
      with_zip_extname = ::UnixUtils.zip staging_dir
      with_xlsx_extname = with_zip_extname.sub(/.zip$/, '.xlsx')
      ::FileUtils.mv with_zip_extname, with_xlsx_extname
      @generated = true
      with_xlsx_extname
    end
  end
end

#quiet_booleans!Object

Instead of TRUE or FALSE, show TRUE and blank if false



33
34
35
# File 'lib/xlsx_writer/document.rb', line 33

def quiet_booleans!
  @quiet_booleans = true
end

#quiet_booleans?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/xlsx_writer/document.rb', line 37

def quiet_booleans?
  @quiet_booleans == true
end