module RubyXL::OOXMLObjectClassMethods
def accessorize(str)
def accessorize(str) acc = str.to_s.dup acc.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') acc.gsub!(/([a-z\d])([A-Z])/,'\1_\2') acc.gsub!(':','_') acc.downcase.to_sym end
def define_attribute(attr_name, attr_type, extra_params = {})
define_attribute(:errorStyle, %w{ stop warning information }, :default => 'stop',)
The value of the element will be accessible as a String by calling +obj.expression+
define_attribute(:_, :string, :accessor => :expression)
An Integer attribute 'uniqueCount' accessible as +obj.unique_count+
define_attribute(:uniqueCount, :int)
A Boolean attribute 'outline' with default value +true+ will be accessible by calling +obj.outline+
define_attribute(:outline, :bool, :default => true)
==== Examples
* +:computed+ - Do not store this attribute on +parse+, but do call the object-provided read accessor on +write_xml+.
* +:required+ - Whether this attribute is required when writing XML. If the value of the attrinute is not explicitly provided, +:default+ is written instead.
* +:default+ - Value this attribute defaults to if not explicitly provided.
* +:accessor+ - Name of the accessor for this attribute to be defined on the object. If not provided, defaults to classidied +attribute_name+.
* +extra_parameters+ - Hash of optional parameters as follows:
* one of +simple_types+ - String, plus the list of acceptable values is saved for future validation (not used yet).
* +:bool+ - Boolean ("1" and "true" convert to +true+, others to +false+)
* +:ref+ - RubyXL::Reference
* +:sqref+ - RubyXL::Sqref
* +:string+ - String (no conversion)
* +:double+ - Float
* +:uint+ - Unsigned Integer
* +:int+ - Integer
* +attribute_type+ - Specifies the conversion type for the attribute when parsing. Available options are:
* Special attibute name '_' (underscore) denotes the value of the element rather than attribute.
* +attribute_name+ - Name of the element attribute as seen in the source XML. Can be either "String" or :Symbol
=== Parameters
Defines an attribute of OOXML object.
def define_attribute(attr_name, attr_type, extra_params = {}) attrs = obtain_class_variable(:@@ooxml_attributes) attr_hash = extra_params.merge({ :attr_type => attr_type }) attr_hash[:accessor] ||= accessorize(attr_name) attrs[attr_name.to_s] = attr_hash self.send(:attr_accessor, attr_hash[:accessor]) unless attr_hash[:computed] end
def define_child_node(klass, extra_params = {})
define_child_node(RubyXL::Font, :collection => :with_count, :accessor => :fonts)
Use class RubyXL::BorderEdge when parsing both the elements
define_child_node(RubyXL::BorderEdge, :node_name => :right)
define_child_node(RubyXL::BorderEdge, :node_name => :left)
Define an array of nodes accessed by obj.hyperlinks accessor, each of which will be parsed by the RubyXL::Hyperlink.parse()
define_child_node(RubyXL::Hyperlink, :collection => true, :accessor => :hyperlinks)
Define a singular child node parsed by the RubyXL::BorderEdge.parse() and accessed by the default obj.alignment accessor
define_child_node(RubyXL::Alignment)
==== Examples
* +:with_count+ - same as +true+, but in addition, the attribute +count+ is defined on the current object, that will be automatically set to the number of elements in the collection at the start of +write_xml+ call.
* +true+ - a collection of child nodes is accessed as +Array+ through the respective accessor;
* +false+ (default) - child node is directly accessible through the respective accessor;
* +:collection+ - Whether the child node should be treated as a single node or a collection of nodes:
* +:node_name+ - Node name for the child node, in case it does not match the one defined by the +klass+.
* +:accessor+ - Name of the accessor for this attribute to be defined on the object. If not provided, defaults to classidied +attribute_name+.
* +extra_parameters+ - Hash of optional parameters as follows:
* +klass+ - Class (descendant of RubyXL::OOXMLObject) of the child nodes. Child node objects will be produced by calling +parse+ method of that class.
=== Parameters
Defines a child node of OOXML object.
def define_child_node(klass, extra_params = {}) child_nodes = obtain_class_variable(:@@ooxml_child_nodes) child_node_name = (extra_params[:node_name] || klass.class_variable_get(:@@ooxml_tag_name)).to_s accessor = (extra_params[:accessor] || accessorize(child_node_name)).to_sym child_nodes[child_node_name] = { :class => klass, :is_array => extra_params[:collection], :accessor => accessor } define_count_attribute if extra_params[:collection] == :with_count self.send(:attr_accessor, accessor) end
def define_count_attribute
def define_count_attribute define_attribute(:count, :uint, :required => true) end
def define_element_name(element_name)
==== Examples
* +element_name+
=== Parameters
extra parameter can be used to override the default element name.
In case of different objects represented by the same class in different parts of OOXML tree, +:node_name+
Defines the name of the element that represents the current OOXML object. Should only be used once per object.
def define_element_name(element_name) self.class_variable_set(:@@ooxml_tag_name, element_name) end
def obtain_class_variable(var_name, default = {})
the setter/getter method addresses it in the context of descendant class,
addressing variable by name creates it in the context of defining class, while calling
rather than by directly addressing the name of the variable because of context issues:
Throughout this class, we are setting class variables through explicit method calls
with the passed-in +default+ (or +{}+, if not specified)
Get the value of a [sub]class variable if it exists, or create the respective variable
def obtain_class_variable(var_name, default = {}) self.class_variable_get(var_name) rescue NameError self.class_variable_set(var_name, default) end
def parse(node, known_namespaces = nil)
def parse(node, known_namespaces = nil) case node when String, IO, Zip::InputStream then node = Nokogiri::XML.parse(node) end if node.is_a?(Nokogiri::XML::Document) then node = node.root ignorable_attr = node.attributes['Ignorable'] @ignorables << ignorable_attr.value if ignorable_attr end obj = self.new obj.local_namespaces = node.namespace_definitions known_attributes = obtain_class_variable(:@@ooxml_attributes) content_params = known_attributes['_'] process_attribute(obj, node.text, content_params) if content_params node.attributes.each_pair { |attr_name, attr| attr_name = if attr.namespace then "#{attr.namespace.prefix}:#{attr.name}" else attr.name end attr_params = known_attributes[attr_name] next if attr_params.nil? # raise "Unknown attribute [#{attr_name}] for element [#{node.name}]" if attr_params.nil? process_attribute(obj, attr.value, attr_params) unless attr_params[:computed] } known_child_nodes = obtain_class_variable(:@@ooxml_child_nodes) unless known_child_nodes.empty? known_namespaces ||= obtain_class_variable(:@@ooxml_namespaces) node.element_children.each { |child_node| ns = child_node.namespace prefix = if known_namespaces.has_key?(ns.href) then known_namespaces[ns.href] else ns.prefix end child_node_name = case prefix when '', nil then child_node.name else "#{prefix}:#{child_node.name}" end child_node_params = known_child_nodes[child_node_name] raise "Unknown child node [#{child_node_name}] for element [#{node.name}]" if child_node_params.nil? parsed_object = child_node_params[:class].parse(child_node, known_namespaces) if child_node_params[:is_array] then index = parsed_object.index_in_collection collection = if (self < RubyXL::OOXMLContainerObject) then obj else obj.send(child_node_params[:accessor]) end if index.nil? then collection << parsed_object else collection[index] = parsed_object end else obj.send("#{child_node_params[:accessor]}=", parsed_object) end } end obj end
def process_attribute(obj, raw_value, params)
def process_attribute(obj, raw_value, params) val = raw_value && case params[:attr_type] when :double then Float(raw_value) # http://www.datypic.com/sc/xsd/t-xsd_double.html when :string then raw_value when Array then raw_value # Case of Simple Types when :sqref then RubyXL::Sqref.new(raw_value) when :ref then RubyXL::Reference.new(raw_value) when :bool then ['1', 'true'].include?(raw_value) # http://www.datypic.com/sc/xsd/t-xsd_boolean.html when :int then Integer(raw_value) when :uint then v = Integer(raw_value) raise ArgumentError.new("invalid value for unsigned Integer(): \"#{raw_value}\"") if v < 0 v end obj.send("#{params[:accessor]}=", val) end