diff options
| author | Zsolt Kozaroczy <[email protected]> | 2023-05-17 10:38:11 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-17 10:38:11 +0200 |
| commit | 859e2f529bfb15308c8afac3e22020514a97366e (patch) | |
| tree | c35b58dca864ad0f4800445e58e2ca2eca01d31b | |
| parent | 7899578ad12fb710cbdf74e1aa87e2190c5832ed (diff) | |
| parent | 3e6badd102edd9f4ec54b12ead2e769474e4a045 (diff) | |
| download | caxlsx-859e2f529bfb15308c8afac3e22020514a97366e.tar.gz caxlsx-859e2f529bfb15308c8afac3e22020514a97366e.zip | |
Merge pull request #230 from pkmiec/serializedAttributes
Serialized attributes
| -rw-r--r-- | .rubocop_todo.yml | 8 | ||||
| -rw-r--r-- | lib/axlsx.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/num_fmt.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/util/serialized_attributes.rb | 57 |
4 files changed, 43 insertions, 30 deletions
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ed0c7d35..eb52d7d4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -76,7 +76,6 @@ Lint/UnusedBlockArgument: Lint/UnusedMethodArgument: Exclude: - 'lib/axlsx/package.rb' - - 'lib/axlsx/util/serialized_attributes.rb' - 'lib/axlsx/util/validators.rb' Lint/UselessAssignment: @@ -92,11 +91,6 @@ Lint/Void: - 'lib/axlsx/workbook/worksheet/data_bar.rb' - 'lib/axlsx/workbook/worksheet/pivot_table.rb' -# This cop supports safe autocorrection (--autocorrect). -Performance/BlockGivenWithExplicitBlock: - Exclude: - - 'lib/axlsx/util/serialized_attributes.rb' - # Configuration parameters: MinSize. Performance/CollectionLiteralInLoop: Exclude: @@ -349,7 +343,6 @@ Style/Next: Style/NonNilCheck: Exclude: - 'lib/axlsx/drawing/d_lbls.rb' - - 'lib/axlsx/util/serialized_attributes.rb' - 'lib/axlsx/workbook/worksheet/col.rb' - 'lib/axlsx/workbook/worksheet/page_setup.rb' @@ -377,6 +370,7 @@ Style/OptionalBooleanParameter: Exclude: - 'lib/axlsx.rb' - 'lib/axlsx/package.rb' + - 'lib/axlsx/util/serialized_attributes.rb' - 'lib/axlsx/util/validators.rb' - 'lib/axlsx/workbook/workbook.rb' - 'lib/axlsx/workbook/worksheet/cell.rb' diff --git a/lib/axlsx.rb b/lib/axlsx.rb index bf16afbd..815e0440 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -185,7 +185,7 @@ module Axlsx # @return [Object] def self.booleanize(value) if BOOLEAN_VALUES.include?(value) - value ? 1 : 0 + value ? '1' : '0' else value end diff --git a/lib/axlsx/stylesheet/num_fmt.rb b/lib/axlsx/stylesheet/num_fmt.rb index 1a7add0e..95d9a62b 100644 --- a/lib/axlsx/stylesheet/num_fmt.rb +++ b/lib/axlsx/stylesheet/num_fmt.rb @@ -75,11 +75,7 @@ module Axlsx # Override to avoid removing underscores def serialized_attributes(str = +'', additional_attributes = {}) - attributes = declared_attributes.merge! additional_attributes - attributes.each do |key, value| - str << "#{Axlsx.camel(key, false)}=\"#{Axlsx.booleanize(value)}\" " - end - str + super(str, additional_attributes, false) end end end diff --git a/lib/axlsx/util/serialized_attributes.rb b/lib/axlsx/util/serialized_attributes.rb index b7247e0d..27e19904 100644 --- a/lib/axlsx/util/serialized_attributes.rb +++ b/lib/axlsx/util/serialized_attributes.rb @@ -15,11 +15,21 @@ module Axlsx # which of the instance values are serializable def serializable_attributes(*symbols) @xml_attributes = symbols + @camel_xml_attributes = nil + @ivar_xml_attributes = nil end # a reader for those attributes attr_reader :xml_attributes + def camel_xml_attributes + @camel_xml_attributes ||= @xml_attributes.map { |attr| Axlsx.camel(attr, false) } + end + + def ivar_xml_attributes + @ivar_xml_attributes ||= @xml_attributes.map { |attr| :"@#{attr}" } + end + # This helper registers the attributes that will be formatted as elements. def serializable_element_attributes(*symbols) @xml_element_attributes = symbols @@ -31,13 +41,13 @@ module Axlsx # creates a XML tag with serialized attributes # @see SerializedAttributes#serialized_attributes - def serialized_tag(tagname, str, additional_attributes = {}, &block) - str << "<#{tagname} " + def serialized_tag(tagname, str, additional_attributes = {}) + str << '<' << tagname << ' ' serialized_attributes(str, additional_attributes) if block_given? str << '>' yield - str << "</#{tagname}>" + str << '</' << tagname << '>' else str << '/>' end @@ -49,21 +59,34 @@ module Axlsx # serialization to. # @param [Hash] additional_attributes An option key value hash for # defining values that are not serializable attributes list. - def serialized_attributes(str = +'', additional_attributes = {}) - attributes = declared_attributes.merge! additional_attributes - attributes.each do |key, value| - str << "#{Axlsx.camel(key, false)}=\"#{Axlsx.camel(Axlsx.booleanize(value), false)}\" " + # @param [Boolean] camelize_value Should the attribute values be camelized + def serialized_attributes(str = +'', additional_attributes = {}, camelize_value = true) + camel_xml_attributes = self.class.camel_xml_attributes + ivar_xml_attributes = self.class.ivar_xml_attributes + + self.class.xml_attributes.each_with_index do |attr, index| + next if additional_attributes.key?(attr) + next unless instance_variable_defined?(ivar_xml_attributes[index]) + + value = instance_variable_get(ivar_xml_attributes[index]) + next if value.nil? + + value = Axlsx.booleanize(value) + value = Axlsx.camel(value, false) if camelize_value + + str << camel_xml_attributes[index] << '="' << value.to_s << '" ' end - str - end - # A hash of instance variables that have been declared with - # seraialized_attributes and are not nil. - # This requires ruby 1.9.3 or higher - def declared_attributes - Axlsx.instance_values_for(self).select do |key, value| - value != nil && self.class.xml_attributes.include?(key.to_sym) + additional_attributes.each do |attr, value| + next if value.nil? + + value = Axlsx.booleanize(value) + value = Axlsx.camel(value, false) if camelize_value + + str << Axlsx.camel(attr, false) << '="' << value.to_s << '" ' end + + str end # serialized instance values at text nodes on a camelized element of the @@ -73,7 +96,7 @@ module Axlsx # @param [String] str The string instance to which serialized data is appended # @param [Array] additional_attributes An array of additional attribute names. # @return [String] The serialized output. - def serialized_element_attributes(str = +'', additional_attributes = [], &block) + def serialized_element_attributes(str = +'', additional_attributes = []) attrs = self.class.xml_element_attributes + additional_attributes values = Axlsx.instance_values_for(self) attrs.each do |attribute_name| @@ -82,7 +105,7 @@ module Axlsx value = yield value if block_given? element_name = Axlsx.camel(attribute_name, false) - str << "<#{element_name}>#{value}</#{element_name}>" + str << '<' << element_name << '>' << value << '</' << element_name << '>' end str end |
