Class: Axlsx::SimpleTypedList

Inherits:
Object
  • Object
show all
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)

Instance Method Summary (collapse)

Constructor Details

- (SimpleTypedList) initialize(type, serialize_as = nil)

Creats a new typed list

Parameters:

  • type (Array, Class)

    An array of Class objects or a single Class object

  • serialize (String)

    The tag name to use in serialization

Raises:

  • (ArgumentError)

    if all members of type are not Class objects



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)

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

method_mission override to pass allowed methods to the list.

Raises:

  • (ArgumentError)


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

Returns:

  • (Array)


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

Returns:

  • (Integer)


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.

Returns:

  • (String)


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

Parameters:

  • v (Any)

    the data to be added

Returns:

  • (Integer)

    returns the index of the item added.

Raises:

  • (ArgumentError)

    if the value being added is not one fo the allowed types



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

Parameters:

  • index (Integer)
  • v (Any)

Raises:

  • (ArgumentError)

    if the index is protected by locking

  • (ArgumentError)

    if the item is not one of the allowed types



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

Parameters:

  • v (Any)

    The item to be deleted.

Returns:

  • (Any)

    The item deleted

Raises:

  • (ArgumentError)

    if the item’s index is protected by locking



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

Returns:

  • (Any)

    The item deleted

Raises:

  • (ArgumentError)

    if the index is protected by locking



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

Returns:

  • (self)


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

Parameters:

  • index (Integer)

Returns:

  • (Boolean)


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

See Also:

  • <<


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.

Parameters:

  • xml (Nokogiri::XML::Builder)

    The document builder instance this objects xml will be added to.

Returns:

  • (String)


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

Returns:

  • (self)


44
45
46
47
# File 'lib/axlsx/util/simple_typed_list.rb', line 44

def unlock
  @locked_at = nil
  self
end