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, OutlinePr, PageMargins, PageSetUpPr, PageSetup, Pane, PictureLocking, PrintOptions, ProtectedRange, Row, Selection, SheetCalcPr, SheetFormatPr, SheetPr, SheetProtection, SheetView, TableStyle, TableStyleElement, TableStyleInfo, TableStyles, WorkbookView, Worksheet, 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
-
#serialized_attributes(str = +'',, additional_attributes = {}, camelize_value = true) ⇒ Object
serializes the instance values of the defining object based on the list of serializable attributes.
-
#serialized_element_attributes(str = +'',, additional_attributes = []) ⇒ String
serialized instance values at text nodes on a camelized element of the attribute name.
-
#serialized_tag(tagname, str, additional_attributes = {}) ⇒ Object
creates a XML tag with serialized attributes.
Class Method Details
.included(base) ⇒ Object
Extend with class methods
8 9 10 |
# File 'lib/axlsx/util/serialized_attributes.rb', line 8 def self.included(base) base.send :extend, ClassMethods end |
Instance Method Details
#serialized_attributes(str = +'',, additional_attributes = {}, camelize_value = true) ⇒ 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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/axlsx/util/serialized_attributes.rb', line 63 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 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_element_attributes(str = +'',, additional_attributes = []) ⇒ 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.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/axlsx/util/serialized_attributes.rb', line 99 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| 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 |
#serialized_tag(tagname, str, additional_attributes = {}) ⇒ Object
creates a XML tag with serialized attributes
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/axlsx/util/serialized_attributes.rb', line 44 def serialized_tag(tagname, str, additional_attributes = {}) str << '<' << tagname << ' ' serialized_attributes(str, additional_attributes) if block_given? str << '>' yield str << '</' << tagname << '>' else str << '/>' end end |