Class: Rote::ExtHash
- Inherits:
-
Object
- Object
- Rote::ExtHash
- Defined in:
- lib/rote/rotetasks.rb
Overview
Special type of Hash that uses Regexp keys and maintains insertion order. When searching for a string, the first match (of either kind) is used. Allows backreferences from the key match to be used in the value with $1..$n notation in val str.
Entries are kept in insertion order. Searches/insertion are slow, iteration is constant time. It’s basically an unbucketed hash.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetch the first matching data.
-
#[]=(key, value) ⇒ Object
Insert the given regex key unless it already exists.
-
#fetch_entry(key) ⇒ Object
Fetch a single entry based on key equality.
-
#initialize(map = nil) ⇒ ExtHash
constructor
Create a new RxHash, copying the supplied map (in random order).
-
#member?(key) ⇒ Boolean
Determine membership based on key equality.
Constructor Details
#initialize(map = nil) ⇒ ExtHash
Create a new RxHash, copying the supplied map (in random order).
73 74 75 76 |
# File 'lib/rote/rotetasks.rb', line 73 def initialize(map = nil) @data = [] map.each { |k,v| self[k] = v } if map end |
Instance Method Details
#[](key) ⇒ Object
Fetch the first matching data.
88 89 90 91 92 93 94 |
# File 'lib/rote/rotetasks.rb', line 88 def [](key) md = nil if v = @data.detect { |it| md = /^#{it[0]}$/.match(key.to_s) } v[1][0].gsub!(/\$(\d)/) { md[$1.to_i] } v[1] end end |
#[]=(key, value) ⇒ Object
Insert the given regex key unless it already exists. You may use string representations for the keys, but they are converted as-is to regexps.
Returns the value that was inserted, or nil.
83 84 85 |
# File 'lib/rote/rotetasks.rb', line 83 def []=(key,value) @data << [key,value] unless member?(key) end |
#fetch_entry(key) ⇒ Object
Fetch a single entry based on key equality.
97 98 99 |
# File 'lib/rote/rotetasks.rb', line 97 def fetch_entry(key) @data.detect { |it| it[0] == key } end |
#member?(key) ⇒ Boolean
Determine membership based on key equality.
102 103 104 |
# File 'lib/rote/rotetasks.rb', line 102 def member?(key) true if fetch_entry(key) end |