hobby 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42e7f7a0e7818909ac5d95c50484655aab19fac1
4
- data.tar.gz: 7b0f857a9f4ceb6dfdb0eff86d6d546d2b285a6a
3
+ metadata.gz: d7bc8b664dad60ba755a291e2b6ecac6e330b227
4
+ data.tar.gz: 2dce16045fe1301e04a45b6057787b2b036b9332
5
5
  SHA512:
6
- metadata.gz: d2f3740ca3b95e942af4bf6dc4fb5f8d5acfdd21bd6c48f0544b8f262de0f004856a31547f3739ad440a11195c80a0348e4c4237afb026d4fa566dc6cb138ad2
7
- data.tar.gz: fe87a3f5ca1be2c5ee1a346cdeb9e26800c642e4de8251217f8461ba91c8dbb63c7b52ea1a208edb35f6851d27b47e385df4bfb153849065e826fc1b2ace1fed
6
+ metadata.gz: fd1088888933b0786921737b63f546110078a91372bfc59db7b266efdfcf8e796c59f3e4cc28c5678030541583d9f59ea7d61b633b2e1f8e5c2f169fc16e85f3
7
+ data.tar.gz: 2874923dffc328ff549ba1d7e2104ba170722a2a5a0f93ffb6276607153489a1b9ca7bd67f263a3b9c4274e80eab9cd302ec558ad762c7b3e3255a40c563ffba
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'devtools', github: 'mbj/devtools'
3
+ gem 'devtools'
4
4
  gem 'minitest'
5
5
  gem 'minitest-power_assert'
6
6
  gem 'rack-test'
data/LICENSE CHANGED
@@ -1,5 +1,5 @@
1
1
  Copyright (c) 2013 Patricio Mac Adden
2
- Copyright (c) 2015 Anatoly Cherno
2
+ Copyright (c) 2015 Anatoly Chernow
3
3
 
4
4
  MIT License
5
5
 
data/hobby.gemspec CHANGED
@@ -4,9 +4,9 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'hobby'
7
- spec.version = '0.0.1'
8
- spec.authors = ['Anatoly Cherno']
9
- spec.email = ['anatoly.cherno@gmail.com']
7
+ spec.version = '0.0.2'
8
+ spec.authors = ['Anatoly Chernow']
9
+ spec.email = ['chertoly@gmail.com']
10
10
  spec.summary = %q{A minimal DSL over rack}
11
11
  spec.homepage = 'https://github.com/ch1c0t/hobby'
12
12
  spec.license = 'MIT'
data/lib/hobby/app.rb CHANGED
@@ -17,9 +17,9 @@ module Hobby
17
17
  extend Forwardable
18
18
  delegate [:map, :use] => :builder
19
19
 
20
- Verbs.each do |verb|
21
- define_method verb.downcase do |path = '/', &route|
22
- router.add_route verb, path, &route
20
+ VERBS.each do |verb|
21
+ define_method verb.downcase do |path = '/', &action|
22
+ router.add_route verb, path, &action
23
23
  end
24
24
  end
25
25
  end
@@ -0,0 +1,12 @@
1
+ class Hobby::Router
2
+ class Route
3
+ def initialize verb, path, &action
4
+ @verb, @path, @action = verb, path, action
5
+ end
6
+
7
+ attr_reader :verb, :path
8
+ attr_accessor :action
9
+
10
+ alias to_proc action
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ class Hobby::Router
2
+ class Routes < Hash
3
+ def initialize
4
+ @patterns = {}
5
+ super { |hash, key| hash[key] = find key }
6
+ end
7
+
8
+ def []= key, route
9
+ if key.include? ?:
10
+ string = key.gsub(/(:\w+)/) { "(?<#{$1[1..-1]}>[^/?#]+)" }
11
+ @patterns[/^#{string}$/] = route
12
+ else
13
+ super and super "#{key}/", route
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def find key
20
+ _, route = @patterns.find { |pattern, _| pattern.match key }
21
+ [route, $~.names.map(&:to_sym).zip($~.captures).to_h] if route
22
+ end
23
+ end
24
+ end
data/lib/hobby/router.rb CHANGED
@@ -4,9 +4,13 @@ module Hobby
4
4
  @routes = Routes.new
5
5
  end
6
6
 
7
- def add_route verb, path, &route
7
+ def add_route verb, path, &action
8
+ route = Route.new verb, path, &action
9
+
8
10
  path = nil if path.eql? '/'
9
11
  @routes["#{verb}#{path}"] = route
12
+
13
+ route
10
14
  end
11
15
 
12
16
  def route_for env
@@ -14,28 +18,8 @@ module Hobby
14
18
  env[:path_params] = params if params
15
19
  route
16
20
  end
17
-
18
- class Routes < Hash
19
- def initialize
20
- @patterns = {}
21
- super { |hash, key| hash[key] = find key }
22
- end
23
-
24
- def []= key, route
25
- if key.include? ?:
26
- string = key.gsub(/(:\w+)/) { "(?<#{$1[1..-1]}>[^/?#]+)" }
27
- @patterns[/^#{string}$/] = route
28
- else
29
- super and super "#{key}/", route
30
- end
31
- end
32
-
33
- private
34
-
35
- def find key
36
- _, route = @patterns.find { |pattern, _| pattern.match key }
37
- [route, $~.names.map(&:to_sym).zip($~.captures).to_h] if route
38
- end
39
- end
40
21
  end
41
22
  end
23
+
24
+ require 'hobby/router/routes'
25
+ require 'hobby/router/route'
@@ -1,7 +1,7 @@
1
1
  module Hobby::RSpec
2
2
  module Router
3
3
  extend self
4
- SOME_ROUTE = ->{}
4
+ SOME_ROUTE = ->{:some_route}
5
5
 
6
6
  def env_for path, verb = 'GET'
7
7
  {'REQUEST_METHOD' => verb, 'PATH_INFO' => path }
@@ -20,7 +20,7 @@ module Hobby::RSpec
20
20
 
21
21
  params_are_ok = (@params ? (@params.to_a - env[:path_params].to_a).empty? : true)
22
22
 
23
- (route == SOME_ROUTE) && params_are_ok
23
+ route && (route.to_proc.call == SOME_ROUTE.call) && params_are_ok
24
24
  end
25
25
 
26
26
  chain :and_set_params do |**params|
data/lib/hobby.rb CHANGED
@@ -2,7 +2,7 @@ require 'rack'
2
2
  require 'forwardable'
3
3
 
4
4
  module Hobby
5
- Verbs = %w!DELETE GET HEAD OPTIONS PATCH POST PUT!
5
+ VERBS = %w[DELETE GET HEAD OPTIONS PATCH POST PUT]
6
6
  end
7
7
 
8
8
  require 'hobby/router'
data/spec/app_spec.rb CHANGED
@@ -142,5 +142,12 @@ describe Hobby::App do
142
142
  assert { last_response.body == 'a:b:c' }
143
143
  end
144
144
  end
145
+
146
+ describe Decorator do
147
+ it do
148
+ get '/route'
149
+ assert { last_response.body == 'GET:initial:/route' }
150
+ end
151
+ end
145
152
  end
146
153
  end
@@ -0,0 +1,6 @@
1
+ route = get '/route' do
2
+ 'initial'
3
+ end
4
+
5
+ action = route.action
6
+ route.action = ->{ "#{route.verb}:#{instance_exec &action}:#{route.path}" }
data/spec/apps/Main.rb CHANGED
@@ -1,4 +1,4 @@
1
- Hobby::Verbs.each do |verb|
1
+ Hobby::VERBS.each do |verb|
2
2
  class_eval "#{verb.downcase}('/') { '#{verb}' }"
3
3
  class_eval "#{verb.downcase}('/:name') { my[:name] }"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Anatoly Cherno
7
+ - Anatoly Chernow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-10 00:00:00.000000000 Z
11
+ date: 2016-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -26,7 +26,7 @@ dependencies:
26
26
  version: '0'
27
27
  description:
28
28
  email:
29
- - anatoly.cherno@gmail.com
29
+ - chertoly@gmail.com
30
30
  executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
@@ -42,8 +42,11 @@ files:
42
42
  - lib/hobby.rb
43
43
  - lib/hobby/app.rb
44
44
  - lib/hobby/router.rb
45
+ - lib/hobby/router/route.rb
46
+ - lib/hobby/router/routes.rb
45
47
  - lib/hobby/rspec/router.rb
46
48
  - spec/app_spec.rb
49
+ - spec/apps/Decorator.rb
47
50
  - spec/apps/Env.rb
48
51
  - spec/apps/Main.rb
49
52
  - spec/apps/Map.rb
@@ -74,12 +77,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
77
  version: '0'
75
78
  requirements: []
76
79
  rubyforge_project:
77
- rubygems_version: 2.5.1
80
+ rubygems_version: 2.5.2
78
81
  signing_key:
79
82
  specification_version: 4
80
83
  summary: A minimal DSL over rack
81
84
  test_files:
82
85
  - spec/app_spec.rb
86
+ - spec/apps/Decorator.rb
83
87
  - spec/apps/Env.rb
84
88
  - spec/apps/Main.rb
85
89
  - spec/apps/Map.rb