Module: Axlsx::SerializedAttributes
- Included in:
- Border, Break, CellAlignment, CellProtection, CellStyle, Cfvo, Col, Color, ConditionalFormattingRule, DataBar, DefinedName, FilterColumn, Filters, Filters::DateGroupItem, GradientFill, HeaderFooter, Hyperlink, IconSet, NumFmt, PageMargins, PageSetUpPr, PageSetup, Pane, PictureLocking, PrintOptions, ProtectedRange, Row, Selection, SheetCalcPr, SheetFormatPr, SheetPr, SheetProtection, SheetView, TableStyle, TableStyleElement, TableStyleInfo, TableStyles, WorksheetHyperlink, Xf
- Defined in:
- lib/axlsx/util/serialized_attributes.rb
Overview
This module allows us to define a list of symbols defining which attributes will be serialized for a class.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
Extend with class methods.
Instance Method Summary collapse
-
#declared_attributes ⇒ Object
A hash of instance variables that have been declared with seraialized_attributes and are not nil.
-
#serialized_attributes(str = '', additional_attributes = {}) ⇒ Object
serializes the instance values of the defining object based on the list of serializable attributes.
-
#serialized_element_attributes(str = '', additional_attributes = [], &block) ⇒ String
serialized instance values at text nodes on a camelized element of the attribute name.
Class Method Details
.included(base) ⇒ Object
Extend with class methods
7 8 9 |
# File 'lib/axlsx/util/serialized_attributes.rb', line 7 def self.included(base) base.send :extend, ClassMethods end |
Instance Method Details
#declared_attributes ⇒ Object
A hash of instance variables that have been declared with seraialized_attributes and are not nil. This requires ruby 1.9.3 or higher
53 54 55 56 57 |
# File 'lib/axlsx/util/serialized_attributes.rb', line 53 def declared_attributes instance_values.select do |key, value| value != nil && self.class.xml_attributes.include?(key.to_sym) end end |
#serialized_attributes(str = '', additional_attributes = {}) ⇒ Object
serializes the instance values of the defining object based on the list of serializable attributes. serialization to. defining values that are not serializable attributes list.
42 43 44 45 46 47 48 |
# File 'lib/axlsx/util/serialized_attributes.rb', line 42 def serialized_attributes(str = '', additional_attributes = {}) attributes = declared_attributes.merge! additional_attributes attributes.each do |key, value| str << "#{Axlsx.camel(key, false)}=\"#{Axlsx.camel(value, false)}\" " end str end |
#serialized_element_attributes(str = '', additional_attributes = [], &block) ⇒ String
serialized instance values at text nodes on a camelized element of the attribute name. You may pass in a block for evaluation against non nil values. We use an array for element attributes becuase misordering will break the xml and 1.8.7 does not support ordered hashes.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/axlsx/util/serialized_attributes.rb', line 66 def serialized_element_attributes(str='', additional_attributes=[], &block) attrs = self.class.xml_element_attributes + additional_attributes values = instance_values attrs.each do |attribute_name| value = values[attribute_name.to_s] next if value.nil? value = yield value if block_given? element_name = Axlsx.camel(attribute_name, false) str << "<#{element_name}>#{value}</#{element_name}>" end str end |