nyanko 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -16,6 +16,7 @@ end
16
16
 
17
17
  group :development, :test do
18
18
  gem "pry-rails"
19
+ gem "thin"
19
20
  end
20
21
 
21
22
  group :assets do
data/lib/nyanko/config.rb CHANGED
@@ -4,14 +4,19 @@ module Nyanko
4
4
  attr_accessor(
5
5
  :backtrace_limit,
6
6
  :cache_units,
7
+ :clear_units_cache,
7
8
  :compatible_css_class,
9
+ :enable_logger,
10
+ :proxy_method_name,
8
11
  :raise_error,
9
12
  :units_directory_path
10
13
  )
11
14
 
12
15
  def reset
13
16
  self.backtrace_limit = 10
14
- self.cache_units = !Rails.env.development?
17
+ self.clear_units_cache = Rails.env.development? || Rails.env.test?
18
+ self.enable_logger = true
19
+ self.proxy_method_name = :unit
15
20
  self.raise_error = Rails.env.development?
16
21
  self.units_directory_path = "app/units"
17
22
  end
@@ -5,6 +5,17 @@ module Nyanko
5
5
  module ClassMethods
6
6
  private
7
7
 
8
+ def inherited(base)
9
+ if Config.clear_units_cache && base.name == "ApplicationController"
10
+ base.class_eval do
11
+ prepend_before_filter do
12
+ Nyanko::Loader.cache.clear
13
+ end
14
+ end
15
+ end
16
+ super
17
+ end
18
+
8
19
  def unit_action(unit_name, *function_names, &block)
9
20
  options = function_names.extract_options!
10
21
  block ||= Proc.new { head 400 }
@@ -2,42 +2,9 @@ module Nyanko
2
2
  module ExceptionHandler
3
3
  class << self
4
4
  def handle(exception)
5
- logger.debug Message.new(exception).to_s
5
+ Logger.debug(exception)
6
6
  raise exception if Config.raise_error
7
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
8
  end
42
9
  end
43
10
  end
@@ -55,12 +55,11 @@ module Nyanko
55
55
 
56
56
  def with_unit_view_path(context)
57
57
  if context.view?
58
- origin = context.view_paths
59
58
  context.view_paths.unshift unit.view_path
60
59
  end
61
60
  yield
62
61
  ensure
63
- context.view_paths = origin if context.view?
62
+ context.view_paths.paths.unshift if context.view?
64
63
  end
65
64
  end
66
65
  end
@@ -11,6 +11,8 @@ module Nyanko
11
11
  result = function.invoke(self, options.invoke_options)
12
12
  result = surround_with_html_tag(result, function, options) if view?
13
13
  result
14
+ rescue FunctionFinder::FunctionNotFound
15
+ run_default
14
16
  rescue Exception => exception
15
17
  ExceptionHandler.handle(exception)
16
18
  run_default
data/lib/nyanko/loader.rb CHANGED
@@ -19,7 +19,7 @@ module Nyanko
19
19
  end
20
20
 
21
21
  def load
22
- if loaded? && Config.cache_units
22
+ if loaded?
23
23
  load_from_cache
24
24
  else
25
25
  load_from_file
@@ -0,0 +1,60 @@
1
+ module Nyanko
2
+ module Logger
3
+
4
+ class << self
5
+ ::Logger::Severity.constants.each do |level|
6
+ method_name = level.downcase
7
+ define_method(method_name) do |message|
8
+ logger.send(method_name, decorate(message)) if Config.enable_logger
9
+ end
10
+ end
11
+
12
+ def decorate(message)
13
+ Message.new(message).to_s
14
+ end
15
+
16
+ def logger
17
+ Rails.logger
18
+ end
19
+ end
20
+
21
+ class Message
22
+ PREFIX = " [Nyanko]"
23
+
24
+ def initialize(object)
25
+ @object = object
26
+ end
27
+
28
+ def to_s
29
+ prefix(content)
30
+ end
31
+
32
+ def prefix(str)
33
+ str.split("\n").map {|line| "#{PREFIX} #{line}" }.join("\n")
34
+ end
35
+
36
+ def content
37
+ if @object.is_a?(Exception)
38
+ "#{klass}#{body}\n#{backtrace}"
39
+ else
40
+ @object.to_s
41
+ end
42
+ end
43
+
44
+ def body
45
+ unless @object.message.empty?
46
+ " - #@object"
47
+ end
48
+ end
49
+
50
+ def klass
51
+ @object.class
52
+ end
53
+
54
+ def backtrace
55
+ lines = @object.backtrace[0...Config.backtrace_limit]
56
+ lines.map {|line| " #{line}" }.join("\n")
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/nyanko/unit.rb CHANGED
@@ -62,11 +62,10 @@ module Nyanko
62
62
  end
63
63
 
64
64
  def find_function(identifier, label)
65
- klass = ScopeFinder.find(identifier)
66
- candidates = scopes.keys
67
- target = klass.ancestors.find {|klass| scopes[klass] }
68
- scope = scopes[target]
69
- scope[label] if scope
65
+ scope = ScopeFinder.find(identifier)
66
+ target = scope.ancestors.find {|klass| scopes[klass] }
67
+ functions = scopes[target]
68
+ functions[label] if functions
70
69
  end
71
70
 
72
71
  def functions
@@ -7,12 +7,20 @@ module Nyanko
7
7
  include Helper
8
8
  end
9
9
 
10
- def unit(name = nil)
11
- name ||= Function.current_unit.try(:to_key)
12
- if name && unit = Loader.load(name)
13
- UnitProxy.new(unit, self)
10
+ def method_missing(method_name, *args, &block)
11
+ if method_name == Config.proxy_method_name.to_sym
12
+ class_eval do
13
+ define_method(method_name) do |*_args|
14
+ name = _args.first || Function.current_unit.try(:to_key)
15
+ if name && unit = Loader.load(name)
16
+ UnitProxy.new(unit, self)
17
+ end
18
+ end
19
+ end
20
+ send(method_name, args.first)
21
+ else
22
+ super
14
23
  end
15
24
  end
16
- alias_method :ext, :unit
17
25
  end
18
26
  end
@@ -1,3 +1,3 @@
1
1
  module Nyanko
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/nyanko.rb CHANGED
@@ -12,6 +12,7 @@ require "nyanko/function"
12
12
  require "nyanko/helper"
13
13
  require "nyanko/invoker"
14
14
  require "nyanko/loader"
15
+ require "nyanko/logger"
15
16
  require "nyanko/railtie"
16
17
  require "nyanko/unit"
17
18
  require "nyanko/unit_proxy"
data/nyanko.gemspec CHANGED
@@ -25,5 +25,6 @@ Gem::Specification.new do |gem|
25
25
  gem.add_development_dependency "simplecov"
26
26
  gem.add_development_dependency "slim"
27
27
  gem.add_development_dependency "sqlite3"
28
+ gem.add_development_dependency "thin"
28
29
  gem.add_development_dependency "uglifier"
29
30
  end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe ApplicationController do
4
+ controller do
5
+ def index
6
+ head 200
7
+ end
8
+ end
9
+
10
+ it "clears cache before each request" do
11
+ Nyanko::Loader.cache.should_receive(:clear).exactly(2)
12
+ get :index
13
+ get :index
14
+ end
15
+ end
@@ -2,33 +2,6 @@ require "spec_helper"
2
2
 
3
3
  module Nyanko
4
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
5
  context "when Config.raise_error is truthy" do
33
6
  around do |example|
34
7
  origin, Config.raise_error = Config.raise_error, true
@@ -37,20 +10,7 @@ module Nyanko
37
10
  end
38
11
 
39
12
  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)
13
+ expect { described_class.handle(Exception.new) }.to raise_error
54
14
  end
55
15
  end
56
16
  end
@@ -113,6 +113,13 @@ module Nyanko
113
113
  end
114
114
  end
115
115
 
116
+ context "when function is not found" do
117
+ it "runs default but not handled by ExceptionHandler" do
118
+ ExceptionHandler.should_not_receive(:handle)
119
+ view.invoke(:example_unit, :non_existent_function) { "default" }.should == "default"
120
+ end
121
+ end
122
+
116
123
  context "when an error is raised in invoking" do
117
124
  context "when block is given" do
118
125
  context "when context is a view" do
@@ -38,20 +38,6 @@ module Nyanko
38
38
  described_class.load(:non_existent_unit)
39
39
  end
40
40
  end
41
-
42
- context "when Config.cache_unit is false" do
43
- around do |example|
44
- origin, Config.cache_units = Config.cache_units, false
45
- example.run
46
- Config.cache_units = origin
47
- end
48
-
49
- it "loads unit without cache" do
50
- described_class.any_instance.should_not_receive(:load_from_cache)
51
- described_class.load(:example_unit)
52
- described_class.load(:example_unit)
53
- end
54
- end
55
41
  end
56
42
  end
57
43
  end
@@ -0,0 +1,90 @@
1
+ require "spec_helper"
2
+ require "stringio"
3
+
4
+ module Nyanko
5
+ describe Logger do
6
+ around do |example|
7
+ origin, Rails.logger = Rails.logger, ::Logger.new(io)
8
+ example.run
9
+ Rails.logger = origin
10
+ end
11
+
12
+ let(:io) do
13
+ StringIO.new
14
+ end
15
+
16
+ let(:log) do
17
+ io.string.rstrip
18
+ end
19
+
20
+ let(:exception) do
21
+ exception = Exception.new("error message")
22
+ exception.set_backtrace(20.times.map {|i| "test.rb:#{i + 1}:in `method#{i + 1}'" })
23
+ exception
24
+ end
25
+
26
+ let(:lines) do
27
+ <<-EOS.strip_heredoc.split("\n").map {|line| " #{line}" }
28
+ [Nyanko] Exception - error message
29
+ [Nyanko] test.rb:1:in `method1'
30
+ [Nyanko] test.rb:2:in `method2'
31
+ [Nyanko] test.rb:3:in `method3'
32
+ [Nyanko] test.rb:4:in `method4'
33
+ [Nyanko] test.rb:5:in `method5'
34
+ [Nyanko] test.rb:6:in `method6'
35
+ [Nyanko] test.rb:7:in `method7'
36
+ [Nyanko] test.rb:8:in `method8'
37
+ [Nyanko] test.rb:9:in `method9'
38
+ [Nyanko] test.rb:10:in `method10'
39
+ EOS
40
+ end
41
+
42
+ context "when Config.enable_logger is true" do
43
+ around do |example|
44
+ origin, Config.enable_logger = Config.enable_logger, true
45
+ example.run
46
+ Config.enable_logger = origin
47
+ end
48
+
49
+ context "when given Exception" do
50
+ it "parses and logs it" do
51
+ described_class.debug(exception)
52
+ log.should == lines.join("\n")
53
+ end
54
+
55
+ context "when Config.backtrace_limit is configured" do
56
+ around do |example|
57
+ origin, Config.backtrace_limit = Config.backtrace_limit, 5
58
+ example.run
59
+ Config.backtrace_limit = origin
60
+ end
61
+
62
+ it "prints backtrace up to configured depth" do
63
+ described_class.debug(exception)
64
+ log.should == lines[0..5].join("\n")
65
+ end
66
+ end
67
+ end
68
+
69
+ context "when given String" do
70
+ it "adds prefix" do
71
+ described_class.debug("test")
72
+ log.should == " [Nyanko] test"
73
+ end
74
+ end
75
+ end
76
+
77
+ context "when Config.enable_logger is false" do
78
+ around do |example|
79
+ origin, Config.enable_logger = Config.enable_logger, false
80
+ example.run
81
+ Config.enable_logger = origin
82
+ end
83
+
84
+ it "logs nothing" do
85
+ described_class.debug("test")
86
+ log.should == ""
87
+ end
88
+ end
89
+ end
90
+ end
@@ -7,10 +7,6 @@ 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
-
14
10
  context "when given unit name" do
15
11
  it "returns proxy for specified unit" do
16
12
  proxy = view.unit(:example_unit)
@@ -33,6 +29,20 @@ module Nyanko
33
29
  proxy.unit.should == ExampleUnit
34
30
  end
35
31
  end
32
+
33
+ context "when Config.proxy_method_name is configured" do
34
+ around do |example|
35
+ origin, Config.proxy_method_name = Config.proxy_method_name, :proxy
36
+ example.run
37
+ Config.proxy_method_name = origin
38
+ end
39
+
40
+ it "change this method name with it" do
41
+ proxy = view.proxy(:example_unit)
42
+ proxy.should be_a UnitProxy
43
+ view.should be_respond_to(:proxy)
44
+ end
45
+ end
36
46
  end
37
47
  end
38
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyanko
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-28 00:00:00.000000000 Z
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -155,6 +155,22 @@ dependencies:
155
155
  - - ! '>='
156
156
  - !ruby/object:Gem::Version
157
157
  version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: thin
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
158
174
  - !ruby/object:Gem::Dependency
159
175
  name: uglifier
160
176
  requirement: !ruby/object:Gem::Requirement
@@ -194,6 +210,7 @@ files:
194
210
  - lib/nyanko/invoker/function_finder.rb
195
211
  - lib/nyanko/invoker/options.rb
196
212
  - lib/nyanko/loader.rb
213
+ - lib/nyanko/logger.rb
197
214
  - lib/nyanko/railtie.rb
198
215
  - lib/nyanko/unit.rb
199
216
  - lib/nyanko/unit/extender.rb
@@ -204,6 +221,7 @@ files:
204
221
  - lib/nyanko/unit_proxy_provider.rb
205
222
  - lib/nyanko/version.rb
206
223
  - nyanko.gemspec
224
+ - spec/controllers/application_controller_spec.rb
207
225
  - spec/dummy/README.rdoc
208
226
  - spec/dummy/Rakefile
209
227
  - spec/dummy/app/assets/javascripts/application.js
@@ -255,6 +273,7 @@ files:
255
273
  - spec/nyanko/helper_spec.rb
256
274
  - spec/nyanko/invoker_spec.rb
257
275
  - spec/nyanko/loader_spec.rb
276
+ - spec/nyanko/logger_spec.rb
258
277
  - spec/nyanko/unit/extender_spec.rb
259
278
  - spec/nyanko/unit/scope_finder_spec.rb
260
279
  - spec/nyanko/unit_proxy_provider_spec.rb
@@ -286,6 +305,7 @@ signing_key:
286
305
  specification_version: 3
287
306
  summary: Rails extension tool
288
307
  test_files:
308
+ - spec/controllers/application_controller_spec.rb
289
309
  - spec/dummy/README.rdoc
290
310
  - spec/dummy/Rakefile
291
311
  - spec/dummy/app/assets/javascripts/application.js
@@ -337,6 +357,7 @@ test_files:
337
357
  - spec/nyanko/helper_spec.rb
338
358
  - spec/nyanko/invoker_spec.rb
339
359
  - spec/nyanko/loader_spec.rb
360
+ - spec/nyanko/logger_spec.rb
340
361
  - spec/nyanko/unit/extender_spec.rb
341
362
  - spec/nyanko/unit/scope_finder_spec.rb
342
363
  - spec/nyanko/unit_proxy_provider_spec.rb