Class: Tire::Alias::IndexCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tire/alias.rb

Overview

Thin wrapper around array representing a collection of indices for a specific alias, which allows hooking into adding/removing indices.

It keeps track of which aliases to add and which to remove in two separate collections, ‘add_indices` and `remove_indices`.

It delegates Enumerable-like methods to the ‘add_indices` collection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ IndexCollection

Returns a new instance of IndexCollection.



236
237
238
239
# File 'lib/tire/alias.rb', line 236

def initialize(*values)
  @add_indices    = Array.new(values).flatten.compact
  @remove_indices = []
end

Instance Attribute Details

#add_indicesObject (readonly)

Returns the value of attribute add_indices.



234
235
236
# File 'lib/tire/alias.rb', line 234

def add_indices
  @add_indices
end

#remove_indicesObject (readonly)

Returns the value of attribute remove_indices.



234
235
236
# File 'lib/tire/alias.rb', line 234

def remove_indices
  @remove_indices
end

Instance Method Details

#[](index) ⇒ Object



272
273
274
# File 'lib/tire/alias.rb', line 272

def [](index)
  @add_indices[index]
end

#clearObject



267
268
269
270
# File 'lib/tire/alias.rb', line 267

def clear
  @remove_indices = @add_indices.clone
  @add_indices.clear
end

#delete(value) ⇒ Object Also known as: remove



247
248
249
250
# File 'lib/tire/alias.rb', line 247

def delete(value)
  @add_indices.delete value
  @remove_indices |= [value]
end

#delete_if(&block) ⇒ Object



253
254
255
256
257
# File 'lib/tire/alias.rb', line 253

def delete_if(&block)
  @add_indices.clone.each do |name|
    delete(name) if block.call(name)
  end
end

#each(&block) ⇒ Object



259
260
261
# File 'lib/tire/alias.rb', line 259

def each(&block)
  @add_indices.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/tire/alias.rb', line 263

def empty?
  @add_indices.empty?
end

#inspectObject



288
289
290
# File 'lib/tire/alias.rb', line 288

def inspect
  %Q|<#{self.class} #{@add_indices.map{|i| "\"#{i}\""}.join(', ')}>|
end

#push(value) ⇒ Object Also known as: add



241
242
243
244
# File 'lib/tire/alias.rb', line 241

def push(value)
  @add_indices |= [value]
  @remove_indices.delete value
end

#sizeObject



276
277
278
# File 'lib/tire/alias.rb', line 276

def size
  @add_indices.size
end

#to_aryObject



280
281
282
# File 'lib/tire/alias.rb', line 280

def to_ary
  @add_indices
end

#to_sObject



284
285
286
# File 'lib/tire/alias.rb', line 284

def to_s
  @add_indices.join(', ')
end