Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chris committed Jan 28, 2013
0 parents commit cd1e6d8
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage
pkg
Gemfile.lock
.rvmrc
gems
bin
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source :rubygems
gemspec
12 changes: 12 additions & 0 deletions Rakefile
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
24 changes: 24 additions & 0 deletions dm-regex.gemspec
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
1 change: 1 addition & 0 deletions lib/dm-regex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'dm-regex/regex'
68 changes: 68 additions & 0 deletions lib/dm-regex/regex.rb
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
5 changes: 5 additions & 0 deletions lib/dm-regex/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module DataMapper
module Regex
VERSION = '0.0.1'
end
end
47 changes: 47 additions & 0 deletions spec/dm-regex_spec.rb
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
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'dm-regex'
require 'dm-migrations'

0 comments on commit cd1e6d8

Please sign in to comment.