Class: Sass::OrderedHash

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/sass/css.rb

Overview

This class is based on the Ruby 1.9 ordered hashes. It keeps the semantics and most of the efficiency of normal hashes while also keeping track of the order in which elements were set.

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Constructor Details

#initializeOrderedHash

Returns a new instance of OrderedHash.



52
53
54
# File 'lib/sass/css.rb', line 52

def initialize
  @hash = {}
end

Instance Method Details

#[](key) ⇒ Object



60
61
62
# File 'lib/sass/css.rb', line 60

def [](key)
  @hash[key] && @hash[key].value
end

#[]=(key, value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sass/css.rb', line 64

def []=(key, value)
  node = Node.new(key, value)

  if old = @hash[key]
    if old.prev
      old.prev.next = old.next
    else # old is @first and @last
      @first = @last = nil
    end
  end

  if @first.nil?
    @first = @last = node
  else
    node.prev = @last
    @last.next = node
    @last = node
  end

  @hash[key] = node
  value
end

#each {|[@first.key, @first.value]| ... } ⇒ Object

Yields:

  • ([@first.key, @first.value])


87
88
89
90
91
92
93
# File 'lib/sass/css.rb', line 87

def each
  return unless @first
  yield [@first.key, @first.value]
  node = @first
  yield [node.key, node.value] while node = node.next
  self
end

#initialize_copy(other) ⇒ Object



56
57
58
# File 'lib/sass/css.rb', line 56

def initialize_copy(other)
  @hash = other.instance_variable_get('@hash').clone
end

#valuesObject



95
96
97
# File 'lib/sass/css.rb', line 95

def values
  self.map { |k, v| v }
end