aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/ordered_options.rb
blob: 3952ba41f29780270ce2fe052abffe0e63bb3d4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class OrderedOptions < Array
  def []=(key, value)
    key = key.to_sym
    
    if pair = find_pair(key)
      pair.pop
      pair << value
    else
      self << [key, value]
    end
  end
  
  def [](key)
    pair = find_pair(key.to_sym)
    pair ? pair.last : nil
  end

  private
    def find_pair(key)
      self.each { |i| return i if i.first == key }
      return false
    end
end