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

Comments, ContentType, Relationships, TableStyle, TableStyles

Constant Summary

DESTRUCTIVE =
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.

['replace', 'insert', 'collect!', 'map!', 'pop', 'delete_if',
'reverse!', 'shift', 'shuffle!', 'slice!', 'sort!', 'uniq!',
'unshift', 'zip', 'flatten!', 'fill', 'drop', 'drop_while',
'delete_if', 'clear']
DELEGATES =
Array.instance_methods - self.instance_methods - DESTRUCTIVE

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



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/axlsx/util/simple_typed_list.rb', line 23

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

Instance Attribute Details

- (Array) allowed_types (readonly)

The class constants of allowed types

Returns:

  • (Array)


8
9
10
# File 'lib/axlsx/util/simple_typed_list.rb', line 8

def allowed_types
  @allowed_types
end

- (Integer) locked_at (readonly)

The index below which items cannot be removed

Returns:

  • (Integer)


12
13
14
# File 'lib/axlsx/util/simple_typed_list.rb', line 12

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)


17
18
19
# File 'lib/axlsx/util/simple_typed_list.rb', line 17

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



58
59
60
61
62
# File 'lib/axlsx/util/simple_typed_list.rb', line 58

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.



110
111
112
# File 'lib/axlsx/util/simple_typed_list.rb', line 110

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



94
95
96
97
98
99
# File 'lib/axlsx/util/simple_typed_list.rb', line 94

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



74
75
76
77
78
# File 'lib/axlsx/util/simple_typed_list.rb', line 74

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



83
84
85
86
87
# File 'lib/axlsx/util/simple_typed_list.rb', line 83

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)


38
39
40
41
# File 'lib/axlsx/util/simple_typed_list.rb', line 38

def lock
  @locked_at = @list.size
  self
end

- (Boolean) protected?(index)

determines if the index is protected

Parameters:

  • index (Integer)

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/axlsx/util/simple_typed_list.rb', line 103

def protected? index
  return false unless @locked_at.is_a? Fixnum
  index < @locked_at
end

- (Object) push(v)

alternate of << method

See Also:

  • <<


66
67
68
# File 'lib/axlsx/util/simple_typed_list.rb', line 66

def push(v)
  self.<< v
end

- (Object) to_ary



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

def to_ary
  @list
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)


163
164
165
166
167
168
169
# File 'lib/axlsx/util/simple_typed_list.rb', line 163

def to_xml(xml)
  classname = @allowed_types[0].name.split('::').last
  el_name = serialize_as || (classname[0,1].downcase + classname[1..-1])
  xml.send(el_name, :count=>@list.size) {
    @list.each { |item| item.to_xml(xml) }
  }
end

- (Object) to_xml_string(str = '')



150
151
152
153
154
155
156
# File 'lib/axlsx/util/simple_typed_list.rb', line 150

def to_xml_string(str = '')
  classname = @allowed_types[0].name.split('::').last
  el_name = serialize_as || (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

- (self) unlock

Unlock the list

Returns:

  • (self)


49
50
51
52
# File 'lib/axlsx/util/simple_typed_list.rb', line 49

def unlock
  @locked_at = nil
  self
end