Class: TinyCss::OrderedHash
- Inherits:
-
Object
- Object
- TinyCss::OrderedHash
- Includes:
- Enumerable
- Defined in:
- lib/tiny_css/ordered_hash.rb
Instance Attribute Summary collapse
-
#keys ⇒ Object
Returns the value of attribute keys.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key, &block) ⇒ Object
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ OrderedHash
constructor
A new instance of OrderedHash.
- #inspect ⇒ Object
- #key?(key) ⇒ Boolean
- #split(*keys_to_split) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ OrderedHash
Returns a new instance of OrderedHash.
7 8 9 10 11 12 13 |
# File 'lib/tiny_css/ordered_hash.rb', line 7 def initialize self.keys = [] @hash = Hash.new { |h, k| self.keys << k.to_s h[k] = OrderedHash.new } end |
Instance Attribute Details
#keys ⇒ Object
Returns the value of attribute keys.
5 6 7 |
# File 'lib/tiny_css/ordered_hash.rb', line 5 def keys @keys end |
Instance Method Details
#[](key) ⇒ Object
15 16 17 |
# File 'lib/tiny_css/ordered_hash.rb', line 15 def [](key) @hash[key.to_s] end |
#[]=(key, value) ⇒ Object
19 20 21 22 23 |
# File 'lib/tiny_css/ordered_hash.rb', line 19 def []=(key, value) key = key.to_s @hash[key] = value self.keys << key unless self.keys.include?(key) end |
#delete(key, &block) ⇒ Object
25 26 27 28 29 |
# File 'lib/tiny_css/ordered_hash.rb', line 25 def delete(key, &block) key = key.to_s self.keys.delete key @hash.delete key, &block end |
#dup ⇒ Object
31 32 33 34 35 36 |
# File 'lib/tiny_css/ordered_hash.rb', line 31 def dup dup = super dup.instance_variable_set(:@hash, @hash.dup) dup.keys = self.keys.dup dup end |
#each(&block) ⇒ Object
38 39 40 41 |
# File 'lib/tiny_css/ordered_hash.rb', line 38 def each(&block) self.keys.each { |k| yield [k, self[k]] } self end |
#empty? ⇒ Boolean
53 54 55 |
# File 'lib/tiny_css/ordered_hash.rb', line 53 def empty? @hash.empty? end |
#inspect ⇒ Object
43 44 45 46 47 |
# File 'lib/tiny_css/ordered_hash.rb', line 43 def inspect ary = [] each { |k, v| ary << "#{ k.inspect }=>#{ v.inspect }" } "{#{ ary.join(', ') }}" end |
#key?(key) ⇒ Boolean
57 58 59 |
# File 'lib/tiny_css/ordered_hash.rb', line 57 def key?(key) keys.include?(key) end |
#split(*keys_to_split) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/tiny_css/ordered_hash.rb', line 61 def split(*keys_to_split) h = self.class.new keys.find_all {|k| keys_to_split.include?(k) }.each do |k| h[k] = delete(k) end h end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/tiny_css/ordered_hash.rb', line 49 def to_s map { |k, v| "#{ k }:#{ v }" }.join ';' end |