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

allowed_types[R]

The class constants of allowed types @return [Array]

locked_at[R]

The index below which items cannot be removed @return [Integer]

serialize_as[R]

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

new(type, serialize_as=nil) click to toggle source

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

+(v) click to toggle source

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
<<(v) click to toggle source

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
Also aliased as: push
==(v) click to toggle source

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
[]=(index, v) click to toggle source

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(v) click to toggle source

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_at(index) click to toggle source

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
insert(index, v) click to toggle source

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() click to toggle source

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
protected?(index) click to toggle source

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
push(v)
Alias for: <<
to_a()
Alias for: to_ary
to_ary() click to toggle source
# File lib/axlsx/util/simple_typed_list.rb, line 64
def to_ary
  @list
end
Also aliased as: to_a
to_xml_string(str = '') click to toggle source
# 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
transpose() { |column_index, row_index| ... } click to toggle source

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() click to toggle source

Unlock the list @return [self]

# File lib/axlsx/util/simple_typed_list.rb, line 72
def unlock
  @locked_at = nil
  self
end