Class: SassC::Util::NormalizedMap

Inherits:
Object
  • Object
show all
Defined in:
lib/sassc/util/normalized_map.rb

Overview

A hash that normalizes its string keys while still allowing you to get back to the original keys that were stored. If several different values normalize to the same value, whichever is stored last wins.

Instance Method Summary collapse

Constructor Details

#initialize(map = nil) ⇒ NormalizedMap

Create a normalized map



12
13
14
15
16
# File 'lib/sassc/util/normalized_map.rb', line 12

def initialize(map = nil)
  @key_strings = {}
  @map = {}
  map.each {|key, value| self[key] = value} if map
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



109
110
111
# File 'lib/sassc/util/normalized_map.rb', line 109

def method_missing(method, *args, &block)
  @map.send(method, *args, &block)
end

Instance Method Details

#[](k) ⇒ Object



41
42
43
# File 'lib/sassc/util/normalized_map.rb', line 41

def [](k)
  @map[normalize(k)]
end

#[]=(k, v) ⇒ Object



33
34
35
36
37
38
# File 'lib/sassc/util/normalized_map.rb', line 33

def []=(k, v)
  normalized = normalize(k)
  @map[normalized] = v
  @key_strings[normalized] = k
  v
end

#as_storedHash

Returns Hash with the keys as they were stored (before normalization).

Returns:

  • (Hash)

    Hash with the keys as they were stored (before normalization).



58
59
60
# File 'lib/sassc/util/normalized_map.rb', line 58

def as_stored
  SassC::Util.map_keys(@map) {|k| @key_strings[k]}
end

#delete(k) ⇒ Object



51
52
53
54
55
# File 'lib/sassc/util/normalized_map.rb', line 51

def delete(k)
  normalized = normalize(k)
  @key_strings.delete(normalized)
  @map.delete(normalized)
end

#denormalize(key) ⇒ String

Returns the version of ‘key` as it was stored before normalization. If `key` isn’t in the map, returns it as it was passed in.

Returns:

  • (String)


28
29
30
# File 'lib/sassc/util/normalized_map.rb', line 28

def denormalize(key)
  @key_strings[normalize(key)] || key
end

#dupObject



94
95
96
97
98
# File 'lib/sassc/util/normalized_map.rb', line 94

def dup
  d = super
  d.send(:instance_variable_set, "@map", @map.dup)
  d
end

#eachObject



74
75
76
# File 'lib/sassc/util/normalized_map.rb', line 74

def each
  @map.each {|k, v| yield(k, v)}
end

#empty?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/sassc/util/normalized_map.rb', line 62

def empty?
  @map.empty?
end

#has_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/sassc/util/normalized_map.rb', line 46

def has_key?(k)
  @map.has_key?(normalize(k))
end

#keysObject



70
71
72
# File 'lib/sassc/util/normalized_map.rb', line 70

def keys
  @map.keys
end

#mapObject



90
91
92
# File 'lib/sassc/util/normalized_map.rb', line 90

def map
  @map.map {|k, v| yield(k, v)}
end

#normalize(key) ⇒ Object

Specifies how to transform the key. This can be overridden to create other normalization behaviors.



20
21
22
# File 'lib/sassc/util/normalized_map.rb', line 20

def normalize(key)
  key.tr("-", "_")
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/sassc/util/normalized_map.rb', line 113

def respond_to_missing?(method, include_private = false)
  @map.respond_to?(method, include_private)
end

#sizeObject



78
79
80
# File 'lib/sassc/util/normalized_map.rb', line 78

def size
  @map.size
end

#sort_byObject



100
101
102
# File 'lib/sassc/util/normalized_map.rb', line 100

def sort_by
  @map.sort_by {|k, v| yield k, v}
end

#to_aObject



86
87
88
# File 'lib/sassc/util/normalized_map.rb', line 86

def to_a
  @map.to_a
end

#to_hashObject



82
83
84
# File 'lib/sassc/util/normalized_map.rb', line 82

def to_hash
  @map.dup
end

#update(map) ⇒ Object



104
105
106
107
# File 'lib/sassc/util/normalized_map.rb', line 104

def update(map)
  map = map.as_stored if map.is_a?(NormalizedMap)
  map.each {|k, v| self[k] = v}
end

#valuesObject



66
67
68
# File 'lib/sassc/util/normalized_map.rb', line 66

def values
  @map.values
end