Class: Axlsx::SimpleTypedList
- Inherits:
-
Object
- Object
- Axlsx::SimpleTypedList
- Defined in:
- lib/axlsx/util/simple_typed_list.rb
Overview
A SimpleTypedList is a type restrictive collection that allows some of the methods from Array and supports basic xml serialization.
Direct Known Subclasses
Comments, ContentType, Relationships, TableStyle, TableStyles
Constant Summary
- DESTRUCTIVE =
Note:
the following methods are not allowed
:replace :insert :collect! :map! :pop :delete_if :reverse! :shift :shuffle! :slice! :sort! :uniq! :unshift :zip :flatten! :fill :drop :drop_while :delete_if :clearmethod_mission override to pass allowed methods to the list.
['replace', 'insert', 'collect!', 'map!', 'pop', 'delete_if', 'reverse!', 'shift', 'shuffle!', 'slice!', 'sort!', 'uniq!', 'unshift', 'zip', 'flatten!', 'fill', 'drop', 'drop_while', 'delete_if', 'clear']
- DELEGATES =
Array.instance_methods - self.instance_methods - DESTRUCTIVE
Instance Attribute Summary (collapse)
-
- (Array) allowed_types
readonly
The class constants of allowed types.
-
- (Integer) locked_at
readonly
The index below which items cannot be removed.
-
- (String) serialize_as
readonly
The tag name to use when serializing this object by default the parent node for all items in the list is the classname of the first allowed type with the first letter in lowercase.
Instance Method Summary (collapse)
-
- (Integer) <<(v)
Concat operator.
-
- (Object) ==(v)
override the equality method so that this object can be compared to a simple array.
-
- (Object) []=(index, v)
positional assignment.
-
- (Any) delete(v)
delete the item from the list.
-
- (Any) delete_at(index)
delete the item from the list at the index position provided.
-
- (SimpleTypedList) initialize(type, serialize_as = nil)
constructor
Creats a new typed list.
-
- (self) lock
Lock this list at the current size.
-
- (Boolean) protected?(index)
determines if the index is protected.
-
- (Object) push(v)
alternate of << method.
- - (Object) to_ary
-
- (String) to_xml(xml)
Serializes the list If the serialize_as property is set, it is used as the parent node name.
- - (Object) to_xml_string(str = '')
-
- (self) unlock
Unlock the list.
Constructor Details
- (SimpleTypedList) initialize(type, serialize_as = nil)
Creats a new typed list
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 23 def initialize type, serialize_as=nil if type.is_a? Array type.each { |item| raise ArgumentError, "All members of type must be Class objects" unless item.is_a? Class } @allowed_types = type else raise ArgumentError, "Type must be a Class object or array of Class objects" unless type.is_a? Class @allowed_types = [type] end @list = [] @locked_at = nil @serialize_as = serialize_as end |
Instance Attribute Details
- (Array) allowed_types (readonly)
The class constants of allowed types
8 9 10 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 8 def allowed_types @allowed_types end |
- (Integer) locked_at (readonly)
The index below which items cannot be removed
12 13 14 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 12 def locked_at @locked_at end |
- (String) serialize_as (readonly)
The tag name to use when serializing this object by default the parent node for all items in the list is the classname of the first allowed type with the first letter in lowercase.
17 18 19 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 17 def serialize_as @serialize_as end |
Instance Method Details
- (Integer) <<(v)
Concat operator
58 59 60 61 62 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 58 def <<(v) DataTypeValidator.validate "SimpleTypedList.<<", @allowed_types, v @list << v @list.size - 1 end |
- (Object) ==(v)
override the equality method so that this object can be compared to a simple array. if this object's list is equal to the specifiec array, we return true.
110 111 112 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 110 def ==(v) v == @list end |
- (Object) []=(index, v)
positional assignment. Adds the item at the index specified
94 95 96 97 98 99 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 94 def []=(index, v) DataTypeValidator.validate "SimpleTypedList.<<", @allowed_types, v raise ArgumentError, "Item is protected and cannot be changed" if protected? index @list[index] = v v end |
- (Any) delete(v)
delete the item from the list
74 75 76 77 78 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 74 def delete(v) return unless @list.include? v raise ArgumentError, "Item is protected and cannot be deleted" if protected? @list.index(v) @list.delete v end |
- (Any) delete_at(index)
delete the item from the list at the index position provided
83 84 85 86 87 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 83 def delete_at(index) @list[index] raise ArgumentError, "Item is protected and cannot be deleted" if protected? index @list.delete_at index end |
- (self) lock
Lock this list at the current size
38 39 40 41 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 38 def lock @locked_at = @list.size self end |
- (Boolean) protected?(index)
determines if the index is protected
103 104 105 106 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 103 def protected? index return false unless @locked_at.is_a? Fixnum index < @locked_at end |
- (Object) push(v)
alternate of << method
66 67 68 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 66 def push(v) self.<< v end |
- (Object) to_ary
43 44 45 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 43 def to_ary @list end |
- (String) to_xml(xml)
Serializes the list If the serialize_as property is set, it is used as the parent node name. If the serialize_as property is nil, the first item in the list of allowed_types will be used, having the first letter of the class changed to lower case.
163 164 165 166 167 168 169 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 163 def to_xml(xml) classname = @allowed_types[0].name.split('::').last el_name = serialize_as || (classname[0,1].downcase + classname[1..-1]) xml.send(el_name, :count=>@list.size) { @list.each { |item| item.to_xml(xml) } } end |
- (Object) to_xml_string(str = '')
150 151 152 153 154 155 156 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 150 def to_xml_string(str = '') classname = @allowed_types[0].name.split('::').last el_name = serialize_as || (classname[0,1].downcase + classname[1..-1]) str << '<' << el_name << ' count="' << @list.size.to_s << '">' @list.each { |item| item.to_xml_string(str) } str << '</' << el_name << '>' end |
- (self) unlock
Unlock the list
49 50 51 52 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 49 def unlock @locked_at = nil self end |