Class: RODF::Document

Inherits:
Container show all
Defined in:
lib/rodf/document.rb

Direct Known Subclasses

Spreadsheet, Text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Container

create, #initialize

Constructor Details

This class inherits a constructor from RODF::Container

Class Method Details

.file(ods_file_name, &contents) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/rodf/document.rb', line 3

def self.file(ods_file_name, &contents)
  doc = new

  if contents.arity.zero?
    contents.call(doc)
  else
    doc.instance_exec(doc, &contents)
  end

  doc.write_to ods_file_name
end

Instance Method Details

#add_default_styles(*elements) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/rodf/document.rb', line 87

def add_default_styles(*elements)
  if elements.first.is_a?(Array)
    elements = elements.first
  end

  elements.each do |element|
    default_style(element)
  end
end

#add_office_styles(*elements) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/rodf/document.rb', line 113

def add_office_styles(*elements)
  if elements.first.is_a?(Array)
    elements = elements.first
  end

  elements.each do |element|
    office_style(element)
  end
end

#add_styles(*elements) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/rodf/document.rb', line 61

def add_styles(*elements)
  if elements.first.is_a?(Array)
    elements = elements.first
  end

  elements.each do |element|
    style(element)
  end
end

#bytesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rodf/document.rb', line 19

def bytes
  buffer = Zip::OutputStream::write_buffer do |zio|
    zio.put_next_entry('META-INF/manifest.xml')

    zio << self.class.skeleton.manifest(self.class.doc_type)

    zio.put_next_entry('styles.xml')

    zio << self.class.skeleton.styles

    zio << self.office_styles_xml unless self.office_styles.empty?

    zio << "</office:styles> </office:document-styles>"

    zio.put_next_entry('content.xml')

    zio << self.xml
  end

  buffer.set_encoding('ASCII-8BIT')

  buffer.rewind

  buffer.sysread
end

#default_style(*args, &block) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/rodf/document.rb', line 75

def default_style(*args, &block)
  x = DefaultStyle.new(*args, &block)

  default_styles << x

  return x
end

#default_stylesObject



71
72
73
# File 'lib/rodf/document.rb', line 71

def default_styles
  @default_styles ||= []
end

#default_styles_xmlObject



83
84
85
# File 'lib/rodf/document.rb', line 83

def default_styles_xml
  default_styles.map(&:xml).join
end

#office_style(*args, &block) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/rodf/document.rb', line 101

def office_style(*args, &block)
  x = OfficeStyle.new(*args, &block)

  office_styles << x

  return x
end

#office_stylesObject



97
98
99
# File 'lib/rodf/document.rb', line 97

def office_styles
  @office_styles ||= []
end

#office_styles_xmlObject



109
110
111
# File 'lib/rodf/document.rb', line 109

def office_styles_xml
  office_styles.map(&:xml).join
end

#style(*args, &block) ⇒ Object



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

def style(*args, &block)
  x = Style.new(*args, &block)

  styles << x

  return x
end

#stylesObject



45
46
47
# File 'lib/rodf/document.rb', line 45

def styles
  @styles ||= []
end

#styles_xmlObject



57
58
59
# File 'lib/rodf/document.rb', line 57

def styles_xml
  styles.map(&:xml).join
end

#write_to(ods_file_name) ⇒ Object



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

def write_to(ods_file_name)
  File.open(ods_file_name, 'wb') { |f| f << self.bytes }
end