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
CatAxisData, ContentType, Relationships, TableStyle, TableStyles
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.
-
- (Object) method_missing(meth, *args, &block)
method_mission override to pass allowed methods to the list.
-
- (Boolean) protected?(index)
determines if the index is protected.
-
- (Object) push(v)
alternate of << method.
-
- (String) to_xml(xml)
Serializes the list If the serialize_as property is set, it is used as the parent node name.
-
- (self) unlock
Unlock the list.
Constructor Details
- (SimpleTypedList) initialize(type, serialize_as = nil)
Creats a new typed list
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 22 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(meth, *args, &block)
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 :clear
method_mission override to pass allowed methods to the list.
131 132 133 134 135 136 137 138 139 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 131 def method_missing(meth, *args, &block) raise ArgumentError, "#{meth} not supported" if [:replace, :insert, :collect!, :map!, :pop, :delete_if, :reverse!, :shift, :shuffle!, :slice!, :sort!, :uniq!, :unshift, :zip, :flatten!, :fill, :drop, :drop_while, :delete_if, :clear].include? meth.to_sym if @list.respond_to? meth @list.send(meth, *args, &block) else puts "method:#{meth.inspect}" super end end |
Instance Attribute Details
- (Array) allowed_types (readonly)
The class constants of allowed types
7 8 9 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 7 def allowed_types @allowed_types end |
- (Integer) locked_at (readonly)
The index below which items cannot be removed
11 12 13 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 11 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.
16 17 18 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 16 def serialize_as @serialize_as end |
Instance Method Details
- (Integer) <<(v)
Concat operator
53 54 55 56 57 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 53 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.
105 106 107 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 105 def ==(v) v == @list end |
- (Object) []=(index, v)
positional assignment. Adds the item at the index specified
89 90 91 92 93 94 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 89 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
69 70 71 72 73 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 69 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
78 79 80 81 82 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 78 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
37 38 39 40 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 37 def lock @locked_at = @list.size self end |
- (Boolean) protected?(index)
determines if the index is protected
98 99 100 101 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 98 def protected? index return false unless @locked_at.is_a? Fixnum index < @locked_at end |
- (Object) push(v)
alternate of << method
61 62 63 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 61 def push(v) self.<< v 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.
146 147 148 149 150 151 152 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 146 def to_xml(xml) classname = @allowed_types[0].name.split('::').last el_name = serialize_as || (classname[0,1].downcase + classname[1..-1]).pluralize xml.send(el_name, :count=>@list.size) { @list.each { |item| item.to_xml(xml) } } end |
- (self) unlock
Unlock the list
44 45 46 47 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 44 def unlock @locked_at = nil self end |