diff options
| author | Randy Morgan <[email protected]> | 2012-10-14 11:34:51 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-10-14 11:34:51 +0900 |
| commit | a441bc1ff24c5b238203adb49b6e7ce208d5dabf (patch) | |
| tree | 770da3a7384636a4cc0c35ff443d212a1ef47912 /lib | |
| parent | e4d4538e1ea15b6aff9e4ef3fbc47c2e29ceb8be (diff) | |
| download | caxlsx-a441bc1ff24c5b238203adb49b6e7ce208d5dabf.tar.gz caxlsx-a441bc1ff24c5b238203adb49b6e7ce208d5dabf.zip | |
Refactored to use use options parser.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/drawing/hyperlink.rb | 41 | ||||
| -rw-r--r-- | lib/axlsx/drawing/num_data.rb | 12 | ||||
| -rw-r--r-- | lib/axlsx/drawing/num_data_source.rb | 33 | ||||
| -rw-r--r-- | lib/axlsx/drawing/one_cell_anchor.rb | 42 | ||||
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 52 |
5 files changed, 88 insertions, 92 deletions
diff --git a/lib/axlsx/drawing/hyperlink.rb b/lib/axlsx/drawing/hyperlink.rb index 3b3f8c37..3f4a2e80 100644 --- a/lib/axlsx/drawing/hyperlink.rb +++ b/lib/axlsx/drawing/hyperlink.rb @@ -6,8 +6,28 @@ module Axlsx class Hyperlink include Axlsx::SerializedAttributes + include Axlsx::OptionsParser + + #Creates a hyperlink object + # parent must be a Pic for now, although I expect that other object support this tag and its cNvPr parent + # @param [Pic] parent + # @option options [String] tooltip message shown when hyperlinked object is hovered over with mouse. + # @option options [String] tgtFrame Target frame for opening hyperlink + # @option options [String] invalidUrl supposedly use to store the href when we know it is an invalid resource. + # @option options [String] href the target resource this hyperlink links to. This is actually stored on the relationship. + # @option options [String] action A string that can be used to perform specific actions. For excel please see this reference: http://msdn.microsoft.com/en-us/library/ff532419%28v=office.12%29.aspx + # @option options [Boolean] endSnd terminate any sound events when processing this link + # @option options [Boolean] history include this link in the list of visited links for the applications history. + # @option options [Boolean] highlightClick indicate that the link has already been visited. + def initialize(parent, options={}) + DataTypeValidator.validate "Hyperlink.parent", [Pic], parent + @parent = parent + parse_options options + yield self if block_given? + end serializable_attributes :invalid_url, :action, :end_snd, :highlight_click, :history, :tgt_frame, :tooltip + # The destination of the hyperlink stored in the drawing's relationships document. # @return [String] attr_accessor :href @@ -63,27 +83,6 @@ module Axlsx # @return [String] attr_accessor :tooltip - #Creates a hyperlink object - # parent must be a Pic for now, although I expect that other object support this tag and its cNvPr parent - # @param [Pic] parent - # @option options [String] tooltip message shown when hyperlinked object is hovered over with mouse. - # @option options [String] tgtFrame Target frame for opening hyperlink - # @option options [String] invalidUrl supposedly use to store the href when we know it is an invalid resource. - # @option options [String] href the target resource this hyperlink links to. This is actually stored on the relationship. - # @option options [String] action A string that can be used to perform specific actions. For excel please see this reference: http://msdn.microsoft.com/en-us/library/ff532419%28v=office.12%29.aspx - # @option options [Boolean] endSnd terminate any sound events when processing this link - # @option options [Boolean] history include this link in the list of visited links for the applications history. - # @option options [Boolean] highlightClick indicate that the link has already been visited. - def initialize(parent, options={}) - DataTypeValidator.validate "Hyperlink.parent", [Pic], parent - @parent = parent - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - yield self if block_given? - - end - # Serializes the object # @param [String] str # @return [String] diff --git a/lib/axlsx/drawing/num_data.rb b/lib/axlsx/drawing/num_data.rb index ee6b145e..c1773f59 100644 --- a/lib/axlsx/drawing/num_data.rb +++ b/lib/axlsx/drawing/num_data.rb @@ -4,9 +4,7 @@ module Axlsx #This class specifies data for a particular data point. It is used for both numCache and numLit object class NumData - # A string representing the format code to apply. For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute. - # @return [String] - attr_reader :format_code + include Axlsx::OptionsParser # creates a new NumVal object # @option options [String] formatCode @@ -15,11 +13,13 @@ module Axlsx def initialize(options={}) @format_code = "General" @pt = SimpleTypedList.new NumVal - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options end + # A string representing the format code to apply. For more information see see the SpreadsheetML numFmt element's (§18.8.30) formatCode attribute. + # @return [String] + attr_reader :format_code + # Creates the val objects for this data set. I am not overly confident this is going to play nicely with time and data types. # @param [Array] values An array of cells or values. def data=(values=[]) diff --git a/lib/axlsx/drawing/num_data_source.rb b/lib/axlsx/drawing/num_data_source.rb index 63f0d811..14d53301 100644 --- a/lib/axlsx/drawing/num_data_source.rb +++ b/lib/axlsx/drawing/num_data_source.rb @@ -3,18 +3,7 @@ module Axlsx # A numeric data source for use by charts. class NumDataSource - # The tag name to use when serializing this data source. - # Only items defined in allowed_tag_names are allowed - # @return [Symbol] - attr_reader :tag_name - - attr_reader :data - - # allowed element tag names - # @return [Array] - def self.allowed_tag_names - [:yVal, :val] - end + include Axlsx::OptionsParser # creates a new NumDataSource object # @option options [Array] data An array of Cells or Numeric objects @@ -30,12 +19,24 @@ module Axlsx if options[:data] && options[:data].first.is_a?(Cell) @f = Axlsx::cell_range(options[:data]) end - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end + parse_options options + end + + + # The tag name to use when serializing this data source. + # Only items defined in allowed_tag_names are allowed + # @return [Symbol] + attr_reader :tag_name + + attr_reader :data + + # allowed element tag names + # @return [Array] + def self.allowed_tag_names + [:yVal, :val] end - # sets the tag name for this data source + # sets the tag name for this data source # @param [Symbol] v One of the allowed_tag_names def tag_name=(v) Axlsx::RestrictionValidator.validate "#{self.class.name}.tag_name", self.class.allowed_tag_names, v diff --git a/lib/axlsx/drawing/one_cell_anchor.rb b/lib/axlsx/drawing/one_cell_anchor.rb index 9a202cc1..b16a939b 100644 --- a/lib/axlsx/drawing/one_cell_anchor.rb +++ b/lib/axlsx/drawing/one_cell_anchor.rb @@ -6,6 +6,26 @@ module Axlsx # @see Worksheet#add_image class OneCellAnchor + include Axlsx::OptionsParser + + # Creates a new OneCellAnchor object and an Pic associated with it. + # @param [Drawing] drawing + # @option options [Array] start_at the col, row to start at + # @option options [Integer] width + # @option options [Integer] height + # @option options [String] image_src the file location of the image you will render + # @option options [String] name the name attribute for the rendered image + # @option options [String] descr the description of the image rendered + def initialize(drawing, options={}) + @drawing = drawing + @width = 0 + @height = 0 + drawing.anchors << self + @from = Marker.new + parse_options options + @object = Pic.new(self, options) + end + # A marker that defines the from cell anchor. The default from column and row are 0 and 0 respectively # @return [Marker] attr_reader :from @@ -28,27 +48,6 @@ module Axlsx # @return [Integer] attr_reader :height - - # Creates a new OneCellAnchor object and an Pic associated with it. - # @param [Drawing] drawing - # @option options [Array] start_at the col, row to start at - # @option options [Integer] width - # @option options [Integer] height - # @option options [String] image_src the file location of the image you will render - # @option options [String] name the name attribute for the rendered image - # @option options [String] descr the description of the image rendered - def initialize(drawing, options={}) - @drawing = drawing - @width = 0 - @height = 0 - drawing.anchors << self - @from = Marker.new - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - @object = Pic.new(self, options) - end - # @see height def height=(v) Axlsx::validate_unsigned_int(v); @height = v; end @@ -61,7 +60,6 @@ module Axlsx @drawing.anchors.index(self) end - # Serializes the object # @param [String] str # @return [String] diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index 3eb99e7d..9375f20c 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -5,6 +5,26 @@ module Axlsx # @see Worksheet#add_image class Pic + include Axlsx::OptionsParser + + # Creates a new Pic(ture) object + # @param [Anchor] anchor the anchor that holds this image + # @option options [String] name + # @option options [String] descr + # @option options [String] image_src + # @option options [Array] start_at + # @option options [Intger] width + # @option options [Intger] height + def initialize(anchor, options={}) + @anchor = anchor + @hyperlink = nil + @anchor.drawing.worksheet.workbook.images << self + parse_options options + start_at(*options[:start_at]) if options[:start_at] + yield self if block_given? + @picture_locking = PictureLocking.new(options) + end + # allowed file extenstions ALLOWED_EXTENSIONS = ['gif', 'jpeg', 'png', 'jpg'] @@ -12,7 +32,6 @@ module Axlsx # @return [String] attr_reader :name - # A description of the picture # @return [String] attr_reader :descr @@ -29,26 +48,6 @@ module Axlsx # The picture locking attributes for this picture attr_reader :picture_locking - # Creates a new Pic(ture) object - # @param [Anchor] anchor the anchor that holds this image - # @option options [String] name - # @option options [String] descr - # @option options [String] image_src - # @option options [Array] start_at - # @option options [Intger] width - # @option options [Intger] height - def initialize(anchor, options={}) - @anchor = anchor - @hyperlink = nil - @anchor.drawing.worksheet.workbook.images << self - options.each do |o| - self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" - end - start_at(*options[:start_at]) if options[:start_at] - yield self if block_given? - @picture_locking = PictureLocking.new(options) - end - attr_reader :hyperlink # sets or updates a hyperlink for this image. @@ -136,8 +135,8 @@ module Axlsx use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor) @anchor.height = v end - - # This is a short cut method to set the start anchor position + + # This is a short cut method to set the start anchor position # If you need finer granularity in positioning use # graphic_frame.anchor.from.colOff / rowOff # @param [Integer] x The column @@ -148,7 +147,7 @@ module Axlsx @anchor.from.row = y @anchor.from end - + # noop if not using a two cell anchor # @param [Integer] x The column # @param [Integer] y The row @@ -178,7 +177,7 @@ module Axlsx str << '<a:prstGeom prst="rect"><a:avLst/></a:prstGeom></xdr:spPr></xdr:pic>' end - + private # Changes the anchor to a one cell anchor. @@ -186,7 +185,7 @@ module Axlsx return if @anchor.is_a?(OneCellAnchor) swap_anchor(OneCellAnchor.new(@anchor.drawing, :from => @anchor.from)) end - + #changes the anchor type to a two cell anchor def use_two_cell_anchor return if @anchor.is_a?(TwoCellAnchor) @@ -202,6 +201,5 @@ module Axlsx @anchor.drawing.anchors[@anchor.drawing.anchors.index(@anchor)] = new_anchor @anchor = new_anchor end - end end |
