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
-
#allowed_types ⇒ Array
readonly
The class constants of allowed types.
-
#locked_at ⇒ Integer
readonly
The index below which items cannot be removed.
-
#serialize_as ⇒ String
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
-
#<<(v) ⇒ Integer
Concat operator.
-
#==(v) ⇒ Object
override the equality method so that this object can be compared to a simple array.
-
#[]=(index, v) ⇒ Object
positional assignment.
-
#delete(v) ⇒ Any
delete the item from the list.
-
#delete_at(index) ⇒ Any
delete the item from the list at the index position provided.
-
#initialize(type, serialize_as = nil) ⇒ SimpleTypedList
constructor
Creats a new typed list.
-
#lock ⇒ self
Lock this list at the current size.
-
#method_missing(meth, *args, &block) ⇒ Object
method_mission override to pass allowed methods to the list.
-
#protected?(index) ⇒ Boolean
determines if the index is protected.
-
#push(v) ⇒ Object
alternate of << method.
-
#to_xml(xml) ⇒ String
Serializes the list If the serialize_as property is set, it is used as the parent node name.
-
#unlock ⇒ self
Unlock the list.
Constructor Details
#initialize(type, serialize_as = nil) ⇒ SimpleTypedList
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
#method_missing(meth, *args, &block) ⇒ Object
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
#allowed_types ⇒ Array (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 |
#locked_at ⇒ Integer (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 |
#serialize_as ⇒ String (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
#<<(v) ⇒ Integer
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 |
#==(v) ⇒ Object
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 |
#[]=(index, v) ⇒ Object
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 |
#delete(v) ⇒ Any
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 |
#delete_at(index) ⇒ Any
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 |
#lock ⇒ self
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 |
#protected?(index) ⇒ Boolean
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 |
#push(v) ⇒ Object
alternate of << method
61 62 63 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 61 def push(v) self.<< v end |
#to_xml(xml) ⇒ String
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 |
#unlock ⇒ self
Unlock the list
44 45 46 47 |
# File 'lib/axlsx/util/simple_typed_list.rb', line 44 def unlock @locked_at = nil self end |