README.md in dm-regex-0.0.1 vs README.md in dm-regex-0.0.2

- old
+ new

@@ -41,5 +41,50 @@ p ApacheLogEntry.match( '87.18.183.252 - - [13/Aug/2008:00:50:49 -0700] "GET /blog/index.xml HTTP/1.1" 302 527 "-" "Feedreader 3.13 (Powered by Newsbrain)"' ) # => #<ApacheLogEntry @id=nil @h="87.18.183.252" @l="-" @u="-" @t=#<DateTime: 2008-08-13T00:50:49-07:00 ((2454692j,28249s,0n),-25200s,2299161j)> @r="GET /blog/index.xml HTTP/1.1" @s=302 @b=527 @referer="-" @user_agent="Feedreader 3.13 (Powered by Newsbrain)"> ``` + +or using Regex compile options ... + +``` ruby +ApacheLogEntry.compile(%{ + ^ + \\g<h> # host + [ ] + \\g<l> # l + [ ] + \\g<u> # user + [ ]\\[ + \\g<t> # timestamp + \\][ ]" + \\g<r> # request + "[ ] + \\g<s> # status + [ ] + \\g<b> # bytes + [ ]" + \\g<referer> # referer + "[ ]" + \\g<user_agent> # user agent + "$ + }, + Regexp::EXTENDED +) +ApacheLogEntry.match( + '87.18.183.252 - - [13/Aug/2008:00:50:49 -0700] "GET /blog/index.xml HTTP/1.1" 302 527 "-" "Feedreader 3.13 (Powered by Newsbrain)"' +) do |o| + p o +end +# => #<ApacheLogEntry @id=nil @h="87.18.183.252" @l="-" @u="-" @t=#<DateTime: 2008-08-13T00:50:49-07:00 ((2454692j,28249s,0n),-25200s,2299161j)> @r="GET /blog/index.xml HTTP/1.1" @s=302 @b=527 @referer="-" @user_agent="Feedreader 3.13 (Powered by Newsbrain)"> +``` + +`.compile` +--- + * works the same as the built-in `Regexp.compile` + * uses named groups specified using `\g<name>` syntax to map groups to properties. (NB. make sure to use `\\g` when using double quoted strings) + +`property opts` +--- + + * `:pat` option specifies the pattern used to match the property. (NB. the default of `/.+?/` might be good enough in most cases) + * `:method` option takes a proc that is used to transform the matched value. (NB. DataMapper's built-in typescasting might be good enough in most cases)