Class: MatchMap
- Inherits:
-
Object
- Object
- MatchMap
- Defined in:
- lib/match_map.rb,
lib/match_map/version.rb
Overview
A hash-like object that tries to match an argument against all keys (using == for non-Regexp keys and pattern matching for Regexp keys)
Constant Summary collapse
- VERSION =
'3.0.0'
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#echo ⇒ Object
Returns the value of attribute echo.
Instance Method Summary collapse
- #[](arg) ⇒ Object
- #[]=(key, val) ⇒ Object
- #delete(key) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(h = {}, &blk) ⇒ MatchMap
constructor
A new instance of MatchMap.
- #normal_inner_get(arg) ⇒ Object
- #optimize! ⇒ Object
- #optimized_inner_get(arg) ⇒ Object
- #set_attrs(key, val) ⇒ Object
Constructor Details
#initialize(h = {}, &blk) ⇒ MatchMap
Returns a new instance of MatchMap.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/match_map.rb', line 10 def initialize(h = {}, &blk) @default = nil # default miss value is nil @attrs = {} # Set up the appripriate @map and define which inner_get to use # initially, the non-optimized version @map = {} define_singleton_method :inner_get, method(:normal_inner_get) # Initialize with the given hash h.each_pair do |k, v| self[k] = v end if block_given? blk.call(self) end end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
7 8 9 |
# File 'lib/match_map.rb', line 7 def default @default end |
#echo ⇒ Object
Returns the value of attribute echo.
8 9 10 |
# File 'lib/match_map.rb', line 8 def echo @echo end |
Instance Method Details
#[](arg) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/match_map.rb', line 50 def [] arg rv = [] rv.push *arg if @echo == :always if arg.is_a? Array arg.map {|s| inner = self.inner_get(s); rv.push *inner} else inner = self.inner_get arg rv.push *inner end rv.uniq! rv.compact! if rv.size == 0 if @echo == :onmiss return [*arg] else return [@default].compact end end return rv end |
#[]=(key, val) ⇒ Object
35 36 37 38 |
# File 'lib/match_map.rb', line 35 def []= key, val @map[key] = val set_attrs key, val end |
#delete(key) ⇒ Object
30 31 32 |
# File 'lib/match_map.rb', line 30 def delete key @map.delete(key) end |
#has_key?(key) ⇒ Boolean
96 97 98 |
# File 'lib/match_map.rb', line 96 def has_key? key @map.has_key? key end |
#normal_inner_get(arg) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/match_map.rb', line 75 def normal_inner_get arg rv = [] @map.keys.each do |k| if k.is_a? Regexp m = k.match arg.to_s else m = (k == arg) ? arg : false end if m v = @map[k] if v.is_a? Proc processed = v.call(m) rv.push *processed if processed else rv.push *v end end end return rv end |
#optimize! ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/match_map.rb', line 102 def optimize! singleton_class = class << self; self; end @map.each_pair do |k,v| if k.is_a? Regexp or v.is_a? Proc singleton_class.send(:define_method, :inner_get, method(:normal_inner_get)) return end end singleton_class.send(:define_method, :inner_get, method(:optimized_inner_get)) end |
#optimized_inner_get(arg) ⇒ Object
71 72 73 |
# File 'lib/match_map.rb', line 71 def optimized_inner_get arg return [@map[arg]] end |
#set_attrs(key, val) ⇒ Object
46 47 48 |
# File 'lib/match_map.rb', line 46 def set_attrs key, val @attrs[key] = {:regexkey => (key.is_a? Regexp), :procval => (val.is_a? Proc)} end |