Class: Sass::OrderedHash

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/gems/haml-2.0.4/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.



58
59
60
# File 'lib/gems/haml-2.0.4/lib/sass/css.rb', line 58

def initialize
  @hash = {}
end

Instance Method Details

#[](key) ⇒ Object



66
67
68
# File 'lib/gems/haml-2.0.4/lib/sass/css.rb', line 66

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

#[]=(key, value) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gems/haml-2.0.4/lib/sass/css.rb', line 70

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])


93
94
95
96
97
98
99
# File 'lib/gems/haml-2.0.4/lib/sass/css.rb', line 93

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



62
63
64
# File 'lib/gems/haml-2.0.4/lib/sass/css.rb', line 62

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

#valuesObject



101
102
103
# File 'lib/gems/haml-2.0.4/lib/sass/css.rb', line 101

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