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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64027ca973fda52e94edacef4d3570f8e78a6e8c
4
- data.tar.gz: 47763de06fb5ffb12babdcc07d7ad0ab61ff86a0
3
+ metadata.gz: 107d11189a291b7486db035f572921ff75946129
4
+ data.tar.gz: 484670541b44ee3c37e59c219a5769a489caccce
5
5
  SHA512:
6
- metadata.gz: 72a4aaad4c7473ec7b680ff1f52cf490084cf88d969a960f976bf7d79b854e2dc19ff63fadf968af9fd0a94d54046bedfb430e1b368e61c3621502bb29ec80eb
7
- data.tar.gz: 3a81393aa445660e1105fecbc2002f8802c27c571d239621cb8a9bdc6041c06e5ad5037922a2a01232a4b6a83aea50de7ed410364bf04f189369e8e220822526
6
+ metadata.gz: a130aa58b46327176465becf3d4d030bdaad6bb6b681ca0c8be4f55658b6fe72351e06050c9d1eafc7a98e4319629045697ea55b09f02725023082453e27fa8c
7
+ data.tar.gz: 5fb16a4139ac277a4cea7da8320f2e3ead517d8bcaa54c5ae0b71aa20c5dbb7cfe1c75fe5462d33f7b8aa71a3adc85affb562e4997978d634563db9d8f3405db
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  ---
2
2
  rvm:
3
- - 1.9.2
4
3
  - 1.9.3
5
4
  - 2.0.0
6
5
  - jruby-19mode
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ 3.3.0
2
+ - App.config
3
+ - before_initialize hooks
4
+ - after_initialize hooks
5
+
1
6
  3.2.2
2
7
  - Use Journey directly from ActionDispatch, since rails/journey is obsolete
3
8
  - Fix indifferent nested params
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
- def self.inheritable name, value
10
- @_inheritables ||= []
11
- @_inheritables << name
12
- self.class.send :attr_accessor, name
13
- self.send "#{name}=", value
14
- end
15
-
16
- def self.inherited subclass
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 parent do
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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module NYNY
2
- VERSION = "3.2.2"
2
+ VERSION = "3.3.0"
3
3
  end
@@ -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
@@ -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 == Dir.pwd
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 == File.join(Dir.pwd, "foo")
13
+ NYNY.root.join("foo").should == Pathname.pwd + "foo"
14
14
  end
15
15
  end
@@ -43,7 +43,7 @@ describe RequestScope do
43
43
  delete '/cookie_halt' do
44
44
  cookies.delete 'foo'
45
45
  halt 200, {}, 'blah'
46
- 'blah'
46
+ cookies[:foo] = 'bar'
47
47
  end
48
48
  end
49
49
  end
data/spec/runner_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Runner do
3
+ describe 'App.run!' do
4
4
  let (:kls) { mock_app_class {} }
5
5
 
6
6
  before do
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.2.2
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-01-28 00:00:00.000000000 Z
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
@@ -1,11 +0,0 @@
1
- require 'better_errors'
2
-
3
- module NYNY
4
- module Runner
5
- def run! port=9292
6
- use Rack::CommonLogger
7
- use BetterErrors::Middleware unless NYNY.env.production?
8
- Rack::Handler.pick(['thin', 'webrick']).run new, :Port => port
9
- end
10
- end
11
- end