nyanko 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # Nyanko
2
- Nyanko is a Rails extension tool.
3
- This is deeply inspired from [Chanko](https://github.com/cookpad/chanko) and has same API as Chanko.
4
-
5
- ![](http://octodex.github.com/images/nyantocat.gif)
2
+ ```
3
+ _ ∧___
4
+ /(*゚ー゚) /\ Nyanko is a Rails extension framework,
5
+ /| ̄ ∪∪  ̄|\/ which is deeply inspired from Chanko
6
+ | |/ and has same API and clean implementation as Chanko.
7
+  ̄ ̄ ̄ ̄ ̄ https://github.com/cookpad/chanko
8
+ ```
6
9
 
7
10
 
8
11
  ## Requirements
@@ -98,8 +101,6 @@ $ rails s
98
101
 
99
102
  ## Todo
100
103
  * auto-reloading
101
- * logger
102
104
  * configuration
103
- * backwoard compatibility (alias ext to unit)
104
105
  * document
105
106
  * test (for now, test coverage is 100%)
data/lib/nyanko/config.rb CHANGED
@@ -8,6 +8,23 @@ module Nyanko
8
8
  def units_directory_path=(path)
9
9
  @units_directory_path = path
10
10
  end
11
+
12
+ def raise_error
13
+ @raise_error ||= lambda { Rails.env.development? }
14
+ @raise_error.respond_to?(:call) ? @raise_error.call : @raise_error
15
+ end
16
+
17
+ def raise_error=(raise_error)
18
+ @raise_error = raise_error
19
+ end
20
+
21
+ def backtrace_limit
22
+ @backtrace_limit ||= 10
23
+ end
24
+
25
+ def backtrace_limit=(limit)
26
+ @backtrace_limit = limit
27
+ end
11
28
  end
12
29
  end
13
30
  end
@@ -18,6 +18,7 @@ module Nyanko
18
18
  end
19
19
  end
20
20
  end
21
+ alias_method :ext_action, :unit_action
21
22
  end
22
23
  end
23
24
  end
@@ -0,0 +1,43 @@
1
+ module Nyanko
2
+ module ExceptionHandler
3
+ class << self
4
+ def handle(exception)
5
+ logger.debug Message.new(exception).to_s
6
+ raise exception if Config.raise_error
7
+ end
8
+
9
+ def logger
10
+ Rails.logger
11
+ end
12
+ end
13
+
14
+ class Message
15
+ def initialize(exception)
16
+ @exception = exception
17
+ end
18
+
19
+ def to_s
20
+ "#{prefix} #{klass}#{body}\n#{backtrace}"
21
+ end
22
+
23
+ def prefix
24
+ " [Nyanko]"
25
+ end
26
+
27
+ def body
28
+ unless @exception.message.empty?
29
+ " - #{@exception}"
30
+ end
31
+ end
32
+
33
+ def klass
34
+ @exception.class
35
+ end
36
+
37
+ def backtrace
38
+ lines = @exception.backtrace[0...Config.backtrace_limit]
39
+ lines.map {|line| "#{prefix} #{line}" }.join("\n")
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,11 +1,11 @@
1
1
  module Nyanko
2
2
  module Invoker
3
3
  class FunctionFinder
4
- UnitNotFound = Class.new(StandardError)
5
4
  FunctionNotFound = Class.new(StandardError)
6
5
 
7
- def self.find(*args)
8
- new(*args).find or raise FunctionNotFound
6
+ def self.find(context, options)
7
+ new(context, options).find or
8
+ raise FunctionNotFound, "The function for #{options[:functions].inspect} is not found"
9
9
  end
10
10
 
11
11
  def initialize(context, options)
@@ -29,7 +29,7 @@ module Nyanko
29
29
  alias_method_chain :find, :dependencies
30
30
 
31
31
  def find_unit(name)
32
- Loader.load(name) or raise UnitNotFound
32
+ Loader.load(name)
33
33
  end
34
34
 
35
35
  def has_inactive_dependent_unit?
@@ -10,8 +10,8 @@ module Nyanko
10
10
  result = function.invoke(self, options.invoke_options)
11
11
  result = surround_with_html_tag(result, function, options) if view?
12
12
  result
13
- rescue Exception
14
- p $!
13
+ rescue Exception => exception
14
+ ExceptionHandler.handle(exception)
15
15
  case
16
16
  when !block
17
17
  nil
data/lib/nyanko/loader.rb CHANGED
@@ -2,6 +2,8 @@ require "pathname"
2
2
 
3
3
  module Nyanko
4
4
  class Loader
5
+ UnitNotFound = Class.new(StandardError)
6
+
5
7
  class << self
6
8
  def load(unit_name)
7
9
  new(unit_name).load
@@ -31,7 +33,8 @@ module Nyanko
31
33
  def load_from_file
32
34
  require_unit_files
33
35
  cache[@name] = constantize
34
- rescue Exception
36
+ rescue Exception => exception
37
+ ExceptionHandler.handle(exception)
35
38
  cache[@name] = false
36
39
  nil
37
40
  end
@@ -50,6 +53,8 @@ module Nyanko
50
53
 
51
54
  def constantize
52
55
  @name.to_s.camelize.constantize
56
+ rescue NameError
57
+ raise UnitNotFound, "The unit for #{@name.inspect} is not found"
53
58
  end
54
59
 
55
60
  def cache
@@ -1,5 +1,3 @@
1
- require "rails"
2
-
3
1
  module Nyanko
4
2
  class Railtie < Rails::Railtie
5
3
  initializer "nyanko" do |app|
@@ -17,5 +17,6 @@ module Nyanko
17
17
  raise NoUnitError
18
18
  end
19
19
  end
20
+ alias_method :ext, :unit
20
21
  end
21
22
  end
@@ -1,3 +1,3 @@
1
1
  module Nyanko
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/nyanko.rb CHANGED
@@ -2,10 +2,12 @@ require "action_controller"
2
2
  require "action_view"
3
3
  require "active_record"
4
4
  require "active_support/all"
5
+ require "rails"
5
6
 
6
7
  require "nyanko/active_if"
7
8
  require "nyanko/config"
8
9
  require "nyanko/controller"
10
+ require "nyanko/exception_handler"
9
11
  require "nyanko/function"
10
12
  require "nyanko/helper"
11
13
  require "nyanko/invoker"
@@ -17,6 +17,10 @@ module ExampleUnit
17
17
  function(:bar) do
18
18
  "bar"
19
19
  end
20
+
21
+ function(:alias) do
22
+ "alias"
23
+ end
20
24
  end
21
25
 
22
26
  scope(:view) do
@@ -9,6 +9,7 @@ module Nyanko
9
9
  unit_action(:example_unit, :test)
10
10
  unit_action(:example_unit, :foo, :bar)
11
11
  unit_action(:example_unit, :error)
12
+ ext_action(:example_unit, :alias)
12
13
 
13
14
  def head(code)
14
15
  "Bad Request #{code}"
@@ -29,6 +30,10 @@ module Nyanko
29
30
  controller.bar.should == "bar"
30
31
  end
31
32
 
33
+ it "is aliased with `ext_action`" do
34
+ controller.alias.should == "alias"
35
+ end
36
+
32
37
  context "when invoke is fallen back" do
33
38
  it "halts with 400 status code" do
34
39
  controller.error.should == "Bad Request 400"
@@ -0,0 +1,57 @@
1
+ require "spec_helper"
2
+
3
+ module Nyanko
4
+ describe ExceptionHandler do
5
+ let(:exception) do
6
+ exception = Exception.new("error message")
7
+ exception.set_backtrace(20.times.map {|i| "test.rb:#{i + 1}:in `method#{i + 1}'" })
8
+ exception
9
+ end
10
+
11
+ let(:lines) do
12
+ <<-EOS.strip_heredoc.split("\n").map {|line| " #{line}" }
13
+ [Nyanko] Exception - error message
14
+ [Nyanko] test.rb:1:in `method1'
15
+ [Nyanko] test.rb:2:in `method2'
16
+ [Nyanko] test.rb:3:in `method3'
17
+ [Nyanko] test.rb:4:in `method4'
18
+ [Nyanko] test.rb:5:in `method5'
19
+ [Nyanko] test.rb:6:in `method6'
20
+ [Nyanko] test.rb:7:in `method7'
21
+ [Nyanko] test.rb:8:in `method8'
22
+ [Nyanko] test.rb:9:in `method9'
23
+ [Nyanko] test.rb:10:in `method10'
24
+ EOS
25
+ end
26
+
27
+ it "logs exception as debug level" do
28
+ Rails.logger.should_receive(:debug).with(lines.join("\n"))
29
+ described_class.handle(exception)
30
+ end
31
+
32
+ context "when Config.raise_error is truthy" do
33
+ around do |example|
34
+ origin, Config.raise_error = Config.raise_error, true
35
+ example.run
36
+ Config.raise_error = origin
37
+ end
38
+
39
+ it "raises up error" do
40
+ expect { described_class.handle(exception) }.to raise_error
41
+ end
42
+ end
43
+
44
+ describe "when Config.backtrace_limit is configured" do
45
+ around do |example|
46
+ origin, Config.backtrace_limit = Config.backtrace_limit, 5
47
+ example.run
48
+ Config.backtrace_limit = origin
49
+ end
50
+
51
+ it "prints backtrace up to configured depth" do
52
+ Rails.logger.should_receive(:debug).with(lines[0..5].join("\n"))
53
+ described_class.handle(exception)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -7,6 +7,10 @@ module Nyanko
7
7
  end
8
8
 
9
9
  describe "#unit" do
10
+ it "is aliased with `ext`" do
11
+ view.ext(:example_unit).should be_a UnitProxy
12
+ end
13
+
10
14
  context "when given unit name" do
11
15
  it "returns proxy for specified unit" do
12
16
  proxy = view.unit(:example_unit)
metadata CHANGED
@@ -1,74 +1,84 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyanko
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Ryo Nakamura
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-01-27 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rails
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - ">="
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: 3.0.10
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - ">="
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: 3.0.10
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: coffee-rails
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - ">="
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: 3.0.10
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - ">="
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: 3.0.10
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: jquery-rails
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - ">="
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - ">="
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: pry
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - ">="
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - ">="
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  - !ruby/object:Gem::Dependency
70
79
  name: rspec-rails
71
80
  requirement: !ruby/object:Gem::Requirement
81
+ none: false
72
82
  requirements:
73
83
  - - '='
74
84
  - !ruby/object:Gem::Version
@@ -76,6 +86,7 @@ dependencies:
76
86
  type: :development
77
87
  prerelease: false
78
88
  version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
79
90
  requirements:
80
91
  - - '='
81
92
  - !ruby/object:Gem::Version
@@ -83,71 +94,81 @@ dependencies:
83
94
  - !ruby/object:Gem::Dependency
84
95
  name: sass-rails
85
96
  requirement: !ruby/object:Gem::Requirement
97
+ none: false
86
98
  requirements:
87
- - - ">="
99
+ - - ! '>='
88
100
  - !ruby/object:Gem::Version
89
101
  version: 3.0.10
90
102
  type: :development
91
103
  prerelease: false
92
104
  version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
93
106
  requirements:
94
- - - ">="
107
+ - - ! '>='
95
108
  - !ruby/object:Gem::Version
96
109
  version: 3.0.10
97
110
  - !ruby/object:Gem::Dependency
98
111
  name: simplecov
99
112
  requirement: !ruby/object:Gem::Requirement
113
+ none: false
100
114
  requirements:
101
- - - ">="
115
+ - - ! '>='
102
116
  - !ruby/object:Gem::Version
103
117
  version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
107
122
  requirements:
108
- - - ">="
123
+ - - ! '>='
109
124
  - !ruby/object:Gem::Version
110
125
  version: '0'
111
126
  - !ruby/object:Gem::Dependency
112
127
  name: slim
113
128
  requirement: !ruby/object:Gem::Requirement
129
+ none: false
114
130
  requirements:
115
- - - ">="
131
+ - - ! '>='
116
132
  - !ruby/object:Gem::Version
117
133
  version: '0'
118
134
  type: :development
119
135
  prerelease: false
120
136
  version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
121
138
  requirements:
122
- - - ">="
139
+ - - ! '>='
123
140
  - !ruby/object:Gem::Version
124
141
  version: '0'
125
142
  - !ruby/object:Gem::Dependency
126
143
  name: sqlite3
127
144
  requirement: !ruby/object:Gem::Requirement
145
+ none: false
128
146
  requirements:
129
- - - ">="
147
+ - - ! '>='
130
148
  - !ruby/object:Gem::Version
131
149
  version: '0'
132
150
  type: :development
133
151
  prerelease: false
134
152
  version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
135
154
  requirements:
136
- - - ">="
155
+ - - ! '>='
137
156
  - !ruby/object:Gem::Version
138
157
  version: '0'
139
158
  - !ruby/object:Gem::Dependency
140
159
  name: uglifier
141
160
  requirement: !ruby/object:Gem::Requirement
161
+ none: false
142
162
  requirements:
143
- - - ">="
163
+ - - ! '>='
144
164
  - !ruby/object:Gem::Version
145
165
  version: '0'
146
166
  type: :development
147
167
  prerelease: false
148
168
  version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
149
170
  requirements:
150
- - - ">="
171
+ - - ! '>='
151
172
  - !ruby/object:Gem::Version
152
173
  version: '0'
153
174
  description: Nyanko is a Rails extension tool deeply inspired from Chanko
@@ -157,7 +178,7 @@ executables: []
157
178
  extensions: []
158
179
  extra_rdoc_files: []
159
180
  files:
160
- - ".gitignore"
181
+ - .gitignore
161
182
  - Gemfile
162
183
  - MIT-LICENSE
163
184
  - README.md
@@ -166,6 +187,7 @@ files:
166
187
  - lib/nyanko/active_if.rb
167
188
  - lib/nyanko/config.rb
168
189
  - lib/nyanko/controller.rb
190
+ - lib/nyanko/exception_handler.rb
169
191
  - lib/nyanko/function.rb
170
192
  - lib/nyanko/helper.rb
171
193
  - lib/nyanko/invoker.rb
@@ -228,6 +250,7 @@ files:
228
250
  - spec/fixtures/units/example_unit/example_unit.rb
229
251
  - spec/fixtures/units/inactive_unit/inactive_unit.rb
230
252
  - spec/nyanko/controller_spec.rb
253
+ - spec/nyanko/exception_handler_spec.rb
231
254
  - spec/nyanko/function_spec.rb
232
255
  - spec/nyanko/helper_spec.rb
233
256
  - spec/nyanko/invoker_spec.rb
@@ -240,26 +263,27 @@ files:
240
263
  - spec/spec_helper.rb
241
264
  homepage: https://github.com/r7kamura/nyanko
242
265
  licenses: []
243
- metadata: {}
244
266
  post_install_message:
245
267
  rdoc_options: []
246
268
  require_paths:
247
269
  - lib
248
270
  required_ruby_version: !ruby/object:Gem::Requirement
271
+ none: false
249
272
  requirements:
250
- - - ">="
273
+ - - ! '>='
251
274
  - !ruby/object:Gem::Version
252
275
  version: '0'
253
276
  required_rubygems_version: !ruby/object:Gem::Requirement
277
+ none: false
254
278
  requirements:
255
- - - ">="
279
+ - - ! '>='
256
280
  - !ruby/object:Gem::Version
257
281
  version: '0'
258
282
  requirements: []
259
283
  rubyforge_project:
260
- rubygems_version: 2.0.0.preview3
284
+ rubygems_version: 1.8.24
261
285
  signing_key:
262
- specification_version: 4
286
+ specification_version: 3
263
287
  summary: Rails extension tool
264
288
  test_files:
265
289
  - spec/dummy/README.rdoc
@@ -308,6 +332,7 @@ test_files:
308
332
  - spec/fixtures/units/example_unit/example_unit.rb
309
333
  - spec/fixtures/units/inactive_unit/inactive_unit.rb
310
334
  - spec/nyanko/controller_spec.rb
335
+ - spec/nyanko/exception_handler_spec.rb
311
336
  - spec/nyanko/function_spec.rb
312
337
  - spec/nyanko/helper_spec.rb
313
338
  - spec/nyanko/invoker_spec.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: 3bf6a9e3b51bc80536b6dd54ca34a36cce8bde19
4
- data.tar.gz: ee455931da637b0687a514b177bdbcb42a43c1b1
5
- !binary "U0hBNTEy":
6
- metadata.gz: 49253ba6ec4aa0195957714b64ab93802ed1727c3af38a90b2c7bf003f861517209261f2e1c9a0a8b3d9e5b39191fe09d526bc4d03ce9cc0696db4180ac9a3dd
7
- data.tar.gz: 29bb6270a940e5a74208536928f58c547c9b4fa042f69135e92a672b2dbf6b4458de9f388d7feaa58dbef81340c686131dddfe9096a8735ae63d0f2fa2598041