-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chris
committed
Jan 28, 2013
0 parents
commit cd1e6d8
Showing
9 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
coverage | ||
pkg | ||
Gemfile.lock | ||
.rvmrc | ||
gems | ||
bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
source :rubygems | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'bundler' | ||
Bundler.setup | ||
Bundler::GemHelper.install_tasks | ||
|
||
require "rspec/core/rake_task" | ||
|
||
task "default" => "spec" | ||
|
||
RSpec::Core::RakeTask.new do |t| | ||
t.pattern = 'spec/**/*_spec.rb' | ||
t.rspec_opts = ["--colour", "--format", "nested"] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- encoding: utf-8 -*- | ||
require File.expand_path('../lib/dm-regex/version', __FILE__) | ||
|
||
Gem::Specification.new do |gem| | ||
gem.authors = [ 'locochris' ] | ||
gem.email = [ '[email protected]' ] | ||
gem.summary = 'DataMapper plugin enabling models to be created from matching a regex' | ||
gem.description = gem.summary | ||
gem.homepage = "http://github.com/locomote/dm-regex" | ||
|
||
gem.files = `git ls-files`.split("\n") | ||
gem.test_files = `git ls-files -- {spec}/*`.split("\n") | ||
|
||
gem.name = 'dm-regex' | ||
gem.require_paths = [ "lib" ] | ||
gem.version = DataMapper::Regex::VERSION | ||
|
||
gem.add_runtime_dependency('dm-core', '~> 1.0') | ||
gem.add_runtime_dependency('dm-migrations', '~> 1.0') | ||
gem.add_runtime_dependency('dm-sqlite-adapter', '~> 1.0') | ||
|
||
gem.add_development_dependency('rake', '~> 10.0') | ||
gem.add_development_dependency('rspec', '~> 2.0') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'dm-regex/regex' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'dm-core' | ||
|
||
module DataMapper | ||
module Regex | ||
def self.included(model) | ||
model.extend ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
attr_reader :pat | ||
|
||
def compile(pattern, options=0) | ||
@group_names = pattern.scan(/\\g<(\w+)>/).flatten.map(&:to_sym) | ||
@pat = ::Regexp.compile("#{groups}#{pattern}", options) | ||
end | ||
|
||
def match(buf, parent=self) | ||
pat.match(buf) { |m| parent.first_or_new(attrs_from_match(m)) } | ||
end | ||
|
||
def property(name, type, opts={}, &block) | ||
if group_pat = opts.delete(:pat) | ||
map_pat[name] = group_pat | ||
end | ||
if group_method = opts.delete(:method) | ||
map_method[name] = group_method | ||
end | ||
super | ||
end | ||
|
||
private | ||
|
||
def map_pat | ||
@map_pat ||= {} | ||
end | ||
|
||
def map_method | ||
@map_method ||= {} | ||
end | ||
|
||
def map_value(name, value) | ||
value | ||
end | ||
|
||
def groups | ||
@groups ||= @group_names.inject("") { |str, name| | ||
str << "(?<#{name}>#{map_pat.fetch(name, '.+?')}){0}" | ||
} | ||
end | ||
|
||
def attrs_from_match(m) | ||
properties.map(&:name).inject({}) { |h, k| | ||
if @group_names.include?(k) | ||
value = m[k] | ||
if method = map_method[k] | ||
value = method.call(value) | ||
end | ||
h.update(k => value) | ||
else | ||
h | ||
end | ||
} | ||
end | ||
end | ||
end | ||
|
||
Model.append_inclusions(Regex) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module DataMapper | ||
module Regex | ||
VERSION = '0.0.1' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'spec_helper' | ||
|
||
class ApacheLogEntry | ||
include DataMapper::Resource | ||
|
||
property :id , Serial | ||
property :h , String , :pat => /[.0-9]+?/ | ||
property :l , String | ||
property :u , String | ||
property :t , DateTime , :method => lambda { |value| DateTime.strptime(value, '%d/%b/%Y:%H:%M:%S %z') } | ||
property :r , String | ||
property :s , Integer | ||
property :b , Integer | ||
property :referer , String | ||
property :user_agent , String | ||
|
||
# 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)" | ||
compile '^\g<h> \g<l> \g<u> \[\g<t>\] "\g<r>" \g<s> \g<b> "\g<referer>" "\g<user_agent>"$' | ||
end | ||
|
||
|
||
DataMapper.setup :default, "sqlite::memory:" | ||
DataMapper.auto_upgrade! | ||
|
||
describe DataMapper::Regex do | ||
describe '.match' do | ||
subject { ApacheLogEntry.match(line) } | ||
|
||
before :all do | ||
|
||
end | ||
|
||
let(:line) { | ||
'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)"' | ||
} | ||
|
||
its(:h) { should == '87.18.183.252' } | ||
its(:l) { should == '-' } | ||
its(:u) { should == '-' } | ||
its(:t) { should == DateTime.parse('13/08/2008T00:50:49 -0700') } | ||
its(:r) { should == 'GET /blog/index.xml HTTP/1.1' } | ||
its(:s) { should == 302 } | ||
its(:b) { should == 527 } | ||
its(:referer) { should == '-' } | ||
its(:user_agent) { should == 'Feedreader 3.13 (Powered by Newsbrain)' } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'dm-regex' | ||
require 'dm-migrations' | ||
|