Method: RubyXL::OOXMLObjectClassMethods#define_attribute
- Defined in:
- lib/rubyXL/objects/ooxml_object.rb
permalink #define_attribute(attr_name, attr_type, extra_params = nil) ⇒ Object
Defines an attribute of OOXML object.
Parameters
-
attribute_name
- Name of the element attribute as seen in the source XML. Can be either"String"
or:Symbol
-
Special attribute name
'_'
(underscore) denotes the value of the element rather than attribute.
-
-
attribute_type
- Specifies the conversion type for the attribute when parsing. Available options are:-
:int
-Integer
-
:uint
- UnsignedInteger
-
:double
-Float
</u> -
:string
-String
(no conversion) -
:sqref
- RubyXL::Sqref -
:ref
- RubyXL::Reference -
:bool
-Boolean
(“1” and “true” convert totrue
, others tofalse
) -
one of
simple_types
-String
, plus the list of acceptable values is saved for future validation (not used yet).
-
-
extra_parameters
- Hash of optional parameters as follows:-
:accessor
- Name of the accessor for this attribute to be defined on the object. If not provided, defaults to classidiedattribute_name
. -
:default
- Value this attribute defaults to if not explicitly provided. -
:required
- Whether this attribute is required when writing XML. If the value of the attrinute is not explicitly provided,:default
is written instead. -
:computed
- Do not store this attribute onparse
, but do call the object-provided read accessor onwrite_xml
.
-
Examples
define_attribute(:outline, :bool, :default => true)
A Boolean
attribute ‘outline’ with default value true
will be accessible by calling obj.outline
define_attribute(:uniqueCount, :int)
An Integer
attribute ‘uniqueCount’ accessible as obj.unique_count
define_attribute(:_, :string, :accessor => :expression)
The value of the element will be accessible as a String
by calling obj.expression
define_attribute(:errorStyle, %w{ stop warning information }, :default => 'stop',)
A String
attribute named ‘errorStyle’ will be accessible as obj.error_style
, valid values are "stop"
, "warning"
, "information"
48 49 50 51 52 53 54 55 |
# File 'lib/rubyXL/objects/ooxml_object.rb', line 48 def define_attribute(attr_name, attr_type, extra_params = nil) attrs = obtain_class_variable(:@@ooxml_attributes) attr_hash = { :attr_type => attr_type } attr_hash.merge!(extra_params) if extra_params 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 |