Class: OrderedSet

Inherits:
BlankSlate show all
Defined in:
lib/ramaze/snippets/ordered_set.rb

Overview

Basically an Set, but with Order, ain’t that obivous?

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ OrderedSet

Create new instances, optionally pass the first set



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ramaze/snippets/ordered_set.rb', line 13

def initialize(*args)
  if args.size == 1
    @set = args.shift
  else
    @set = *args
  end

  @set ||= []
  @set = [@set] unless ::Array === @set
  @set.uniq!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Delegate everything, but controlled, keep elements unique. Warning, this is not really atomic.



48
49
50
# File 'lib/ramaze/snippets/ordered_set.rb', line 48

def method_missing(meth, *args, &block)
  @set.__send__(meth, *args, &block)
end

Class Method Details

.[](*args) ⇒ Object



8
9
10
# File 'lib/ramaze/snippets/ordered_set.rb', line 8

def self.[](*args)
  new(*args)
end

Instance Method Details

#[]=(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ramaze/snippets/ordered_set.rb', line 34

def []= *args
  @set.map! do |e|
    if ::Array === args.last
      args.last.include?(e) ? nil : e
    else
      args.last == e ? nil : e
    end
  end
  @set.__send__(:[]=, *args)
  @set.compact!
end