Class: Packr::RegexpGroup
- Inherits:
-
Object
- Object
- Packr::RegexpGroup
- Defined in:
- lib/packr/regexp_group.rb
Defined Under Namespace
Classes: Item
Constant Summary collapse
- IGNORE =
"\\0"
- BACK_REF =
/\\(\d+)/
- ESCAPE_CHARS =
/\\./
- ESCAPE_BRACKETS =
/\(\?[:=!]|\[[^\]]+\]/
- BRACKETS =
/\(/
- KEYS =
"~"
Instance Attribute Summary collapse
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #exec(string, &replacement) ⇒ Object
-
#initialize(values, flags = nil) ⇒ RegexpGroup
constructor
A new instance of RegexpGroup.
- #test(string) ⇒ Object
- #to_s ⇒ Object
- #union(*args) ⇒ Object
- #value_of(type = nil) ⇒ Object
Constructor Details
#initialize(values, flags = nil) ⇒ RegexpGroup
Returns a new instance of RegexpGroup.
13 14 15 16 17 18 19 |
# File 'lib/packr/regexp_group.rb', line 13 def initialize(values, flags = nil) @values = [] values.each { |key, value| @values << Item.new(key, value) } if flags && flags.is_a(String) @ignore_case = !!(flags =~ /i/) end end |
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values.
4 5 6 |
# File 'lib/packr/regexp_group.rb', line 4 def values @values end |
Class Method Details
.count(expression) ⇒ Object
116 117 118 119 |
# File 'lib/packr/regexp_group.rb', line 116 def self.count(expression) expression = expression.to_s.gsub(ESCAPE_CHARS, "").gsub(ESCAPE_BRACKETS, "") expression.scan(BRACKETS).length end |
Instance Method Details
#exec(string, &replacement) ⇒ Object
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 |
# File 'lib/packr/regexp_group.rb', line 30 def exec(string, &replacement) string = string.to_s regexp = value_of replacement ||= lambda do |match| return "" if match.nil? arguments = [match] + $~.captures + [$~.begin(0), string] offset, result = 1, "" @values.each do |item| nxt = offset + item.length + 1 if arguments[offset] # do we have a result? rep = item.replacement if rep.is_a?(Proc) args = arguments[offset...nxt] index = arguments[-2] result = rep.call *(args + [index, string]) else result = rep.is_a?(Numeric) ? arguments[offset + rep] : rep.to_s end end offset = nxt end result end replacement.is_a?(Proc) ? string.gsub(regexp, &replacement) : string.gsub(regexp, replacement.to_s) end |
#test(string) ⇒ Object
59 60 61 |
# File 'lib/packr/regexp_group.rb', line 59 def test(string) exec(string) != string end |
#to_s ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/packr/regexp_group.rb', line 63 def to_s length = 0 "(" + @values.map { |item| # Fix back references. ref = item.to_s.gsub(BACK_REF) { |m| "\\" + (1 + $1.to_i + length).to_s } length += item.length + 1 ref }.join(")|(") + ")" end |
#union(*args) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/packr/regexp_group.rb', line 21 def union(*args) values = {} @values.each { |item| values[item.expression] = item.replacement } args.each do |arg| arg.values.each { |item| values[item.expression] = item.replacement } end self.class.new(values) end |
#value_of(type = nil) ⇒ Object
73 74 75 76 77 |
# File 'lib/packr/regexp_group.rb', line 73 def value_of(type = nil) return self if type == Object flag = @ignore_case ? Regexp::IGNORECASE : nil Regexp.new(self.to_s, flag) end |