Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding a rakefile and ability to build a roflscale gem

  • Loading branch information...
commit 4dca3af3aa055d3aed97706d7cedcf2a6e5ce98e 1 parent 161885b
@tenderlove tenderlove authored
View
1  lib/journey.rb
@@ -4,3 +4,4 @@
require 'journey/nfa/builder'
require 'journey/nfa/simulator'
require 'journey/ragel'
+require 'journey/ragel/gem'
View
2  lib/journey/ragel.rb
@@ -50,7 +50,7 @@ def initialize name, asts
return matches;
}
-void Init_router()
+void Init_roflscale()
{
VALUE above = rb_path2class("<%= name %>");
VALUE cParser = rb_define_class_under(above, "Parser", rb_cObject);
View
107 lib/journey/ragel/gem.rb
@@ -0,0 +1,107 @@
+require 'fileutils'
+require 'date'
+
+module Journey
+ class Ragel
+ class Gem
+ attr_reader :gemname, :rl_source, :klass
+
+ def initialize gemname, klass, rl_source
+ @klass = klass
+ @gemname = gemname
+ @rl_source = rl_source
+ end
+
+ def write_all
+ rakefile
+ ragel
+ extconf
+ gemspec
+ monkeypatch
+ end
+
+ def monkeypatch
+ FileUtils.mkdir "lib"
+ File.open("lib/#{gemname}.rb", 'w') do |f|
+ f.write <<-eoruby
+require '#{gemname}.so'
+module Journey
+ class Router
+ private
+ def filter_routes path
+ parser = #{klass}::Parser.new
+ offsets = parser.parse path
+
+ return [] unless offsets
+ offsets.map { |i| routes[i] }
+ end
+ end
+end
+ eoruby
+ end
+ end
+
+ def gemspec
+ File.open("#{gemname}.gemspec", 'w') do |f|
+ f.write <<-eospec
+# -*- encoding: utf-8 -*-
+Gem::Specification.new do |s|
+ s.name = "#{gemname}"
+ s.version = "#{Time.now.to_i}"
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["#{`whoami`}"]
+ s.date = "#{Date.today.to_s}"
+ s.description = "Roflscale your router"
+ s.email = ["aaron@tenderlovemaking.com"]
+ s.extensions = ["ext/#{gemname}/extconf.rb"]
+ s.files = [
+ "Rakefile",
+ "ext/#{gemname}/#{gemname}.c",
+ "ext/#{gemname}/#{gemname}.rl",
+ "ext/#{gemname}/extconf.rb",
+ "lib/#{gemname}.rb",
+ "#{gemname}.gemspec",
+ ]
+
+ s.homepage = "http://github.com/rails/roflscale"
+ s.rdoc_options = ["--main", "Rakefile"]
+ s.require_paths = ["lib", "ext"]
+ s.rubyforge_project = "roflscale"
+ s.rubygems_version = "1.8.11"
+ s.summary = "omg!"
+ s.test_files = []
+end
+ eospec
+ end
+ end
+
+ def rakefile
+ File.open('Rakefile', 'w') do |f|
+ f.puts <<-eorake
+file 'ext/#{gemname}/#{gemname}.c' => 'ext/#{gemname}/#{gemname}.rl' do |t|
+ sh "ragel -o \#{t.name} \#{t.prerequisites.first}"
+end
+
+task :package => 'ext/#{gemname}/#{gemname}.c' do
+ sh 'gem build #{gemname}.gemspec'
+end
+ eorake
+ end
+ end
+
+ def ragel
+ FileUtils.mkdir_p "ext/#{gemname}"
+ File.open("ext/#{gemname}/#{gemname}.rl", 'w') do |f|
+ f.write rl_source
+ end
+ end
+
+ def extconf
+ FileUtils.mkdir_p "ext/#{gemname}"
+ File.open("ext/#{gemname}/extconf.rb", 'w') do |f|
+ f.write "require 'mkmf'\ncreate_makefile '#{gemname}'"
+ end
+ end
+ end
+ end
+end
View
4 lib/journey/routes.rb
@@ -15,6 +15,10 @@ def initialize
@simulator = nil
end
+ def [] i
+ @routes[i]
+ end
+
def length
@routes.length
end
View
17 lib/tasks/roflscale.rake
@@ -0,0 +1,17 @@
+task :roflscale => :environment do
+ app = Rails.application
+ app.reload_routes!
+ asts = app.routes.set.map { |r| r.ast }
+
+ ragel = Journey::Ragel.new app.class.name, asts
+ source = ragel.source
+
+ gem = Journey::Ragel::Gem.new 'roflscale', app.class.name, source
+ FileUtils.rm_rf 'tmp/roflscale'
+ FileUtils.mkdir 'tmp/roflscale'
+ Dir.chdir 'tmp/roflscale' do
+ gem.write_all
+ sh 'rake package'
+ sh 'mv *.gem ../../'
+ end
+end
View
33 test/router/test_ragel.rb
@@ -1,8 +1,41 @@
require 'helper'
require 'erb'
+require 'tempfile'
+require 'fileutils'
module Journey
class TestRagel < MiniTest::Unit::TestCase
+ def test_gem
+ parser = Journey::Parser.new
+
+ asts = %w{
+ /users(.:format)
+ /users/new(.:format)
+ /users/:id/edit(.:format)
+ /users/:id(.:format)
+ }.map { |pattern| parser.parse pattern }
+
+
+ ragel = Journey::Ragel.new "RagelFun", asts
+ source = ragel.source
+
+ gemdir = File.join Dir.tmpdir, 'roflscale'
+
+ FileUtils.rm_rf gemdir
+ FileUtils.mkdir_p gemdir
+
+ Dir.chdir gemdir do
+ gem = Journey::Ragel::Gem.new 'roflscale', 'RagelFun', source
+ gem.write_all
+
+ assert_equal source, File.read('ext/roflscale/roflscale.rl')
+ assert File.exists? 'Rakefile'
+ assert File.exists? 'ext/roflscale/extconf.rb'
+ assert File.exists? 'lib/roflscale.rb'
+ assert File.exists? 'roflscale.gemspec'
+ end
+ end
+
def test_rl_source
parser = Journey::Parser.new
Please sign in to comment.
Something went wrong with that request. Please try again.