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

Attributes

allowed_types[R]

The class constants of allowed types @return [Array]

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, start_size = 0) 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, start_size = 0
  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
  @serialize_as = serialize_as unless serialize_as.nil?
  @list = Array.new(start_size)
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 84
def +(v)
  v.each do |item| 
    DataTypeValidator.validate :SimpleTypedList_plus, @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 95
def <<(v)
  DataTypeValidator.validate :SimpleTypedList_push, @allowed_types, v
  @list << v
  @list.size - 1
end
Also aliased as: push
[]=(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 128
def []=(index, v)
  DataTypeValidator.validate :SimpleTypedList_insert, @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 108
def delete(v)
  return unless include? v
  raise ArgumentError, "Item is protected and cannot be deleted" if protected? 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 117
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 140
def insert(index, v)
  DataTypeValidator.validate :SimpleTypedList_insert, @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 61
def lock
  @locked_at = @list.size
  self
end
locked_at() click to toggle source

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

# File lib/axlsx/util/simple_typed_list.rb, line 29
def locked_at
  defined?(@locked_at) ? @locked_at : nil
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 149
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 73
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 168
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="' << size.to_s << '">')
  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 40
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 68
def unlock
  @locked_at = nil
  self
end