diff options
| author | Randy Morgan <[email protected]> | 2012-10-14 12:02:40 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-10-14 12:02:40 +0900 |
| commit | 5b5410845447772f4ba01b2ee5d03907f5897e7a (patch) | |
| tree | ce66d2add8423cf23b1c1a0bc35162c7447075ee /lib | |
| parent | a441bc1ff24c5b238203adb49b6e7ce208d5dabf (diff) | |
| download | caxlsx-5b5410845447772f4ba01b2ee5d03907f5897e7a.tar.gz caxlsx-5b5410845447772f4ba01b2ee5d03907f5897e7a.zip | |
Refactored to use options parser and serialized attributes
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/content_type/default.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/content_type/override.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/doc_props/app.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/drawing/axis.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/drawing/marker.rb | 26 | ||||
| -rw-r--r-- | lib/axlsx/drawing/picture_locking.rb | 56 | ||||
| -rw-r--r-- | lib/axlsx/drawing/scaling.rb | 26 | ||||
| -rw-r--r-- | lib/axlsx/drawing/scatter_chart.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/drawing/series.rb | 9 | ||||
| -rw-r--r-- | lib/axlsx/drawing/str_data.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/drawing/str_val.rb | 15 | ||||
| -rw-r--r-- | lib/axlsx/drawing/view_3D.rb | 36 | ||||
| -rw-r--r-- | lib/axlsx/drawing/vml_shape.rb | 46 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/color.rb | 31 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/font.rb | 44 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/num_fmt.rb | 35 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/pattern_fill.rb | 22 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/table_style.rb | 33 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/table_style_element.rb | 26 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/table_styles.rb | 25 |
20 files changed, 224 insertions, 242 deletions
diff --git a/lib/axlsx/content_type/default.rb b/lib/axlsx/content_type/default.rb index 23406018..2b784ebf 100644 --- a/lib/axlsx/content_type/default.rb +++ b/lib/axlsx/content_type/default.rb @@ -4,15 +4,15 @@ module Axlsx # An default content part. These parts are automatically created by for you based on the content of your package. class Default + include Axlsx::OptionsParser + #Creates a new Default object # @option options [String] extension # @option options [String] content_type # @raise [ArgumentError] An argument error is raised if both extension and content_type are not specified. def initialize(options={}) raise ArgumentError, INVALID_ARGUMENTS unless validate_options(options) - options.each do |name, value| - self.send("#{name}=", value) if self.respond_to? "#{name}=" - end + parse_options options end # Error string for option validation diff --git a/lib/axlsx/content_type/override.rb b/lib/axlsx/content_type/override.rb index d3768ccd..a3947d99 100644 --- a/lib/axlsx/content_type/override.rb +++ b/lib/axlsx/content_type/override.rb @@ -4,15 +4,15 @@ module Axlsx # An override content part. These parts are automatically created by for you based on the content of your package. class Override + include Axlsx::OptionsParser + #Creates a new Override object # @option options [String] PartName # @option options [String] ContentType # @raise [ArgumentError] An argument error is raised if both PartName and ContentType are not specified. def initialize(options={}) raise ArgumentError, INVALID_ARGUMENTS unless validate_options(options) - options.each do |name, value| - self.send("#{name}=", value) if self.respond_to? "#{name}=" - end + parse_options options end # Error message for invalid options diff --git a/lib/axlsx/doc_props/app.rb b/lib/axlsx/doc_props/app.rb index e5d8f8d1..e6774147 100644 --- a/lib/axlsx/doc_props/app.rb +++ b/lib/axlsx/doc_props/app.rb @@ -11,6 +11,8 @@ module Axlsx # DigSig (DigSigBlob) class App + include Axlsx::OptionsParser + # Creates an App object # @option options [String] template # @option options [String] manager @@ -35,9 +37,7 @@ module Axlsx # @option options [String] app_version # @option options [Integer] doc_security def initialize(options={}) - options.each do |name, value| - self.send("#{name}=", value) if self.respond_to? "#{name}=" - end + parse_options options end # @return [String] The name of the document template. diff --git a/lib/axlsx/drawing/axis.rb b/lib/axlsx/drawing/axis.rb index 12e4f481..1b55bece 100644 --- a/lib/axlsx/drawing/axis.rb +++ b/lib/axlsx/drawing/axis.rb @@ -4,6 +4,8 @@ module Axlsx # the access class defines common properties and values for a chart axis. class Axis + include Axlsx::OptionsParser + # Creates an Axis object # @param [Integer] ax_id the id of this axis # @param [Integer] cross_ax the id of the perpendicular axis @@ -25,9 +27,7 @@ module Axlsx self.format_code = "General" self.crosses = :autoZero self.gridlines = true - options.each do |name, value| - self.send("#{name}=", value) if self.respond_to? "#{name}=" - end + parse_options options end # the fill color to use in the axis shape properties. This should be a 6 character long hex string diff --git a/lib/axlsx/drawing/marker.rb b/lib/axlsx/drawing/marker.rb index 8da9477e..2a46e27f 100644 --- a/lib/axlsx/drawing/marker.rb +++ b/lib/axlsx/drawing/marker.rb @@ -5,6 +5,18 @@ module Axlsx # @see Worksheet#add_chart class Marker + include Axlsx::OptionsParser + + # Creates a new Marker object + # @option options [Integer] col + # @option options [Integer] colOff + # @option options [Integer] row + # @option options [Integer] rowOff + def initialize(options={}) + @col, @colOff, @row, @rowOff = 0, 0, 0, 0 + parse_options options + end + # The column this marker anchors to # @return [Integer] attr_reader :col @@ -21,19 +33,7 @@ module Axlsx # @return [Integer] attr_reader :rowOff - # Creates a new Marker object - # @option options [Integer] col - # @option options [Integer] colOff - # @option options [Integer] row - # @option options [Integer] rowOff - def initialize(options={}) - @col, @colOff, @row, @rowOff = 0, 0, 0, 0 - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] - end - end - - # @see col + # @see col def col=(v) Axlsx::validate_unsigned_int v; @col = v end # @see colOff def colOff=(v) Axlsx::validate_int v; @colOff = v end diff --git a/lib/axlsx/drawing/picture_locking.rb b/lib/axlsx/drawing/picture_locking.rb index 0d6f4e54..e77dac37 100644 --- a/lib/axlsx/drawing/picture_locking.rb +++ b/lib/axlsx/drawing/picture_locking.rb @@ -3,17 +3,17 @@ module Axlsx # The picture locking class defines the locking properties for pictures in your workbook. class PictureLocking + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + include Axlsx::Accessors - attr_reader :noGrp - attr_reader :noSelect - attr_reader :noRot - attr_reader :noChangeAspect - attr_reader :noMove - attr_reader :noResize - attr_reader :noEditPoints - attr_reader :noAdjustHandles - attr_reader :noChangeArrowheads - attr_reader :noChangeShapeType + boolean_attr_accessor :noGrp, :noSelect, :noRot, :noChangeAspect, + :noMove, :noResize, :noEditPoints, :noAdjustHandles, + :noChangeArrowheads, :noChangeShapeType + + serializable_attributes :noGrp, :noSelect, :noRot, :noChangeAspect, + :noMove, :noResize, :noEditPoints, :noAdjustHandles, + :noChangeArrowheads, :noChangeShapeType # Creates a new PictureLocking object # @option options [Boolean] noGrp @@ -28,47 +28,15 @@ module Axlsx # @option options [Boolean] noChangeShapeType def initialize(options={}) @noChangeAspect = true - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options end - # @see noGrp - def noGrp=(v) Axlsx::validate_boolean v; @noGrp = v end - - # @see noSelect - def noSelect=(v) Axlsx::validate_boolean v; @noSelect = v end - - # @see noRot - def noRot=(v) Axlsx::validate_boolean v; @noRot = v end - - # @see noChangeAspect - def noChangeAspect=(v) Axlsx::validate_boolean v; @noChangeAspect = v end - - # @see noMove - def noMove=(v) Axlsx::validate_boolean v; @noMove = v end - - # @see noResize - def noResize=(v) Axlsx::validate_boolean v; @noResize = v end - - # @see noEditPoints - def noEditPoints=(v) Axlsx::validate_boolean v; @noEditPoints = v end - - # @see noAdjustHandles - def noAdjustHandles=(v) Axlsx::validate_boolean v; @noAdjustHandles = v end - - # @see noChangeArrowheads - def noChangeArrowheads=(v) Axlsx::validate_boolean v; @noChangeArrowheads = v end - - # @see noChangeShapeType - def noChangeShapeType=(v) Axlsx::validate_boolean v; @noChangeShapeType = v end - # Serializes the object # @param [String] str # @return [String] def to_xml_string(str = '') str << '<a:picLocks ' - str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ') + serialized_attributes str str << '/>' end diff --git a/lib/axlsx/drawing/scaling.rb b/lib/axlsx/drawing/scaling.rb index 29333bc7..b45bd4ce 100644 --- a/lib/axlsx/drawing/scaling.rb +++ b/lib/axlsx/drawing/scaling.rb @@ -3,6 +3,19 @@ module Axlsx # The Scaling class defines axis scaling class Scaling + include Axlsx::OptionsParser + + # creates a new Scaling object + # @option options [Integer, Fixnum] logBase + # @option options [Symbol] orientation + # @option options [Float] max + # @option options [Float] min + def initialize(options={}) + @orientation = :minMax + @logBase, @min, @max = nil, nil, nil + parse_options options + end + # logarithmic base for a logarithmic axis. # must be between 2 and 1000 # @return [Integer] @@ -21,19 +34,6 @@ module Axlsx # @return [Float] attr_reader :min - # creates a new Scaling object - # @option options [Integer, Fixnum] logBase - # @option options [Symbol] orientation - # @option options [Float] max - # @option options [Float] min - def initialize(options={}) - @orientation = :minMax - @logBase, @min, @max = nil, nil, nil - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end - # @see logBase def logBase=(v) DataTypeValidator.validate "Scaling.logBase", [Integer, Fixnum], v, lambda { |arg| arg >= 2 && arg <= 1000}; @logBase = v; end # @see orientation diff --git a/lib/axlsx/drawing/scatter_chart.rb b/lib/axlsx/drawing/scatter_chart.rb index 92ef9cf2..5e106a04 100644 --- a/lib/axlsx/drawing/scatter_chart.rb +++ b/lib/axlsx/drawing/scatter_chart.rb @@ -7,6 +7,8 @@ module Axlsx # @see README for an example class ScatterChart < Chart + include Axlsx::OptionsParser + # The Style for the scatter chart # must be one of :none | :line | :lineMarker | :marker | :smooth | :smoothMarker # return [Symbol] @@ -30,9 +32,7 @@ module Axlsx super(frame, options) @series_type = ScatterSeries @d_lbls = nil - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options end # see #scatterStyle diff --git a/lib/axlsx/drawing/series.rb b/lib/axlsx/drawing/series.rb index 9a4de2d4..c9f467d5 100644 --- a/lib/axlsx/drawing/series.rb +++ b/lib/axlsx/drawing/series.rb @@ -6,6 +6,8 @@ module Axlsx # @see Chart#add_series class Series + include Axlsx::OptionsParser + # The chart that owns this series # @return [Chart] attr_reader :chart @@ -22,19 +24,15 @@ module Axlsx @order = nil self.chart = chart @chart.series << self - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options end - # The index of this series in the chart's series. # @return [Integer] def index @chart.series.index(self) end - # The order of this series in the chart's series. By default the order is the index of the series. # @return [Integer] def order @@ -68,5 +66,4 @@ module Axlsx str << '</c:ser>' end end - end diff --git a/lib/axlsx/drawing/str_data.rb b/lib/axlsx/drawing/str_data.rb index cb4ff54f..9965ec1d 100644 --- a/lib/axlsx/drawing/str_data.rb +++ b/lib/axlsx/drawing/str_data.rb @@ -5,6 +5,8 @@ module Axlsx # This class is extended for NumData to include the formatCode attribute required for numLit and numCache class StrData + include Axlsx::OptionsParser + # creates a new StrVal object # @option options [Array] :data # @option options [String] :tag_name @@ -12,9 +14,7 @@ module Axlsx @tag_prefix = :str @type = StrVal @pt = SimpleTypedList.new(@type) - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options end # Creates the val objects for this data set. I am not overly confident this is going to play nicely with time and data types. diff --git a/lib/axlsx/drawing/str_val.rb b/lib/axlsx/drawing/str_val.rb index 5f453d97..68cee270 100644 --- a/lib/axlsx/drawing/str_val.rb +++ b/lib/axlsx/drawing/str_val.rb @@ -4,19 +4,20 @@ module Axlsx #This class specifies data for a particular data point. class StrVal - # a string value. - # @return [String] - attr_reader :v + include Axlsx::OptionsParser # creates a new StrVal object # @option options [String] v def initialize(options={}) @v = "" @idx = 0 - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options end + + # a string value. + # @return [String] + attr_reader :v + # @see v def v=(v) @v = v.to_s @@ -27,7 +28,5 @@ module Axlsx Axlsx::validate_unsigned_int(idx) str << '<c:pt idx="' << idx.to_s << '"><c:v>' << v.to_s << '</c:v></c:pt>' end - end - end diff --git a/lib/axlsx/drawing/view_3D.rb b/lib/axlsx/drawing/view_3D.rb index 33a4dbe2..0acba62f 100644 --- a/lib/axlsx/drawing/view_3D.rb +++ b/lib/axlsx/drawing/view_3D.rb @@ -3,6 +3,20 @@ module Axlsx # 3D attributes for a chart. class View3D + include Axlsx::OptionsParser + + # Creates a new View3D for charts + # @option options [Integer] rot_x + # @option options [String] h_percent + # @option options [Integer] rot_y + # @option options [String] depth_percent + # @option options [Boolean] r_ang_ax + # @option options [Integer] perspective + def initialize(options={}) + @rot_x, @h_percent, @rot_y, @depth_percent, @r_ang_ax, @perspective = nil, nil, nil, nil, nil, nil + parse_options options + end + # Validation for hPercent H_PERCENT_REGEX = /0*(([5-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)/ @@ -14,7 +28,7 @@ module Axlsx # @return [Integer] attr_reader :rot_x alias :rotX :rot_x - + # height of chart as % of chart width # must be between 5% and 500% # @return [String] @@ -42,21 +56,7 @@ module Axlsx # @return [Integer] attr_reader :perspective - # Creates a new View3D for charts - # @option options [Integer] rot_x - # @option options [String] h_percent - # @option options [Integer] rot_y - # @option options [String] depth_percent - # @option options [Boolean] r_ang_ax - # @option options [Integer] perspective - def initialize(options={}) - @rot_x, @h_percent, @rot_y, @depth_percent, @r_ang_ax, @perspective = nil, nil, nil, nil, nil, nil - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end - - # @see rot_x + # @see rot_x def rot_x=(v) DataTypeValidator.validate "#{self.class}.rot_x", [Integer, Fixnum], v, lambda {|arg| arg >= -90 && arg <= 90 }; @rot_x = v; end alias :rotX= :rot_x= @@ -78,11 +78,10 @@ module Axlsx # @see r_ang_ax def r_ang_ax=(v) Axlsx::validate_boolean(v); @r_ang_ax = v; end alias :rAngAx= :r_ang_ax= - + # @see perspective def perspective=(v) DataTypeValidator.validate "#{self.class}.perspective", [Integer, Fixnum], v, lambda {|arg| arg >= 0 && arg <= 240 }; @perspective = v; end - # Serializes the object # @param [String] str # @return [String] @@ -96,6 +95,5 @@ module Axlsx str << '<c:perspective val="' << @perspective.to_s << '"/>' unless @perspective.nil? str << '</c:view3D>' end - end end diff --git a/lib/axlsx/drawing/vml_shape.rb b/lib/axlsx/drawing/vml_shape.rb index c915d7e5..701456f3 100644 --- a/lib/axlsx/drawing/vml_shape.rb +++ b/lib/axlsx/drawing/vml_shape.rb @@ -3,6 +3,28 @@ module Axlsx # A VmlShape is used to position and render a comment. class VmlShape + include Axlsx::OptionsParser + + # Creates a new VmlShape + # @option options [Integer|String] left_column + # @option options [Integer|String] left_offset + # @option options [Integer|String] top_row + # @option options [Integer|String] top_offset + # @option options [Integer|String] right_column + # @option options [Integer|String] right_offset + # @option options [Integer|String] bottom_row + # @option options [Integer|String] bottom_offset + def initialize(options={}) + @row = @column = @left_column = @top_row = @right_column = @bottom_row = 0 + @left_offset = 15 + @top_offset = 2 + @right_offset = 50 + @bottom_offset = 5 + @id = (0...8).map{65.+(rand(25)).chr}.join + parse_options options + yield self if block_given? + end + # The row anchor position for this shape determined by the comment's ref value # @return [Integer] attr_reader :row @@ -43,29 +65,7 @@ module Axlsx # @return [Integer] attr_reader :bottom_offset - # Creates a new VmlShape - # @option options [Integer|String] left_column - # @option options [Integer|String] left_offset - # @option options [Integer|String] top_row - # @option options [Integer|String] top_offset - # @option options [Integer|String] right_column - # @option options [Integer|String] right_offset - # @option options [Integer|String] bottom_row - # @option options [Integer|String] bottom_offset - def initialize(options={}) - @row = @column = @left_column = @top_row = @right_column = @bottom_row = 0 - @left_offset = 15 - @top_offset = 2 - @right_offset = 50 - @bottom_offset = 5 - @id = (0...8).map{65.+(rand(25)).chr}.join - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - yield self if block_given? - end - - # @see column + # @see column def column=(v); Axlsx::validate_integerish(v); @column = v.to_i end # @see row diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb index 065eeb46..b5537ac1 100644 --- a/lib/axlsx/stylesheet/color.rb +++ b/lib/axlsx/stylesheet/color.rb @@ -2,6 +2,21 @@ module Axlsx # The color class represents a color used for borders, fills an fonts class Color + + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + + # Creates a new Color object + # @option options [Boolean] auto + # @option options [String] rgb + # @option options [Float] tint + def initialize(options={}) + @rgb = "FF000000" + parse_options options + end + + serializable_attributes :auto, :rgb, :tint + # Determines if the color is system color dependant # @return [Boolean] attr_reader :auto @@ -31,17 +46,7 @@ module Axlsx # @return [Float] attr_reader :tint - # Creates a new Color object - # @option options [Boolean] auto - # @option options [String] rgb - # @option options [Float] tint - def initialize(options={}) - @rgb = "FF000000" - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] - end - end - # @see auto + # @see auto def auto=(v) Axlsx::validate_boolean v; @auto = v end # @see color def rgb=(v) @@ -66,9 +71,7 @@ module Axlsx # @return [String] def to_xml_string(str = '') str << "<color " - self.instance_values.each do |key, value| - str << key.to_s << '="' << value.to_s << '" ' - end + serialized_attributes str str << "/>" end end diff --git a/lib/axlsx/stylesheet/font.rb b/lib/axlsx/stylesheet/font.rb index d35aebf8..03da52e5 100644 --- a/lib/axlsx/stylesheet/font.rb +++ b/lib/axlsx/stylesheet/font.rb @@ -4,6 +4,27 @@ module Axlsx # @note The recommended way to manage fonts, and other styles is Styles#add_style # @see Styles#add_style class Font + include Axlsx::OptionsParser + + # Creates a new Font + # @option options [String] name + # @option options [Integer] charset + # @option options [Integer] family + # @option options [Integer] family + # @option options [Boolean] b + # @option options [Boolean] i + # @option options [Boolean] u + # @option options [Boolean] strike + # @option options [Boolean] outline + # @option options [Boolean] shadow + # @option options [Boolean] condense + # @option options [Boolean] extend + # @option options [Color] color + # @option options [Integer] sz + def initialize(options={}) + parse_options options + end + # The name of the font # @return [String] attr_reader :name @@ -86,28 +107,7 @@ module Axlsx # @return [Integer] attr_reader :sz - # Creates a new Font - # @option options [String] name - # @option options [Integer] charset - # @option options [Integer] family - # @option options [Integer] family - # @option options [Boolean] b - # @option options [Boolean] i - # @option options [Boolean] u - # @option options [Boolean] strike - # @option options [Boolean] outline - # @option options [Boolean] shadow - # @option options [Boolean] condense - # @option options [Boolean] extend - # @option options [Color] color - # @option options [Integer] sz - def initialize(options={}) - options.each do |o| - next if o[1].nil? - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end - # @see name + # @see name def name=(v) Axlsx::validate_string v; @name = v end # @see charset def charset=(v) Axlsx::validate_unsigned_int v; @charset = v end diff --git a/lib/axlsx/stylesheet/num_fmt.rb b/lib/axlsx/stylesheet/num_fmt.rb index d5122f7e..a796b1c7 100644 --- a/lib/axlsx/stylesheet/num_fmt.rb +++ b/lib/axlsx/stylesheet/num_fmt.rb @@ -3,6 +3,26 @@ module Axlsx # A NumFmt object defines an identifier and formatting code for data in cells. # @note The recommended way to manage styles is Styles#add_style class NumFmt + + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + + # Creates a new NumFmt object + # @param [Hash] options Options for the number format object + # @option [Integer] numFmtId The predefined format id or new format id for this format + # @option [String] fomratCode The format code for this number format + def initialize(options={}) + @numFmtId = 0 + @formatCode = "" + parse_options options + end + + serializable_attributes :formatCode, :numFmtId + + # @return [String] The formatting to use for this number format. + # @see http://support.microsoft.com/kb/264372 + attr_reader :formatCode + # @return [Integer] An unsinged integer referencing a standard or custom number format. # @note # These are the known formats I can dig up. The constant NUM_FMT_PERCENT is 9, and uses the default % formatting. Axlsx also defines a few formats for date and time that are commonly used in asia as NUM_FMT_YYYYMMDD and NUM_FRM_YYYYMMDDHHMMSS. @@ -40,29 +60,18 @@ module Axlsx # @see Axlsx attr_reader :numFmtId - # @return [String] The formatting to use for this number format. - # @see http://support.microsoft.com/kb/264372 - attr_reader :formatCode - def initialize(options={}) - @numFmtId = 0 - @formatCode = "" - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] - end - end - # @see numFmtId def numFmtId=(v) Axlsx::validate_unsigned_int v; @numFmtId = v end + # @see formatCode def formatCode=(v) Axlsx::validate_string v; @formatCode = v end - # Serializes the object # @param [String] str # @return [String] def to_xml_string(str = '') str << '<numFmt ' - str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ') + serialized_attributes str str << '/>' end diff --git a/lib/axlsx/stylesheet/pattern_fill.rb b/lib/axlsx/stylesheet/pattern_fill.rb index 0c0a51a6..c06c7f16 100644 --- a/lib/axlsx/stylesheet/pattern_fill.rb +++ b/lib/axlsx/stylesheet/pattern_fill.rb @@ -5,6 +5,17 @@ module Axlsx # @see Style#add_style class PatternFill + include Axlsx::OptionsParser + + # Creates a new PatternFill Object + # @option options [Symbol] patternType + # @option options [Color] fgColor + # @option options [Color] bgColor + def initialize(options={}) + @patternType = :none + parse_options options + end + # The color to use for the the background in solid fills. # @return [Color] attr_reader :fgColor @@ -38,16 +49,6 @@ module Axlsx # @see Office Open XML Part 1 18.18.55 attr_reader :patternType - # Creates a new PatternFill Object - # @option options [Symbol] patternType - # @option options [Color] fgColor - # @option options [Color] bgColor - def initialize(options={}) - @patternType = :none - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - end # @see fgColor def fgColor=(v) DataTypeValidator.validate "PatternFill.fgColor", Color, v; @fgColor=v end # @see bgColor @@ -77,6 +78,5 @@ module Axlsx end str << '</patternFill>' end - end end diff --git a/lib/axlsx/stylesheet/table_style.rb b/lib/axlsx/stylesheet/table_style.rb index db837b99..d330beb2 100644 --- a/lib/axlsx/stylesheet/table_style.rb +++ b/lib/axlsx/stylesheet/table_style.rb @@ -4,6 +4,22 @@ module Axlsx # @note Table are not supported in this version and only the defaults required for a valid workbook are created. class TableStyle < SimpleTypedList + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + + # 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 + parse_options options + super TableStyleElement + end + + serializable_attributes :name, :pivot, :table + # The name of this table style # @return [string] attr_reader :name @@ -16,19 +32,6 @@ module Axlsx # @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 @@ -40,10 +43,8 @@ module Axlsx # @param [String] str # @return [String] def to_xml_string(str = '') - attrs = instance_values.reject { |k, v| ![:name, :pivot, :table].include?(k) } - attrs[:count] = self.size str << '<tableStyle ' - str << attrs.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ') + serialized_attributes str, {:count => self.size} str << '>' each { |table_style_el| table_style_el.to_xml_string(str) } str << '</tableStyle>' diff --git a/lib/axlsx/stylesheet/table_style_element.rb b/lib/axlsx/stylesheet/table_style_element.rb index 1a4c8803..2ffc8af1 100644 --- a/lib/axlsx/stylesheet/table_style_element.rb +++ b/lib/axlsx/stylesheet/table_style_element.rb @@ -3,6 +3,20 @@ module Axlsx # an element of style that belongs to a table style. # @note tables and table styles are not supported in this version. This class exists in preparation for that support. class TableStyleElement + + include Axlsx::OptionsParser + include Axlsx::SerializedAttributes + + # creates a new TableStyleElement object + # @option options [Symbol] type + # @option options [Integer] size + # @option options [Integer] dxfId + def initialize(options={}) + parse_options options + end + + serializable_attributes :type, :size, :dxfId + # The type of style element. The following type are allowed # :wholeTable # :headerRow @@ -43,16 +57,6 @@ module Axlsx # @return [Integer] attr_reader :dxfId - # creates a new TableStyleElement object - # @option options [Symbol] type - # @option options [Integer] size - # @option options [Integer] dxfId - def initialize(options={}) - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? o[0] - end - end - # @see type def type=(v) Axlsx::validate_table_element_type v; @type = v end @@ -67,7 +71,7 @@ module Axlsx # @return [String] def to_xml_string(str = '') str << '<tableStyleElement ' - str << instance_values.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ') + serialized_attributes str str << '/>' end diff --git a/lib/axlsx/stylesheet/table_styles.rb b/lib/axlsx/stylesheet/table_styles.rb index a43e7ce5..6ef07c51 100644 --- a/lib/axlsx/stylesheet/table_styles.rb +++ b/lib/axlsx/stylesheet/table_styles.rb @@ -4,13 +4,7 @@ module Axlsx # @note Support for custom table styles does not exist in this version. Many of the classes required are defined in preparation for future release. Please do not attempt to add custom table styles. class TableStyles < SimpleTypedList - # The default table style. The default value is 'TableStyleMedium9' - # @return [String] - attr_reader :defaultTableStyle - - # The default pivot table style. The default value is 'PivotStyleLight6' - # @return [String] - attr_reader :defaultPivotStyle + include Axlsx::SerializedAttributes # Creates a new TableStyles object that is a container for TableStyle objects # @option options [String] defaultTableStyle @@ -20,7 +14,18 @@ module Axlsx @defaultPivotStyle = options[:defaultPivotStyle] || "PivotStyleLight16" super TableStyle end - # @see defaultTableStyle + + serializable_attributes :defaultTableStyle, :defaultPivotStyle + + # The default table style. The default value is 'TableStyleMedium9' + # @return [String] + attr_reader :defaultTableStyle + + # The default pivot table style. The default value is 'PivotStyleLight6' + # @return [String] + attr_reader :defaultPivotStyle + + # @see defaultTableStyle def defaultTableStyle=(v) Axlsx::validate_string(v); @defaultTableStyle = v; end # @see defaultPivotStyle def defaultPivotStyle=(v) Axlsx::validate_string(v); @defaultPivotStyle = v; end @@ -29,10 +34,8 @@ module Axlsx # @param [String] str # @return [String] def to_xml_string(str = '') - attr = self.instance_values.reject {|k, v| ![:defaultTableStyle, :defaultPivotStyle].include?(k.to_sym) } - attr[:count] = self.size str << '<tableStyles ' - str << attr.map { |key, value| '' << key.to_s << '="' << value.to_s << '"' }.join(' ') + serialized_attributes str, {:count => self.size } str << '>' each { |table_style| table_style.to_xml_string(str) } str << '</tableStyles>' |
