Class: Packr::RegexpGroup::Item
- Inherits:
-
Object
- Object
- Packr::RegexpGroup::Item
- Defined in:
- lib/packr/regexp_group.rb
Instance Attribute Summary collapse
-
#expression ⇒ Object
Returns the value of attribute expression.
-
#length ⇒ Object
Returns the value of attribute length.
-
#replacement ⇒ Object
Returns the value of attribute replacement.
Instance Method Summary collapse
-
#initialize(expression, replacement) ⇒ Item
constructor
A new instance of Item.
- #to_s ⇒ Object
Constructor Details
#initialize(expression, replacement) ⇒ Item
Returns a new instance of Item.
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 |
# File 'lib/packr/regexp_group.rb', line 82 def initialize(expression, replacement) @expression = expression.is_a?(Regexp) ? expression.source : expression.to_s if replacement.is_a?(Numeric) replacement = "\\" + replacement.to_s elsif replacement.nil? replacement = "" end # does the pattern use sub-expressions? if replacement.is_a?(String) and replacement =~ /\\(\d+)/ # a simple lookup? (e.g. "\2") if replacement.gsub(/\n/, " ") =~ /^\\\d+$/ # store the index (used for fast retrieval of matched strings) replacement = replacement[1..-1].to_i else # a complicated lookup (e.g. "Hello \2 \1") # build a function to do the lookup q = (replacement.gsub(/\\./, "") =~ /'/) ? '"' : "'" replacement = replacement.gsub(/\r/, "\\r").gsub(/\\(\d+)/, q + "+(args[\\1]||" + q+q + ")+" + q) replacement_string = q + replacement.gsub(/(['"])\1\+(.*)\+\1\1$/, '\1') + q replacement = lambda { |*args| eval(replacement_string) } end end @length = RegexpGroup.count(@expression) @replacement = replacement end |
Instance Attribute Details
#expression ⇒ Object
Returns the value of attribute expression.
80 81 82 |
# File 'lib/packr/regexp_group.rb', line 80 def expression @expression end |
#length ⇒ Object
Returns the value of attribute length.
80 81 82 |
# File 'lib/packr/regexp_group.rb', line 80 def length @length end |
#replacement ⇒ Object
Returns the value of attribute replacement.
80 81 82 |
# File 'lib/packr/regexp_group.rb', line 80 def replacement @replacement end |
Instance Method Details
#to_s ⇒ Object
111 112 113 |
# File 'lib/packr/regexp_group.rb', line 111 def to_s @expression end |