Class: Sass::Util::NormalizedMap

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/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



10
11
12
13
14
15
# File 'lib/sass/util/normalized_map.rb', line 10

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)



110
111
112
113
114
115
# File 'lib/sass/util/normalized_map.rb', line 110

def method_missing(method, *args, &block)
  if Sass.tests_running
    raise ArgumentError.new("The method #{method} must be implemented explicitly")
  end
  @map.send(method, *args, &block)
end

Instance Method Details

#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).



59
60
61
# File 'lib/sass/util/normalized_map.rb', line 59

def as_stored
  Sass::Util.map_keys(@map) {|k| @key_strings[k]}
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)


29
30
31
# File 'lib/sass/util/normalized_map.rb', line 29

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

#dup



95
96
97
98
99
# File 'lib/sass/util/normalized_map.rb', line 95

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

#each



75
76
77
# File 'lib/sass/util/normalized_map.rb', line 75

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

#empty?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/sass/util/normalized_map.rb', line 63

def empty?
  @map.empty?
end

#keys



71
72
73
# File 'lib/sass/util/normalized_map.rb', line 71

def keys
  @map.keys
end

#map



91
92
93
# File 'lib/sass/util/normalized_map.rb', line 91

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

#normalize(key)

Specifies how to transform the key.

This can be overridden to create other normalization behaviors.



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

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

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

Returns:

  • (Boolean)


117
118
119
# File 'lib/sass/util/normalized_map.rb', line 117

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

#size



79
80
81
# File 'lib/sass/util/normalized_map.rb', line 79

def size
  @map.size
end

#sort_by



101
102
103
# File 'lib/sass/util/normalized_map.rb', line 101

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

#to_a



87
88
89
# File 'lib/sass/util/normalized_map.rb', line 87

def to_a
  @map.to_a
end

#to_hash



83
84
85
# File 'lib/sass/util/normalized_map.rb', line 83

def to_hash
  @map.dup
end

#update(map)



105
106
107
108
# File 'lib/sass/util/normalized_map.rb', line 105

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

#values



67
68
69
# File 'lib/sass/util/normalized_map.rb', line 67

def values
  @map.values
end