Class: Xmlss::UndiesWriter
- Inherits:
-
Object
- Object
- Xmlss::UndiesWriter
- Defined in:
- lib/xmlss/undies_writer.rb
Constant Summary collapse
- XML_NS =
"xmlns"- SHEET_NS =
"ss"- NS_URI =
"urn:schemas-microsoft-com:office:spreadsheet"
Instance Attribute Summary collapse
-
#element_markup ⇒ Object
readonly
Returns the value of attribute element_markup.
-
#style_markup ⇒ Object
readonly
Returns the value of attribute style_markup.
Class Method Summary collapse
-
.attributes(thing, *attrs) ⇒ Object
The Undies writer is responsible for driving the Undies API to generate the xmlss xml markup for the workbook.
- .classify(underscored_string) ⇒ Object
- .coerce(value) ⇒ Object
Instance Method Summary collapse
-
#alignment(alignment, &block) ⇒ Object
workbook style markup directives.
- #border(border, &block) ⇒ Object
- #borders(&block) ⇒ Object
- #cell(cell, &block) ⇒ Object
- #column(column, &block) ⇒ Object
-
#data(data, &block) ⇒ Object
workbook element markup directives.
- #flush ⇒ Object
- #font(font, &block) ⇒ Object
-
#initialize(output_opts = {}) ⇒ UndiesWriter
constructor
A new instance of UndiesWriter.
- #interior(interior, &block) ⇒ Object
- #number_format(number_format, &block) ⇒ Object
- #protection(protection, &block) ⇒ Object
- #row(row, &block) ⇒ Object
- #style(style, &block) ⇒ Object
-
#workbook ⇒ Object
return the full workbook markup, combining the buffers to xmlss spec.
- #worksheet(worksheet, &block) ⇒ Object
Constructor Details
#initialize(output_opts = {}) ⇒ UndiesWriter
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/xmlss/undies_writer.rb', line 49 def initialize(output_opts={}) @opts = output_opts || {} # buffer style markup and create a template to write to it @style_markup = "" styles_out = Undies::Output.new(StringIO.new(@style_markup), @opts.merge({ :pp_level => 2 })) @styles_t = Undies::Template.new(styles_out) # buffer worksheet markup and create a template to write to it @element_markup = "" worksheets_out = Undies::Output.new(StringIO.new(@element_markup), @opts.merge({ :pp_level => 1 })) @worksheets_t = Undies::Template.new(worksheets_out) end |
Instance Attribute Details
#element_markup ⇒ Object (readonly)
Returns the value of attribute element_markup.
47 48 49 |
# File 'lib/xmlss/undies_writer.rb', line 47 def element_markup @element_markup end |
#style_markup ⇒ Object (readonly)
Returns the value of attribute style_markup.
47 48 49 |
# File 'lib/xmlss/undies_writer.rb', line 47 def style_markup @style_markup end |
Class Method Details
.attributes(thing, *attrs) ⇒ Object
The Undies writer is responsible for driving the Undies API to generate the xmlss xml markup for the workbook. Because order doesn’t matter when defining style and worksheet elements, the writer has to buffer the style and worksheet markup separately, then put them together according to Xmlss spec to build the final workbook markup.
18 19 20 21 22 23 24 25 26 |
# File 'lib/xmlss/undies_writer.rb', line 18 def self.attributes(thing, *attrs) [*attrs].flatten.inject({}) do |xattrs, a| xattrs.merge(if !(xv = self.coerce(thing.send(a))).nil? {"#{SHEET_NS}:#{self.classify(a)}" => xv.to_s} else {} end) end end |
.classify(underscored_string) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/xmlss/undies_writer.rb', line 28 def self.classify(underscored_string) underscored_string. to_s.downcase. split("_"). collect{|part| part.capitalize}. join('') end |
.coerce(value) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/xmlss/undies_writer.rb', line 36 def self.coerce(value) if value == true 1 elsif ["",false].include?(value) # don't include false or empty string values nil else value end end |
Instance Method Details
#alignment(alignment, &block) ⇒ Object
workbook style markup directives
94 95 96 97 98 |
# File 'lib/xmlss/undies_writer.rb', line 94 def alignment(alignment, &block) @styles_t._Alignment(self.class.attributes(alignment, [ :horizontal, :vertical, :wrap_text, :rotate ])) end |
#border(border, &block) ⇒ Object
100 101 102 103 104 |
# File 'lib/xmlss/undies_writer.rb', line 100 def border(border, &block) @styles_t._Border(self.class.attributes(border, [ :color, :position, :weight, :line_style ])) end |
#borders(&block) ⇒ Object
106 107 108 |
# File 'lib/xmlss/undies_writer.rb', line 106 def borders(&block) @styles_t._Borders(&block) end |
#cell(cell, &block) ⇒ Object
147 148 149 150 151 |
# File 'lib/xmlss/undies_writer.rb', line 147 def cell(cell, &block) @worksheets_t._Cell(self.class.attributes(cell, [ :index, :style_i_d, :formula, :h_ref, :merge_across, :merge_down ]), &block) end |
#column(column, &block) ⇒ Object
159 160 161 162 163 |
# File 'lib/xmlss/undies_writer.rb', line 159 def column(column, &block) @worksheets_t._Column(self.class.attributes(column, [ :style_i_d, :width, :auto_fit_width, :hidden ])) end |
#data(data, &block) ⇒ Object
workbook element markup directives
141 142 143 144 145 |
# File 'lib/xmlss/undies_writer.rb', line 141 def data(data, &block) @worksheets_t._Data(self.class.attributes(data, :type)) { @worksheets_t.__ data.xml_value } end |
#flush ⇒ Object
67 68 69 70 71 |
# File 'lib/xmlss/undies_writer.rb', line 67 def flush Undies::Template.flush(@worksheets_t) Undies::Template.flush(@styles_t) self end |
#font(font, &block) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/xmlss/undies_writer.rb', line 110 def font(font, &block) @styles_t._Font(self.class.attributes(font, [ :bold, :color, :italic, :size, :shadow, :font_name, :strike_through, :underline, :vertical_align ])) end |
#interior(interior, &block) ⇒ Object
117 118 119 120 121 |
# File 'lib/xmlss/undies_writer.rb', line 117 def interior(interior, &block) @styles_t._Interior(self.class.attributes(interior, [ :color, :pattern, :pattern_color ])) end |
#number_format(number_format, &block) ⇒ Object
123 124 125 126 127 |
# File 'lib/xmlss/undies_writer.rb', line 123 def number_format(number_format, &block) @styles_t._NumberFormat(self.class.attributes(number_format, [ :format ])) end |
#protection(protection, &block) ⇒ Object
129 130 131 132 133 |
# File 'lib/xmlss/undies_writer.rb', line 129 def protection(protection, &block) @styles_t._Protection(self.class.attributes(protection, [ :protect ])) end |
#row(row, &block) ⇒ Object
153 154 155 156 157 |
# File 'lib/xmlss/undies_writer.rb', line 153 def row(row, &block) @worksheets_t._Row(self.class.attributes(row, [ :style_i_d, :height, :auto_fit_height, :hidden ]), &block) end |
#style(style, &block) ⇒ Object
135 136 137 |
# File 'lib/xmlss/undies_writer.rb', line 135 def style(style, &block) @styles_t._Style(self.class.attributes(style, :i_d), &block) end |
#workbook ⇒ Object
return the full workbook markup, combining the buffers to xmlss spec
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/xmlss/undies_writer.rb', line 74 def workbook self.flush "".tap do |markup| Undies::Template.new(Undies::Source.new(Proc.new do __ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" _Workbook(XML_NS => NS_URI, "#{XML_NS}:#{SHEET_NS}" => NS_URI) { _Styles { ___ style_markup.to_s.strip } __ element_markup.to_s.strip } end), { :style_markup => @style_markup, :element_markup => @element_markup }, Undies::Output.new(StringIO.new(markup), @opts)) end.strip end |
#worksheet(worksheet, &block) ⇒ Object
165 166 167 168 169 |
# File 'lib/xmlss/undies_writer.rb', line 165 def worksheet(worksheet, &block) @worksheets_t._Worksheet(self.class.attributes(worksheet, :name)) { @worksheets_t._Table(&block) } end |