File: normalized_map.rb

package info (click to toggle)
ruby-sassc 2.4.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 368 kB
  • sloc: ruby: 2,277; makefile: 3
file content (117 lines) | stat: -rw-r--r-- 2,146 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

require "delegate"

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

class SassC::Util::NormalizedMap

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

  # Specifies how to transform the key.
  # This can be overridden to create other normalization behaviors.
  def normalize(key)
    key.tr("-", "_")
  end

  # 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.
  # @return [String]
  def denormalize(key)
    @key_strings[normalize(key)] || key
  end

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

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

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

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

  # @return [Hash] Hash with the keys as they were stored (before normalization).
  def as_stored
    SassC::Util.map_keys(@map) {|k| @key_strings[k]}
  end

  def empty?
    @map.empty?
  end

  def values
    @map.values
  end

  def keys
    @map.keys
  end

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

  def size
    @map.size
  end

  def to_hash
    @map.dup
  end

  def to_a
    @map.to_a
  end

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

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

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

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

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

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

end