eldr 0.0.3 → 0.0.4

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: 84ecf1db13045d463d9dd4687074cd5a190652f6
4
- data.tar.gz: 8ffee557923c1872b7b6c3d14ba71e8cc7843ce3
3
+ metadata.gz: db52c192c7a12914d17592bb4915c1d51751aeb3
4
+ data.tar.gz: b8c4d24f5e8511218adc5e00b5052a98c2fde831
5
5
  SHA512:
6
- metadata.gz: f1095a2e3d6d125e1876613750ea90e47a41c114817503271af2d05b58ae436a3c915b7fe51628637bce4b362cf4b2d3c7f2cb9bf7dc9381053d51a938a27512
7
- data.tar.gz: 6b2bdb314b65de89b65b34f25b6e4b08cd73a21b57a08ef59cdc383bd075a5038740adbbfac08b73cb3fb1ce0b90a0bf87c7a5831e656d1063d8db49fa495229
6
+ metadata.gz: ee15590d689faecf60a29fc03e1536181f0c73f10ec05c4ced43490af7a0500230cf85cabb066ae00071024952e824bf2852c918e5ad4935d8b99b9978d955ca
7
+ data.tar.gz: 5abe4a5ab8be77188c966642704b531da0aae69b989d50f47ccda6e17aa5989539c646141497fab31917558fcf207efcdc2bb049d774fa758d5fa37b564d3621
@@ -1,5 +1,5 @@
1
1
  # This configuration was generated by `rubocop --auto-gen-config`
2
- # on 2015-01-24 18:09:30 -0800 using RuboCop version 0.28.0.
2
+ # on 2015-02-16 13:42:01 -0800 using RuboCop version 0.28.0.
3
3
  # The point is for the user to remove these configuration records
4
4
  # one by one as the offenses are removed from the code base.
5
5
  # Note that changes in the inspected code, or installation of new
@@ -10,6 +10,6 @@
10
10
  Metrics/ParameterLists:
11
11
  Max: 6
12
12
 
13
- # Offense count: 6
13
+ # Offense count: 7
14
14
  Style/Documentation:
15
15
  Enabled: false
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- eldr (0.0.3)
4
+ eldr (0.0.4)
5
5
  fast_blank (= 0.0.2)
6
6
  mustermann (= 0.4.0)
7
7
  rack (~> 1.5)
@@ -31,7 +31,7 @@ GEM
31
31
  slop (~> 3.4, >= 3.4.5)
32
32
  powerpack (0.0.9)
33
33
  rack (1.6.0)
34
- rack-test (0.6.2)
34
+ rack-test (0.6.3)
35
35
  rack (>= 1.0)
36
36
  rainbow (2.0.0)
37
37
  rake (10.4.2)
@@ -81,7 +81,7 @@ DEPENDENCIES
81
81
  bundler (~> 1.7)
82
82
  coveralls (~> 0.7)
83
83
  eldr!
84
- rack-test (= 0.6.2)
84
+ rack-test (~> 0.6)
85
85
  rake (= 10.4.2)
86
86
  rspec (= 3.1.0)
87
87
  rubocop (= 0.28.0)
data/README.md CHANGED
@@ -6,7 +6,7 @@ Eldr apps are rack apps like this:
6
6
 
7
7
  ```ruby
8
8
  class App < Eldr::App
9
- get '/posts' do |params|
9
+ get '/posts' do
10
10
  Rack::Response.new "posts", 200
11
11
  end
12
12
  end
@@ -22,13 +22,13 @@ And when you want to combine them you can do this:
22
22
 
23
23
  ```ruby
24
24
  class Posts < Eldr::App
25
- get '/' do |params|
25
+ get '/'
26
26
  Rack::Response.new "posts", 200
27
27
  end
28
28
  end
29
29
 
30
30
  class Tasks < Eldr::App
31
- get '/' do |params|
31
+ get '/'
32
32
  Rack::Response.new "tasks", 200
33
33
  end
34
34
  end
@@ -131,8 +131,6 @@ I have already built and released extensions for many common tasks:
131
131
  - [eldr-assets](https://github.com/eldr-rb/eldr-assets): asset tag helpers like `js('jquery', 'app')`, `css('app')` etc
132
132
  - [eldr-responders](https://github.com/eldr-rb/eldr-responders): rails-responder like helpers
133
133
  - [eldr-action](https://github.com/eldr-rb/eldr-action): Action Objects
134
- - [eldr-service](https://github.com/eldr-rb/eldr-service): Action Objects for external services.
135
- - [eldr-cascade](https://github.com/eldr-rb/eldr-cascade): A fork of Rack::Cascade that plays well with Rack::Response and eldr-action.
136
134
 
137
135
  ## Quickstart Guides
138
136
 
@@ -154,7 +152,7 @@ Now create a new file called app.rb with the following contents:
154
152
 
155
153
  ```ruby
156
154
  class App < Eldr::App
157
- get '/' do |params|
155
+ get '/' do
158
156
  Rack::Response.new "Hello World!", 200
159
157
  end
160
158
  end
@@ -177,7 +175,7 @@ When you visit http:///localhost:9292 in your browser you should see "Hello Worl
177
175
 
178
176
  ### Rendering a Template
179
177
 
180
- Eldr provides no render helper in it's core but it is easy to define your own.
178
+ Eldr provides no render helper in its core but it is easy to define your own.
181
179
  One can use Tilt as the templating library (with your engine of choice) and then create some helper methods to handle finding the template and rendering it.
182
180
 
183
181
  Create a module with the following:
@@ -497,7 +495,7 @@ access_control do
497
495
  end
498
496
  ```
499
497
 
500
- The DSL pulled a route's name and it's roles into a before filter, then checked them against the current_user's roles. The filter itself was five lines.
498
+ The DSL pulled a route's name and its roles into a before filter, then checked them against the current_user's roles. The filter itself was five lines.
501
499
 
502
500
  I soon realized that this was redundant abstraction. The DSL didn't save me any coding, it merely gave the code pretty words. I was sacrificing clarity for poetry.
503
501
 
@@ -525,7 +523,7 @@ This allows you to define a base app and allow all your controllers to share the
525
523
  For example:
526
524
 
527
525
  ```ruby
528
- class SimpleMiddleware
526
+ class SimpleCounterMiddleware
529
527
  def initialize(app)
530
528
  @app = app
531
529
  end
@@ -664,9 +662,11 @@ map '/users' do
664
662
  end
665
663
  ```
666
664
 
667
- If we wan to define all our controllers on the root we can use [eldr-cascade](https://github.com/eldr-rb/eldr-cascade). We define the routes we want to override, Cascade will get a 404 on the ones we didn't, then call the next app until it gets a response:
665
+ If we wan to define all our controllers on the root we can use Eldr::Cascade. We define the routes we want to override, Cascade will get a 404 on the ones we didn't, then call the next app until it gets a response:
668
666
 
669
667
  ```ruby
668
+ require 'eldr'
669
+ require 'eldr/cascade'
670
670
  class App
671
671
  # override the users post route but nothing else
672
672
  post '/users' do
data/TODOS CHANGED
@@ -1,3 +1,4 @@
1
1
  - [ ] Rdocs
2
2
  - [ ] performance improvements
3
3
  - [ ] wiki pages
4
+ - [ ] add Eldr::Cascade example to examples/
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency 'rake', '10.4.2'
26
26
  s.add_development_dependency 'rspec', '3.1.0'
27
27
  s.add_development_dependency 'rubocop', '0.28.0'
28
- s.add_development_dependency 'rack-test', '0.6.2'
28
+ s.add_development_dependency 'rack-test', '~> 0.6'
29
29
  s.add_development_dependency 'tilt', '2.0.1'
30
30
  s.add_development_dependency 'slim', '3.0.1'
31
31
  s.add_development_dependency 'coveralls', '~> 0.7'
@@ -18,7 +18,7 @@ class ExampleApp < Eldr::App
18
18
  @cats == 'Cats!'
19
19
  end
20
20
 
21
- get '/posts' do |params|
21
+ get '/posts' do
22
22
  Rack::Response.new "posts", 200
23
23
  end
24
24
 
@@ -89,16 +89,15 @@ module Eldr
89
89
  handler ||= args.pop if args.last.respond_to?(:call)
90
90
  options ||= args.pop if args.last.is_a?(Hash)
91
91
  options ||= {}
92
- add(verb: verb.downcase.to_sym, path: path, handler: handler, options: options)
92
+ add({ verb: verb.downcase.to_sym, path: path, handler: handler }.merge!(options))
93
93
  end
94
94
  end
95
95
 
96
- def add(verb: :get, path: '/', options: {}, handler: nil)
97
- handler = Proc.new if block_given?
98
- route = Route.new(verb: verb, path: path, options: options, handler: handler)
96
+ def add(verb: :get, **route_attributes)
97
+ route = Route.new(route_attributes)
99
98
 
100
99
  route.before_filters.push(*before_filters[route.name]) if before_filters.include? route.name
101
- route.after_filters.push(*after_filters[route.name]) if after_filters.include? route.name
100
+ route.after_filters.push(*after_filters[route.name]) if after_filters.include? route.name
102
101
 
103
102
  routes[verb] << route
104
103
  route
@@ -0,0 +1,47 @@
1
+ # A version of Rack::Cascade that works with Rack::Response.
2
+ # Seriously? WTF Rack::Cascade
3
+ # TODO: Get fixes into rack and remove this entirely
4
+ module Eldr
5
+ class Cascade
6
+ NOTFOUND = [404, { 'CONTENT-TYPE' => 'text/plain' }, []]
7
+
8
+ attr_reader :apps
9
+
10
+ def initialize(apps, catch = [404, 405])
11
+ @apps = []
12
+ @has_app = {}
13
+
14
+ apps.each { |app| add app }
15
+
16
+ @catch = {}
17
+ [*catch].each { |status| @catch[status] = true }
18
+ end
19
+
20
+ def call(env)
21
+ result = NOTFOUND
22
+
23
+ last_body = nil
24
+
25
+ @apps.each do |app|
26
+ last_body.close if last_body.respond_to? :close
27
+
28
+ result = app.call(env).to_a
29
+ last_body = result[2]
30
+ break unless @catch.include?(result[0].to_i)
31
+ end
32
+
33
+ result
34
+ end
35
+
36
+ def add(app)
37
+ @has_app[app] = true
38
+ @apps << app
39
+ end
40
+
41
+ def include?(app)
42
+ @has_app.include? app
43
+ end
44
+
45
+ alias_method :<<, :add
46
+ end
47
+ end
@@ -2,27 +2,15 @@ require 'mustermann'
2
2
 
3
3
  module Eldr
4
4
  class Matcher
5
- PATH_DELIMITER = '/'.freeze
6
- QUERY_PREFIX = '?'.freeze
7
- QUERY_DELIMITER = '&'.freeze
8
-
9
5
  def initialize(path, options = {})
10
- @path = path.is_a?(String) && path.empty? ? PATH_DELIMITER : path
6
+ @path = path
11
7
  @capture = options.delete(:capture)
12
- @default_values = options.delete(:default_values)
13
8
  end
14
9
 
15
10
  def match(pattern)
16
- match_data = handler.match(pattern)
17
- return match_data if match_data
18
- pattern = pattern[0..-2] if mustermann? && pattern != PATH_DELIMITER && pattern.end_with?(PATH_DELIMITER)
19
11
  handler.match(pattern)
20
12
  end
21
13
 
22
- def mustermann?
23
- handler.instance_of?(Mustermann)
24
- end
25
-
26
14
  def handler
27
15
  @handler ||=
28
16
  case @path
@@ -19,6 +19,7 @@ module Eldr
19
19
 
20
20
  ret_routes = routes[verb].select { |route| !route.match(pattern).nil? }
21
21
  raise NotFound if ret_routes.empty?
22
+ ret_routes.sort! { |a, b| a.order <=> b.order }
22
23
  ret_routes
23
24
  end
24
25
 
@@ -1,73 +1,63 @@
1
1
  module Eldr
2
2
  class Route
3
- attr_accessor :name, :app, :capture, :order, :options, :handler
4
- attr_accessor :before_filters, :after_filters, :to
3
+ attr_accessor :app, :verb, :path, :name, :order, :handler, :before_filters, :after_filters
5
4
 
6
- def initialize(verb: :get, path: '/', name: nil, options: {}, handler: nil, app: nil)
7
- @path, @verb = path, verb.to_s.upcase
5
+ def initialize(verb: :get, path: '/', name: nil, order: 0, handler: nil, to: nil)
6
+ @verb, @path, @name, @order = verb.to_s.upcase, path, name, order
7
+ @before_filters, @after_filters = [], []
8
+ handler ||= to
9
+ @handler = create_handler(handler)
10
+ end
8
11
 
9
- @app = app
10
- @capture = {}
11
- @before_filters = []
12
- @after_filters = []
13
- @name = name
12
+ def create_handler(handler)
13
+ return handler unless handler.is_a? String
14
14
 
15
- merge_with_options!(options)
15
+ controller, method = handler.split('#')
16
16
 
17
- @handler = handler
18
- @handler ||= @to
17
+ proc do |env|
18
+ obj = Object.const_get(controller).new
19
+ obj.send(method.to_sym, env)
20
+ end
19
21
  end
20
22
 
21
23
  def call(env, app: nil)
22
- # TODO: Investigate
23
- # maybe if we passed this around between methods it would be more perfomant
24
- # than setting the accessor?
25
24
  @app = app
26
25
 
27
- app.class.before_filters[:all].each { |filter| app.instance_exec(env, &filter) } if app
28
- @before_filters.each { |filter| app.instance_exec(env, &filter) }
26
+ call_before_filters(env)
29
27
 
30
- resp = response(env)
28
+ resp = call_handler(env)
31
29
 
32
- app.class.after_filters[:all].each { |filter| app.instance_exec(env, &filter) } if app
33
- @after_filters.each { |filter| app.instance_exec(env, &filter) }
30
+ call_after_filters(env)
34
31
 
35
32
  resp
36
33
  end
37
34
 
38
- def response(env)
39
- if @handler.is_a? Proc
40
- app.instance_exec(env, &@handler)
41
- elsif @handler.is_a? String
42
- rails_style_response(env)
35
+ def call_before_filters(env)
36
+ if app
37
+ app.class.before_filters[:all].each { |filter| app.instance_exec(env, &filter) }
38
+ before_filters.each { |filter| app.instance_exec(env, &filter) }
43
39
  else
44
- @handler.call(env)
40
+ before_filters.each { |filter| filter.call(env) }
45
41
  end
46
42
  end
47
43
 
48
- def rails_style_response(env)
49
- controller, method = @handler.split('#')
50
-
51
- obj = Object.const_get(controller).new
52
-
53
- if method
54
- obj.send(method.to_sym, env)
44
+ def call_after_filters(env)
45
+ if app
46
+ app.class.after_filters[:all].each { |filter| app.instance_exec(env, &filter) }
47
+ after_filters.each { |filter| app.instance_exec(env, &filter) }
55
48
  else
56
- obj.call(env)
49
+ after_filters.each { |filter| filter.call(env) }
57
50
  end
58
51
  end
59
52
 
60
- def merge_with_options!(options)
61
- @options = {} unless @options
62
- options.each_pair do |key, value|
63
- accessor?(key) ? __send__("#{key}=", value) : (@options[key] = value)
53
+ def call_handler(env)
54
+ if app and handler.is_a? Proc
55
+ app.instance_exec(env, &handler)
56
+ else
57
+ handler.call(env)
64
58
  end
65
59
  end
66
60
 
67
- def accessor?(key)
68
- respond_to?("#{key}=") && respond_to?(key)
69
- end
70
-
71
61
  def matcher
72
62
  @matcher ||= Matcher.new(path, capture: @capture)
73
63
  end
@@ -76,17 +66,9 @@ module Eldr
76
66
  matcher.match(pattern)
77
67
  end
78
68
 
79
- def path(*args)
80
- return @path if args.empty?
81
- params = args[0]
82
- params.delete(:captures)
83
- matcher.expand(params)
84
- end
85
-
86
69
  def params(pattern)
87
70
  params = matcher.handler.params(pattern)
88
- params ||= {}
89
- params
71
+ params ||= {} # rubocop:disable Lint/UselessAssignment
90
72
  end
91
73
  end
92
74
  end
@@ -1,3 +1,3 @@
1
1
  module Eldr
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -17,28 +17,20 @@ describe Eldr::Route do
17
17
  end
18
18
 
19
19
  it 'takes options and sets them' do
20
- route = Eldr::Route.new(options: { name: :cats })
20
+ route = Eldr::Route.new(name: :cats)
21
21
  expect(route.name).to eq(:cats)
22
22
  end
23
23
  end
24
24
 
25
- describe '#response' do
26
- let(:route) do
27
- Eldr::Route.new(handler: proc { 'cats' })
28
- end
29
-
30
- it 'determines type of handler and calls it' do
31
- expect(route.response({})).to eq('cats')
32
- end
33
- end
34
-
35
- describe '#rails_style_response' do
36
- let(:route) do
37
- Eldr::Route.new(handler: 'CatsHandler#index')
38
- end
25
+ describe '#create_handler' do
26
+ context 'rails style handler' do
27
+ let(:route) do
28
+ Eldr::Route.new(handler: 'CatsHandler#index')
29
+ end
39
30
 
40
- it 'calls a handler using a string like Controller#action' do
41
- expect(route.response({})).to eq('cats')
31
+ it 'wraps the handler in a proc' do
32
+ expect(route.create_handler('CatsHandler#index')).to be_a Proc
33
+ end
42
34
  end
43
35
  end
44
36
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eldr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - K-2052
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-25 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -112,16 +112,16 @@ dependencies:
112
112
  name: rack-test
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - '='
115
+ - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 0.6.2
117
+ version: '0.6'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '='
122
+ - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 0.6.2
124
+ version: '0.6'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: tilt
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -200,6 +200,7 @@ files:
200
200
  - lib/eldr.rb
201
201
  - lib/eldr/app.rb
202
202
  - lib/eldr/builder.rb
203
+ - lib/eldr/cascade.rb
203
204
  - lib/eldr/configuration.rb
204
205
  - lib/eldr/matcher.rb
205
206
  - lib/eldr/recognizer.rb