class Axlsx::SimpleTypedList
A SimpleTypedList
is a type restrictive collection that allows some of the methods from Array and supports basic xml serialization. @private
Constants
- DELEGATES
- DESTRUCTIVE
method_mission override to pass allowed methods to the list. @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 :clear
Attributes
The class constants of allowed types @return [Array]
The index below which items cannot be removed @return [Integer]
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. @return [String]
Public Class Methods
Creats a new typed list @param [Array, Class] type An array of Class objects or a single Class object @param [String] serialize_as
The tag name to use in serialization @raise [ArgumentError] if all members of type are not Class objects
# File lib/axlsx/util/simple_typed_list.rb, line 11 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
Public Instance Methods
join operator @param [Array] v the array to join @raise [ArgumentError] if any of the values being joined are not one of the allowed types @return [SimpleTypedList]
# File lib/axlsx/util/simple_typed_list.rb, line 82 def +(v) v.each do |item| DataTypeValidator.validate "SimpleTypedList.+", @allowed_types, item @list << item end end
Concat operator @param [Any] v the data to be added @raise [ArgumentError] if the value being added is not one fo the allowed types @return [Integer] returns the index of the item added.
# File lib/axlsx/util/simple_typed_list.rb, line 93 def <<(v) DataTypeValidator.validate "SimpleTypedList.<<", @allowed_types, v @list << v @list.size - 1 end
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.
# File lib/axlsx/util/simple_typed_list.rb, line 152 def ==(v) v == @list end
positional assignment. Adds the item at the index specified @param [Integer] index @param [Any] v @raise [ArgumentError] if the index is protected by locking @raise [ArgumentError] if the item is not one of the allowed types
# File lib/axlsx/util/simple_typed_list.rb, line 124 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 the item from the list @param [Any] v The item to be deleted. @raise [ArgumentError] if the item’s index is protected by locking @return [Any] The item deleted
# File lib/axlsx/util/simple_typed_list.rb, line 104 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 the item from the list at the index position provided @raise [ArgumentError] if the index is protected by locking @return [Any] The item deleted
# File lib/axlsx/util/simple_typed_list.rb, line 113 def delete_at(index) @list[index] raise ArgumentError, "Item is protected and cannot be deleted" if protected? index @list.delete_at index end
inserts an item at the index specfied @param [Integer] index @param [Any] v @raise [ArgumentError] if the index is protected by locking @raise [ArgumentError] if the index is not one of the allowed types
# File lib/axlsx/util/simple_typed_list.rb, line 136 def insert(index, v) DataTypeValidator.validate "SimpleTypedList.<<", @allowed_types, v raise ArgumentError, "Item is protected and cannot be changed" if protected? index @list.insert(index, v) v end
Lock this list at the current size @return [self]
# File lib/axlsx/util/simple_typed_list.rb, line 59 def lock @locked_at = @list.size self end
determines if the index is protected @param [Integer] index
# File lib/axlsx/util/simple_typed_list.rb, line 145 def protected? index return false unless @locked_at.is_a? Fixnum index < @locked_at end
# File lib/axlsx/util/simple_typed_list.rb, line 64 def to_ary @list end
# File lib/axlsx/util/simple_typed_list.rb, line 192 def to_xml_string(str = '') classname = @allowed_types[0].name.split('::').last el_name = serialize_as.to_s || (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
Transposes the list (without blowing up like ruby does) any non populated cell in the matrix will be a nil value
# File lib/axlsx/util/simple_typed_list.rb, line 39 def transpose return @list.clone if @list.size == 0 row_count = @list.size max_column_count = @list.map{|row| row.cells.size}.max result = Array.new(max_column_count) { Array.new(row_count) } # yes, I know it is silly, but that warning is really annoying row_count.times do |row_index| max_column_count.times do |column_index| datum = if @list[row_index].cells.size >= max_column_count @list[row_index].cells[column_index] elsif block_given? yield(column_index, row_index) end result[column_index][row_index] = datum end end result end
Unlock the list @return [self]
# File lib/axlsx/util/simple_typed_list.rb, line 72 def unlock @locked_at = nil self end