Class: Packr::Collection

Inherits:
Map
  • Object
show all
Defined in:
lib/packr/collection.rb

Overview

A Map that is more array-like (accessible by index).

Direct Known Subclasses

RegexpGroup, Words

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Map

#get, #get_values, #has?, #union

Constructor Details

#initialize(values = nil) ⇒ Collection

Returns a new instance of Collection.



8
9
10
11
# File 'lib/packr/collection.rb', line 8

def initialize(values = nil)
  @keys = []
  super(values)
end

Instance Attribute Details

#keys=(value) ⇒ Object (writeonly)

Sets the attribute keys

Parameters:

  • value

    the value to set the attribute keys to.



6
7
8
# File 'lib/packr/collection.rb', line 6

def keys=(value)
  @keys = value
end

#valuesObject (readonly)

Returns the value of attribute values.



5
6
7
# File 'lib/packr/collection.rb', line 5

def values
  @values
end

Class Method Details

.create(key, item) ⇒ Object



141
142
143
144
# File 'lib/packr/collection.rb', line 141

def self.create(key, item)
  begin; klass = self::Item; rescue; end
  klass ? klass.new(key, item) : item
end

Instance Method Details

#add(key, item = nil) ⇒ Object



13
14
15
16
17
18
# File 'lib/packr/collection.rb', line 13

def add(key, item = nil)
  # Duplicates not allowed using add().
  # But you can still overwrite entries using put().
  return if has?(key)
  put(key, item)
end

#clearObject



20
21
22
23
# File 'lib/packr/collection.rb', line 20

def clear
  super
  @keys.clear
end

#copyObject



25
26
27
28
29
# File 'lib/packr/collection.rb', line 25

def copy
  copy = super
  copy.keys = @keys.dup
  copy
end

#eachObject



31
32
33
# File 'lib/packr/collection.rb', line 31

def each
  @keys.each { |key| yield(get(key), key) }
end

#get_at(index) ⇒ Object



35
36
37
38
39
# File 'lib/packr/collection.rb', line 35

def get_at(index)
  index += @keys.length if index < 0 # starting from the end
  key = @keys[index]
  key.nil? ? nil : @values[key.to_s]
end

#get_keysObject



41
42
43
# File 'lib/packr/collection.rb', line 41

def get_keys
  @keys.dup
end

#index_of(key) ⇒ Object



45
46
47
# File 'lib/packr/collection.rb', line 45

def index_of(key)
  @keys.index(key.to_s)
end

#insert_at(index, key, item = nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/packr/collection.rb', line 49

def insert_at(index, key, item = nil)
  return if @keys[index].nil?
  @keys.insert(index, key.to_s)
  @values[key.to_s] = nil # placeholder
  put(key, item)
end

#item(key_or_index) ⇒ Object



56
57
58
# File 'lib/packr/collection.rb', line 56

def item(key_or_index)
  __send__(key_or_index.is_a?(Numeric) ? :get_at : :get, key_or_index)
end

#mapObject



60
61
62
# File 'lib/packr/collection.rb', line 60

def map
  @keys.map { |key| yield(get(key), key) }
end

#merge(*args) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/packr/collection.rb', line 64

def merge(*args)
  args.each do |values|
    values.is_a?(Collection) ?
        values.each { |item, key| put(key, item) } :
        super(values)
  end
  self
end

#put(key, item = nil) ⇒ Object

TODO update this method



74
75
76
77
78
79
80
81
# File 'lib/packr/collection.rb', line 74

def put(key, item = nil)
  item ||= key
  @keys << key.to_s unless has?(key.to_s)
  begin; klass = self.class::Item; rescue; end
  item = self.class.create(key, item) if klass and !item.is_a?(klass)
  @values[key.to_s] = item
  self
end

#put_at(index, item = nil) ⇒ Object



83
84
85
86
87
# File 'lib/packr/collection.rb', line 83

def put_at(index, item = nil)
  key = @keys[index]
  return if key.nil?
  put(key, item)
end

#remove(key) ⇒ Object



89
90
91
92
93
94
# File 'lib/packr/collection.rb', line 89

def remove(key)
  if has?(key)
    @keys.delete(key.to_s)
    @values.delete(key.to_s)
  end
end

#remove_at(index) ⇒ Object



96
97
98
99
# File 'lib/packr/collection.rb', line 96

def remove_at(index)
  key = @keys.delete_at(index)
  @values.delete(key)
end

#reverse!Object



101
102
103
104
# File 'lib/packr/collection.rb', line 101

def reverse!
  @keys.reverse!
  self
end

#sizeObject



106
107
108
# File 'lib/packr/collection.rb', line 106

def size
  @keys.length
end

#slice(start, fin) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/packr/collection.rb', line 110

def slice(start, fin)
  sliced = copy
  if start
    keys, removed = @keys, @keys
    sliced.keys = @keys[start...fin]
    if sliced.size.nonzero?
      removed = removed[0...start]
      removed = removed + keys[fin..-1] if fin
    end
    removed.each do |remov|
      sliced.values.delete(remov)
    end
  end
  sliced
end

#sort!(&compare) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/packr/collection.rb', line 126

def sort!(&compare)
  if block_given?
    @keys.sort! do |key1, key2|
      compare.call(@values[key1], @values[key2], key1, key2)
    end
  else
    @keys.sort!
  end
  self
end

#to_sObject



137
138
139
# File 'lib/packr/collection.rb', line 137

def to_s
  "(#{@keys.join(',')})"
end