summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/stylesheet/table_style.rb
blob: 3184c042aca83c159c2b2f675bc208457397bdeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# encoding: UTF-8
module Axlsx
  # A single table style definition and is a collection for tableStyleElements
  # @note Table are not supported in this version and only the defaults required for a valid workbook are created.
  class TableStyle < SimpleTypedList

    # The name of this table style
    # @return [string]
    attr_reader :name

    # indicates if this style should be applied to pivot tables
    # @return [Boolean]
    attr_reader :pivot

    # indicates if this style should be applied to tables
    # @return [Boolean]
    attr_reader :table

    # creates a new TableStyle object
    # @raise [ArgumentError] if name option is not provided.
    # @param [String] name
    # @option options [Boolean] pivot
    # @option options [Boolean] table
    def initialize(name, options={})
      self.name = name
      options.each do |o|
        self.send("#{o[0]}=", o[1]) if self.respond_to? o[0]
      end
      super TableStyleElement
    end

    # @see name
    def name=(v) Axlsx::validate_string v; @name=v end
    # @see pivot
    def pivot=(v) Axlsx::validate_boolean v; @pivot=v end
    # @see table
    def table=(v) Axlsx::validate_boolean v; @table=v end


    def to_xml_string(str = '')
      attr = self.instance_values.select { |k, v| [:name, :pivot, :table].include? k }
      attr[:count] = self.size
      str << '<tableStyle '
      str << attr.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ')
      str << '>'
      each { |table_style_el| table_style_el.to_xml_string(str) }
      str << '</tableStyle>'
    end

    # Serializes the table style
    # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
    # @return [String]
    def to_xml(xml)
      attr = self.instance_values.select { |k, v| [:name, :pivot, :table].include? k }
      attr[:count] = self.size
      xml.tableStyle(attr) { self.each { |table_style_el| table_style_el.to_xml(xml) } }
    end
  end
end