nyny 3.2.2 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/CHANGELOG +5 -0
- data/README.md +45 -0
- data/lib/nyny.rb +0 -32
- data/lib/nyny/app.rb +38 -23
- data/lib/nyny/primitives.rb +38 -0
- data/lib/nyny/{core-ext/templates.rb → templates.rb} +0 -0
- data/lib/nyny/version.rb +1 -1
- data/spec/config_spec.rb +46 -0
- data/spec/inheritance_spec.rb +9 -0
- data/spec/initialize_hooks_spec.rb +25 -0
- data/spec/nyny_spec.rb +2 -2
- data/spec/request_scope_spec.rb +1 -1
- data/spec/runner_spec.rb +1 -1
- metadata +7 -4
- data/lib/nyny/core-ext/runner.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 107d11189a291b7486db035f572921ff75946129
|
4
|
+
data.tar.gz: 484670541b44ee3c37e59c219a5769a489caccce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a130aa58b46327176465becf3d4d030bdaad6bb6b681ca0c8be4f55658b6fe72351e06050c9d1eafc7a98e4319629045697ea55b09f02725023082453e27fa8c
|
7
|
+
data.tar.gz: 5fb16a4139ac277a4cea7da8320f2e3ead517d8bcaa54c5ae0b71aa20c5dbb7cfe1c75fe5462d33f7b8aa71a3adc85affb562e4997978d634563db9d8f3405db
|
data/.travis.yml
CHANGED
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -40,6 +40,7 @@ Open the browser at [http://localhost:9292](http://localhost:9292)
|
|
40
40
|
- [Why use NYNY instead of Sinatra](#why-use-nyny-instead-of-sinatra)
|
41
41
|
- [Usage](#usage)
|
42
42
|
- [Environment](#environment)
|
43
|
+
- [Configuration](#configuration)
|
43
44
|
- [Running](#running)
|
44
45
|
- [Defining routes](#defining-routes)
|
45
46
|
- [Request scope](#request-scope)
|
@@ -105,6 +106,50 @@ production
|
|
105
106
|
true
|
106
107
|
```
|
107
108
|
|
109
|
+
## Configuration
|
110
|
+
You can configure your app by attaching arbitrary properties to config object:
|
111
|
+
```ruby
|
112
|
+
class App << NYNY::App
|
113
|
+
config.foo = 'bar'
|
114
|
+
end
|
115
|
+
|
116
|
+
App.config.foo #=> 'bar'
|
117
|
+
```
|
118
|
+
|
119
|
+
Or, you can use the configure block:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
class App < NYNY::App
|
123
|
+
configure do
|
124
|
+
config.always = true
|
125
|
+
end
|
126
|
+
|
127
|
+
configdure :production do
|
128
|
+
config.prod = true
|
129
|
+
end
|
130
|
+
|
131
|
+
configure :test, :development do
|
132
|
+
config.unsafe = true
|
133
|
+
end
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
Also, NYNY provides a simple api for hooking into the app's initialization:
|
138
|
+
```ruby
|
139
|
+
class App < NYNY::App
|
140
|
+
before_initialize do |app|
|
141
|
+
#this will be executed just before the Rack app is compiled
|
142
|
+
#'app' is the a App instance
|
143
|
+
end
|
144
|
+
|
145
|
+
after_initialize do |app, rack_app|
|
146
|
+
#this will be executed after the Rack app is compiled
|
147
|
+
#'app' is the a App instance
|
148
|
+
#'rack_app' is the main block which will be called on any request
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
108
153
|
## Running
|
109
154
|
There are two ways to run a NYNY app __directly__ [[?]](#middleware):
|
110
155
|
|
data/lib/nyny.rb
CHANGED
@@ -1,34 +1,2 @@
|
|
1
|
-
require 'rack'
|
2
|
-
|
3
1
|
require 'nyny/version'
|
4
2
|
require 'nyny/app'
|
5
|
-
require 'nyny/core-ext/runner'
|
6
|
-
require 'nyny/core-ext/templates'
|
7
|
-
|
8
|
-
module NYNY
|
9
|
-
class EnvString < String
|
10
|
-
[:production, :development, :test].each do |env|
|
11
|
-
define_method "#{env}?" do
|
12
|
-
self == env.to_s
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class PathString < String
|
18
|
-
def join other
|
19
|
-
File.join(self, other)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.root
|
24
|
-
@root ||= PathString.new(Dir.pwd)
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.env
|
28
|
-
@env ||= EnvString.new(ENV['RACK_ENV'] || 'development')
|
29
|
-
end
|
30
|
-
|
31
|
-
App.register NYNY::Runner
|
32
|
-
App.register NYNY::Templates
|
33
|
-
end
|
34
|
-
|
data/lib/nyny/app.rb
CHANGED
@@ -1,32 +1,24 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'better_errors'
|
3
|
+
require 'ostruct'
|
1
4
|
require 'nyny/primitives'
|
2
5
|
require 'nyny/request_scope'
|
3
6
|
require 'nyny/router'
|
7
|
+
require 'nyny/templates'
|
4
8
|
|
5
9
|
module NYNY
|
6
10
|
class App
|
11
|
+
include NYNY::Inheritable
|
7
12
|
HTTP_VERBS = [:delete, :get, :head, :options, :patch, :post, :put, :trace]
|
8
13
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
@_inheritables.each do |attr|
|
18
|
-
subclass.send "#{attr}=", self.send(attr).clone
|
19
|
-
subclass.instance_variable_set "@_inheritables", @_inheritables.clone
|
20
|
-
end
|
21
|
-
|
22
|
-
super
|
23
|
-
end
|
24
|
-
|
25
|
-
inheritable :builder, Rack::Builder.new
|
26
|
-
inheritable :route_defs, []
|
27
|
-
inheritable :before_hooks, []
|
28
|
-
inheritable :after_hooks, []
|
29
|
-
inheritable :scope_class, Class.new(RequestScope)
|
14
|
+
inheritable :builder, Rack::Builder.new
|
15
|
+
inheritable :scope_class, Class.new(RequestScope)
|
16
|
+
inheritable :config, OpenStruct.new
|
17
|
+
inheritable :route_defs, []
|
18
|
+
inheritable :before_hooks, []
|
19
|
+
inheritable :after_hooks, []
|
20
|
+
inheritable :before_init_hooks, []
|
21
|
+
inheritable :after_init_hooks, []
|
30
22
|
|
31
23
|
def initialize app=nil
|
32
24
|
self.class.builder.run Router.new({
|
@@ -37,7 +29,9 @@ module NYNY
|
|
37
29
|
:fallback => app
|
38
30
|
})
|
39
31
|
|
32
|
+
self.class.before_init_hooks.each {|h| h.call(self)}
|
40
33
|
@app = self.class.builder.to_app
|
34
|
+
self.class.after_init_hooks.each {|h| h.call(self, @app)}
|
41
35
|
end
|
42
36
|
|
43
37
|
def call env
|
@@ -67,9 +61,8 @@ module NYNY
|
|
67
61
|
|
68
62
|
def namespace url, &block
|
69
63
|
scope = self.scope_class
|
70
|
-
parent = self.superclass
|
71
64
|
|
72
|
-
klass = Class.new
|
65
|
+
klass = Class.new self.superclass do
|
73
66
|
self.scope_class = scope
|
74
67
|
class_eval(&block)
|
75
68
|
end
|
@@ -85,6 +78,20 @@ module NYNY
|
|
85
78
|
after_hooks << Proc.new(&blk)
|
86
79
|
end
|
87
80
|
|
81
|
+
def before_initialize &blk
|
82
|
+
before_init_hooks << Proc.new(&blk)
|
83
|
+
end
|
84
|
+
|
85
|
+
def after_initialize &blk
|
86
|
+
after_init_hooks << Proc.new(&blk)
|
87
|
+
end
|
88
|
+
|
89
|
+
def configure *envs, &block
|
90
|
+
if envs.map(&:to_sym).include?(NYNY.env.to_sym) or envs.empty?
|
91
|
+
instance_eval(&block)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
88
95
|
def use middleware, *args, &block
|
89
96
|
builder.use middleware, *args, &block
|
90
97
|
end
|
@@ -93,6 +100,14 @@ module NYNY
|
|
93
100
|
args << Module.new(&block) if block_given?
|
94
101
|
args.each {|m| scope_class.send :include, m }
|
95
102
|
end
|
103
|
+
|
104
|
+
def run! port=9292
|
105
|
+
use Rack::CommonLogger
|
106
|
+
use BetterErrors::Middleware unless NYNY.env.production?
|
107
|
+
Rack::Handler.pick(['thin', 'webrick']).run new, :Port => port
|
108
|
+
end
|
96
109
|
end #class methods
|
110
|
+
|
111
|
+
register NYNY::Templates
|
97
112
|
end
|
98
113
|
end
|
data/lib/nyny/primitives.rb
CHANGED
@@ -1,6 +1,36 @@
|
|
1
1
|
require 'active_support/hash_with_indifferent_access'
|
2
|
+
require 'pathname'
|
2
3
|
|
3
4
|
module NYNY
|
5
|
+
module Inheritable
|
6
|
+
def self.included base
|
7
|
+
base.class_eval do
|
8
|
+
def self.inheritable name, value
|
9
|
+
@_inheritables ||= []
|
10
|
+
@_inheritables << name
|
11
|
+
self.class.send :attr_accessor, name
|
12
|
+
self.send "#{name}=", value
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.inherited subclass
|
16
|
+
@_inheritables.each do |attr|
|
17
|
+
subclass.send "#{attr}=", self.send(attr).clone
|
18
|
+
subclass.instance_variable_set "@_inheritables", @_inheritables.clone
|
19
|
+
end
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class EnvString < String
|
27
|
+
[:production, :development, :test].each do |env|
|
28
|
+
define_method "#{env}?" do
|
29
|
+
self == env.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
4
34
|
class Request < Rack::Request
|
5
35
|
def params
|
6
36
|
@params ||= ActiveSupport::HashWithIndifferentAccess.new(super)
|
@@ -17,4 +47,12 @@ module NYNY
|
|
17
47
|
end
|
18
48
|
alias_method :body=, :rewrite
|
19
49
|
end
|
50
|
+
|
51
|
+
def self.root
|
52
|
+
@root ||= Pathname.pwd
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.env
|
56
|
+
@env ||= EnvString.new(ENV['RACK_ENV'] || 'development')
|
57
|
+
end
|
20
58
|
end
|
File without changes
|
data/lib/nyny/version.rb
CHANGED
data/spec/config_spec.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe App do
|
4
|
+
describe 'config' do
|
5
|
+
let (:klass) do
|
6
|
+
mock_app_class do
|
7
|
+
get '/' do
|
8
|
+
p config
|
9
|
+
config.foo.should == 'bar'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
let (:app) do
|
15
|
+
Rack::MockRequest.new(klass.new)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'sets any property' do
|
19
|
+
klass.config.foo = 'bar'
|
20
|
+
klass.config.foo.should == 'bar'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'can configure multiple environments at once' do
|
24
|
+
NYNY.stub :env => ['development', 'test'].sample
|
25
|
+
|
26
|
+
kls = mock_app_class do
|
27
|
+
configure :development, :test do
|
28
|
+
config.test_dev = true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
kls.config.test_dev.should == true
|
33
|
+
NYNY.unstub :env
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'configures all environments by default' do
|
37
|
+
kls = mock_app_class do
|
38
|
+
configure do
|
39
|
+
config.foo = true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
kls.config.foo.should == true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/inheritance_spec.rb
CHANGED
@@ -7,6 +7,8 @@ describe NYNY::App do
|
|
7
7
|
def parent_helper; :parent; end
|
8
8
|
end
|
9
9
|
|
10
|
+
config.parent = true
|
11
|
+
|
10
12
|
before do
|
11
13
|
headers['parent before'] = 'true'
|
12
14
|
end
|
@@ -84,5 +86,12 @@ describe NYNY::App do
|
|
84
86
|
child.get('/nested').headers['parent before'].should_not be_nil
|
85
87
|
child.get('/nested').headers['parent after'].should_not be_nil
|
86
88
|
end
|
89
|
+
|
90
|
+
it 'works correctly for config' do
|
91
|
+
Child.config.parent.should == true
|
92
|
+
Child.config.parent = false
|
93
|
+
Child.config.parent.should == false
|
94
|
+
Parent.config.parent.should == true
|
95
|
+
end
|
87
96
|
end
|
88
97
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NYNY::App do
|
4
|
+
let (:target) { OpenStruct.new }
|
5
|
+
|
6
|
+
let (:app) do
|
7
|
+
mock_app_class do
|
8
|
+
before_initialize do |app|
|
9
|
+
app.is_a?(NYNY::App).should == true
|
10
|
+
Rack::Builder.any_instance.should_receive(:to_app)
|
11
|
+
.and_return(Proc.new {})
|
12
|
+
end
|
13
|
+
|
14
|
+
after_initialize do |app, prc|
|
15
|
+
app.is_a?(NYNY::App).should == true
|
16
|
+
prc.respond_to?(:call).should == true
|
17
|
+
Rack::Builder.any_instance.should_not_receive(:to_app)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'runs the hooks in the correct order' do
|
23
|
+
app.new
|
24
|
+
end
|
25
|
+
end
|
data/spec/nyny_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe NYNY do
|
4
4
|
its 'root points to pwd' do
|
5
|
-
NYNY.root.should ==
|
5
|
+
NYNY.root.should == Pathname.pwd
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'has the correct env' do
|
@@ -10,6 +10,6 @@ describe NYNY do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
its 'root can join a path' do
|
13
|
-
NYNY.root.join("foo").should ==
|
13
|
+
NYNY.root.join("foo").should == Pathname.pwd + "foo"
|
14
14
|
end
|
15
15
|
end
|
data/spec/request_scope_spec.rb
CHANGED
data/spec/runner_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nyny
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Lisnic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-contrib
|
@@ -126,15 +126,16 @@ files:
|
|
126
126
|
- Rakefile
|
127
127
|
- lib/nyny.rb
|
128
128
|
- lib/nyny/app.rb
|
129
|
-
- lib/nyny/core-ext/runner.rb
|
130
|
-
- lib/nyny/core-ext/templates.rb
|
131
129
|
- lib/nyny/primitives.rb
|
132
130
|
- lib/nyny/request_scope.rb
|
133
131
|
- lib/nyny/router.rb
|
132
|
+
- lib/nyny/templates.rb
|
134
133
|
- lib/nyny/version.rb
|
135
134
|
- nyny.gemspec
|
136
135
|
- spec/app_spec.rb
|
136
|
+
- spec/config_spec.rb
|
137
137
|
- spec/inheritance_spec.rb
|
138
|
+
- spec/initialize_hooks_spec.rb
|
138
139
|
- spec/nyny_spec.rb
|
139
140
|
- spec/primitives_spec.rb
|
140
141
|
- spec/request_scope_spec.rb
|
@@ -171,7 +172,9 @@ specification_version: 4
|
|
171
172
|
summary: New York, New York.
|
172
173
|
test_files:
|
173
174
|
- spec/app_spec.rb
|
175
|
+
- spec/config_spec.rb
|
174
176
|
- spec/inheritance_spec.rb
|
177
|
+
- spec/initialize_hooks_spec.rb
|
175
178
|
- spec/nyny_spec.rb
|
176
179
|
- spec/primitives_spec.rb
|
177
180
|
- spec/request_scope_spec.rb
|
data/lib/nyny/core-ext/runner.rb
DELETED