www 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .bundle
23
+ *.gemspec
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ source 'http://rubygems.org'
3
+
4
+ gem 'tilt'
5
+ gem 'haml'
6
+ gem 'guard-livereload'
7
+ gem 'rspec'
8
+ gem 'rack-test'
9
+ gem 'rr'
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.2)
5
+ diff-lcs (1.1.2)
6
+ em-websocket (0.1.4)
7
+ addressable (>= 2.1.1)
8
+ eventmachine (>= 0.12.9)
9
+ eventmachine (0.12.10)
10
+ ffi (0.6.3)
11
+ rake (>= 0.8.7)
12
+ growl (1.0.3)
13
+ guard (0.1.1)
14
+ bundler (~> 1.0.2)
15
+ growl (~> 1.0.3)
16
+ libnotify (~> 0.1.3)
17
+ rb-inotify (~> 0.8.1)
18
+ sys-uname (~> 0.8.4)
19
+ thor (~> 0.14.3)
20
+ guard-livereload (0.1.1)
21
+ em-websocket (~> 0.1.3)
22
+ guard (~> 0.1.1)
23
+ json (~> 1.4.6)
24
+ haml (3.0.18)
25
+ json (1.4.6)
26
+ libnotify (0.1.4)
27
+ ffi (>= 0.6.2)
28
+ rack (1.2.1)
29
+ rack-test (0.5.6)
30
+ rack (>= 1.0)
31
+ rake (0.8.7)
32
+ rb-inotify (0.8.1)
33
+ ffi (>= 0.5.0)
34
+ rr (1.0.0)
35
+ rspec (2.0.0)
36
+ rspec-core (= 2.0.0)
37
+ rspec-expectations (= 2.0.0)
38
+ rspec-mocks (= 2.0.0)
39
+ rspec-core (2.0.0)
40
+ rspec-expectations (2.0.0)
41
+ diff-lcs (>= 1.1.2)
42
+ rspec-mocks (2.0.0)
43
+ rspec-core (= 2.0.0)
44
+ rspec-expectations (= 2.0.0)
45
+ sys-uname (0.8.4)
46
+ thor (0.14.3)
47
+ tilt (1.1)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ guard-livereload
54
+ haml
55
+ rack-test
56
+ rr
57
+ rspec
58
+ tilt
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ # A sample Guardfile
2
+ # More info at http://github.com/guard/guard#readme
3
+
4
+ guard 'livereload' do
5
+ watch('.+\.rb')
6
+ end
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- WWW
1
+ Www
2
2
  ====
3
3
 
4
4
  World Wide Web
@@ -11,7 +11,7 @@ app.rb
11
11
  # encoding: utf-8
12
12
  require 'www'
13
13
 
14
- class Foo < Www
14
+ class Foo < Www::Base
15
15
  before do
16
16
  # do something
17
17
  end
@@ -23,24 +23,36 @@ app.rb
23
23
 
24
24
  get '/foo'
25
25
  def foo(params)
26
- params
26
+ haml :title => 'foo'
27
27
  end
28
28
 
29
- get %r{/regexp/?(.*)} # ex: /regexp/foo
29
+ get '/regexp/?(.*)' # ex: /regexp/foo
30
30
  def regexp(arg, params)
31
31
  "#{arg} - #{params.inspect}"
32
32
  end
33
33
 
34
- get %r{/(\d{4})/(\d{2})/(\d{2})} # ex: /2009/10/10
34
+ get '/(\d{4})/(\d{2})/(\d{2})' # ex: /2009/10/10
35
35
  def entry(year, month, date)
36
36
  [year, month, date]
37
37
  end
38
+
39
+ def helper
40
+ 'bar'
41
+ end
38
42
  end
39
43
 
44
+ views/foo.haml
45
+
46
+ !!!
47
+ %html
48
+ %body
49
+ %h1= title
50
+ %p= helper
51
+
40
52
  config.ru
41
53
 
42
54
  require 'app'
43
- run Www
55
+ run Www::App
44
56
 
45
57
  rackup
46
58
 
@@ -49,6 +61,7 @@ rackup
49
61
  TODO
50
62
  ----
51
63
 
64
+ * specs
52
65
  * handle static files
53
66
  * view
54
67
  * redirect
@@ -57,10 +70,11 @@ TODO
57
70
  * url helper
58
71
  * namespace
59
72
 
60
- Run Examples
73
+ Run Example
61
74
  ----
62
75
 
63
- shotgun -Ilib example/config.ru
76
+ % cd example
77
+ % shotgun -I../lib config.ru
64
78
 
65
79
  Note on Patches/Pull Requests
66
80
  ----
data/Rakefile CHANGED
@@ -10,7 +10,9 @@ begin
10
10
  gem.email = "jugyo.org@gmail.com"
11
11
  gem.homepage = "http://github.com/jugyo/www"
12
12
  gem.authors = ["jugyo"]
13
- gem.add_development_dependency "rspec", ">= 1.2.9"
13
+ gem.add_dependency "rack", ">= 1.2.1"
14
+ gem.add_dependency "tilt", ">= 1.1"
15
+ gem.add_development_dependency "rspec", ">= 2.0.0"
14
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
17
  end
16
18
  Jeweler::GemcutterTasks.new
@@ -18,16 +20,10 @@ rescue LoadError
18
20
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
21
  end
20
22
 
21
- require 'spec/rake/spectask'
22
- Spec::Rake::SpecTask.new(:spec) do |spec|
23
- spec.libs << 'lib' << 'spec'
24
- spec.spec_files = FileList['spec/**/*_spec.rb']
25
- end
26
-
27
- Spec::Rake::SpecTask.new(:rcov) do |spec|
28
- spec.libs << 'lib' << 'spec'
29
- spec.pattern = 'spec/**/*_spec.rb'
30
- spec.rcov = true
23
+ require 'rspec/core/rake_task'
24
+ RSpec::Core::RakeTask.new(:spec) do |t|
25
+ t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
26
+ t.pattern = 'spec/**/*_spec.rb'
31
27
  end
32
28
 
33
29
  task :spec => :check_dependencies
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/example/app.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require 'www'
3
3
 
4
- class Foo < Www
4
+ class Foo < Www::Base
5
5
  before do
6
6
  # do something
7
7
  end
@@ -13,16 +13,20 @@ class Foo < Www
13
13
 
14
14
  get '/foo'
15
15
  def foo(params)
16
- params
16
+ haml :title => 'foo'
17
17
  end
18
18
 
19
- get %r{/regexp/?(.*)} # ex: /regexp/foo
19
+ get '/regexp/?(.*)' # ex: /regexp/foo
20
20
  def regexp(arg, params)
21
21
  "#{arg} - #{params.inspect}"
22
22
  end
23
23
 
24
- get %r{/(\d{4})/(\d{2})/(\d{2})} # ex: /2009/10/10
24
+ get '/(\d{4})/(\d{2})/(\d{2})' # ex: /2009/10/10
25
25
  def entry(year, month, date)
26
26
  [year, month, date]
27
27
  end
28
+
29
+ def helper
30
+ 'bar'
31
+ end
28
32
  end
data/example/config.ru CHANGED
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  $:.unshift File.dirname(__FILE__)
3
3
  require 'app'
4
- run Www
4
+ run Www::App
data/example/foo.haml ADDED
@@ -0,0 +1,5 @@
1
+ !!!
2
+ %html
3
+ %body
4
+ %h1= title
5
+ %p= helper
data/lib/www/app.rb ADDED
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+ module Www
3
+ class App
4
+ def self.call(env)
5
+ Base.call(env)
6
+ end
7
+ end
8
+ end
data/lib/www/base.rb ADDED
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+ module Www
3
+ class Base
4
+ include View
5
+
6
+ class << self
7
+ @@current_route = nil
8
+ @@routes = []
9
+
10
+ def routes
11
+ @@routes
12
+ end
13
+
14
+ def route(pattern, methods = [:get])
15
+ @@current_route = Route.new(pattern, methods, self)
16
+ end
17
+ alias_method :_, :route
18
+
19
+ def get(pattern) _(pattern, :get) end
20
+ def post(pattern) _(pattern, :post) end
21
+ def put(pattern) _(pattern, :put) end
22
+ def delete(pattern) _(pattern, :delete) end
23
+
24
+ def before(*route_names, &block)
25
+ before_blocks << [route_names.map{ |i| i.to_sym }, block]
26
+ end
27
+
28
+ def before_blocks
29
+ @before_blocks ||= []
30
+ end
31
+
32
+ def method_added(name)
33
+ return unless @@current_route
34
+ @@current_route.name = name
35
+ @@routes << @@current_route
36
+ @@current_route = nil
37
+
38
+ class_eval do
39
+ alias_method :"www_#{name}", name
40
+ define_method name do |*args|
41
+ self.class.before_blocks.each do |route_names, block|
42
+ if route_names.empty? || route_names.include?(name)
43
+ instance_eval(&block)
44
+ end
45
+ end
46
+
47
+ @route_name = name
48
+ body = send(:"www_#{name}", *args) || ''
49
+ body = body.inspect unless body.is_a?(String)
50
+ [@response.status, @response.header, [body].flatten]
51
+ end
52
+ end
53
+ end
54
+
55
+ def error(code)
56
+ [code, {"Content-Type" => "text/html"}, []]
57
+ end
58
+
59
+ def find_route(path, request_method)
60
+ [@@routes.detect { |route|
61
+ path.match(route.pattern) && route.request_methods.include?(request_method.downcase.to_sym) }, $~]
62
+ end
63
+
64
+ def call(env)
65
+ request = Rack::Request.new(env)
66
+ route, match = find_route(request.path_info, request.request_method)
67
+ if route
68
+ handler = route.clazz.new(request)
69
+ args = match[1..-1] << request.params
70
+ args = adjust_args_for(args, handler.method(:"www_#{route.name}").arity)
71
+ # puts "#{route.clazz}##{route.name}(#{args.map{|i| "'#{i}'"}.join(', ')})"
72
+ handler.send(route.name, *args)
73
+ else
74
+ error 404
75
+ end
76
+ end
77
+
78
+ def adjust_args_for(args, arity)
79
+ args = args[0, arity] if arity >= 0
80
+ end
81
+ end
82
+
83
+ def initialize(request = nil)
84
+ # TODO: @_ で始まるようにした方がいいかもしれない
85
+ @request = request
86
+ @response = Rack::Response.new
87
+ end
88
+ end
89
+ end
data/lib/www/route.rb CHANGED
@@ -1,24 +1,16 @@
1
1
  # encoding: utf-8
2
- class Www
2
+ module Www
3
3
  class Route
4
4
  attr_accessor :pattern, :request_methods, :name, :clazz
5
5
  def initialize(pattern, request_methods, clazz)
6
- case pattern
7
- when Regexp then @pattern = /^#{pattern}$/
8
- when String then @pattern = /^#{Regexp.quote(pattern)}$/
9
- else
10
- raise 'pattern must be Regexp or String'
11
- end
12
- @request_methods = [request_methods].flatten.map { |m| m.to_s.upcase }
6
+ @pattern = "^#{pattern}$"
7
+ @request_methods = [request_methods].flatten.map { |m| m.to_s.downcase.to_sym }
13
8
  @clazz = clazz
14
9
  end
15
10
 
16
- def to_s
17
- inspect
18
- end
19
-
20
11
  def inspect
21
12
  "#<#{self.class}: #{pattern}, [#{request_methods.join(',')}] => #{clazz}##{name} >"
22
13
  end
14
+ alias_method :to_s, :inspect
23
15
  end
24
16
  end
data/lib/www/view.rb ADDED
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require 'tilt'
3
+
4
+ module Www
5
+ module View
6
+ def self.included(base)
7
+ base.send(:extend, ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def view_dir(dir)
12
+ @view_dir = dir
13
+ end
14
+
15
+ def view_dir_path
16
+ @view_dir || '.'
17
+ end
18
+ end
19
+
20
+ Tilt.mappings.keys.map { |key| key.to_sym }.each do |name|
21
+ define_method(name) do |*args|
22
+ render name, *args
23
+ end
24
+ end
25
+
26
+ def render(type, *args)
27
+ name = args[0].is_a?(String) ? args.shift : @route_name
28
+ values = args[0]
29
+ path = File.join(self.class.view_dir_path, "#{name}.#{type}")
30
+ template = Tilt.new(path)
31
+ template.render self, values # TODO: helper class instead of Object.new
32
+ end
33
+ end
34
+ end
data/lib/www.rb CHANGED
@@ -1,79 +1,7 @@
1
1
  # encoding: utf-8
2
- require 'www/route'
3
-
4
- class Www
5
- @@current_route = nil
6
- @@routes = []
7
-
8
- class << self
9
- def routes
10
- @@routes
11
- end
12
-
13
- def route(pattern, methods = [:get])
14
- @@current_route = Route.new(pattern, methods, self)
15
- end
16
- alias_method :_, :route
17
-
18
- def get(pattern) _(pattern, :get) end
19
- def post(pattern) _(pattern, :post) end
20
- def put(pattern) _(pattern, :put) end
21
- def delete(pattern) _(pattern, :delete) end
22
-
23
- def before(&block)
24
- @before_block = block
25
- end
26
-
27
- def before_block
28
- @before_block
29
- end
30
-
31
- def method_added(name)
32
- return unless @@current_route
33
- @@current_route.name = name
34
- @@routes << @@current_route
35
- @@current_route = nil
36
-
37
- class_eval do
38
- alias_method :"www_#{name}", name
39
- define_method name do |*args|
40
- instance_eval(&self.class.before_block)
41
- body = send(:"www_#{name}", *args) || ''
42
- body = body.inspect unless body.is_a?(String)
43
- [@response.status, @response.header, [body].flatten]
44
- end
45
- end
46
- end
47
-
48
- def error(code)
49
- [code, {"Content-Type" => "text/html"}, []]
50
- end
51
-
52
- def find_route(path, request_method)
53
- [@@routes.detect { |route|
54
- route.pattern =~ path && route.request_methods.include?(request_method) }, $~]
55
- end
56
-
57
- def call(env)
58
- request = Rack::Request.new(env)
59
- route, match = find_route(request.path_info, request.request_method)
60
- if route
61
- handler = route.clazz.new(request)
62
- args = match[1..-1]
63
- args << request.params
64
- # adjust args
65
- arity = handler.method(:"www_#{route.name}").arity
66
- args = arity == 0 ? [] : args[0..(arity-1)]
67
- puts "#{route.clazz}##{route.name}(#{args.map{|i| "'#{i}'"}.join(', ')})" # TODO: use logger
68
- handler.send(route.name, *args)
69
- else
70
- error 404
71
- end
72
- end
73
- end
74
-
75
- def initialize(request = nil)
76
- @request = request
77
- @response = Rack::Response.new
78
- end
2
+ module Www
3
+ autoload :Base, 'www/base'
4
+ autoload :Route, 'www/route'
5
+ autoload :App, 'www/app'
6
+ autoload :View, 'www/view'
79
7
  end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'www'
4
- require 'spec'
5
- require 'spec/autorun'
4
+ require 'rspec'
5
+ require 'rspec/autorun'
6
+ require 'rack/test'
6
7
 
7
- Spec::Runner.configure do |config|
8
-
8
+ include Rack::Test::Methods
9
+
10
+ RSpec.configure do |config|
11
+ config.mock_with :rr
9
12
  end
@@ -0,0 +1 @@
1
+ = params.inspect
data/spec/www_spec.rb CHANGED
@@ -1,7 +1,63 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ class Foo < Www::Base
2
+ view_dir 'spec/views'
3
+
4
+ before {}
5
+ before(:foo, :index) {}
6
+
7
+ get '/'
8
+ def index
9
+ "index"
10
+ end
11
+
12
+ get '/foo'
13
+ def foo(params)
14
+ haml :params => params
15
+ end
16
+
17
+ get '/regexp/?(.*)'
18
+ def regexp(arg, params)
19
+ "#{arg} - #{params.inspect}"
20
+ end
21
+
22
+ get '/(\d{4})/(\d{2})/(\d{2})'
23
+ def entry(year, month, date)
24
+ [year, month, date]
25
+ end
26
+ end
27
+
28
+ def app
29
+ Www::App
30
+ end
2
31
 
3
32
  describe "Www" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
33
+ it 'should call before block' do
34
+ foo = Foo.new
35
+ 2.times do
36
+ mock.proxy(foo).instance_eval(&Foo.before_blocks[0][1])
37
+ mock.proxy(foo).instance_eval(&Foo.before_blocks[1][1])
38
+ foo.index
39
+ end
40
+ end
41
+
42
+ it 'should call index as "/"' do
43
+ get('/').body.should == "index"
44
+ end
45
+
46
+ it 'should call foo as "/foo" with params' do
47
+ params = {'foo' => 'bar'}
48
+ get('/foo', params).body.chomp.should == params.inspect
49
+ end
50
+
51
+ it 'should call regexp as "/regexp/..."' do
52
+ get('/regexp/foo').body.should == "foo - #{{}.inspect}"
53
+ end
54
+
55
+ it 'should call regexp as "/regexp/..." with params' do
56
+ params = {'foo' => 'bar'}
57
+ get('/regexp/foo', params).body.should == "foo - #{params.inspect}"
58
+ end
59
+
60
+ it 'should call entry as "/2010/10/15"' do
61
+ get('/2010/10/15').body.should == ['2010', '10', '15'].inspect
6
62
  end
7
63
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - jugyo
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-14 00:00:00 +09:00
17
+ date: 2010-10-16 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: rack
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
@@ -28,10 +28,39 @@ dependencies:
28
28
  segments:
29
29
  - 1
30
30
  - 2
31
- - 9
32
- version: 1.2.9
33
- type: :development
31
+ - 1
32
+ version: 1.2.1
33
+ type: :runtime
34
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: tilt
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 1
46
+ version: "1.1"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 2
59
+ - 0
60
+ - 0
61
+ version: 2.0.0
62
+ type: :development
63
+ version_requirements: *id003
35
64
  description: A World Wide Web Application Framework
36
65
  email: jugyo.org@gmail.com
37
66
  executables: []
@@ -43,16 +72,24 @@ extra_rdoc_files:
43
72
  - README.md
44
73
  files:
45
74
  - .document
75
+ - .gitignore
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - Guardfile
46
79
  - LICENSE
47
80
  - README.md
48
81
  - Rakefile
49
82
  - VERSION
50
83
  - example/app.rb
51
84
  - example/config.ru
85
+ - example/foo.haml
52
86
  - lib/www.rb
87
+ - lib/www/app.rb
88
+ - lib/www/base.rb
53
89
  - lib/www/route.rb
54
- - spec/spec.opts
90
+ - lib/www/view.rb
55
91
  - spec/spec_helper.rb
92
+ - spec/views/foo.haml
56
93
  - spec/www_spec.rb
57
94
  has_rdoc: true
58
95
  homepage: http://github.com/jugyo/www
data/spec/spec.opts DELETED
@@ -1 +0,0 @@
1
- --color