rescue-dog 0.3.4 → 0.3.5

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: 0e2a7f7faf7befcd8fb279f982baa669fcf66964
4
- data.tar.gz: 21ea12236b212cc34badb3351fe6892bff346f42
3
+ metadata.gz: 94b0003e3a52320379af227d77392979bc89d0da
4
+ data.tar.gz: 9a998aedbc2b68dd06211713b6b4306d32d40ab8
5
5
  SHA512:
6
- metadata.gz: cbfc2e1db3316d00f46c99ebf4a79c2e5b24e0e4cfc16512e2ce095da21b3c2927de1d0caa3ce556e590b3a16845a402782b44ab6be160729c3452654bcbbd89
7
- data.tar.gz: b43644434d7c85cc0e5c1835b8a92c3769443a5f4bcafc610640a77b894f2e149ac09efde1d77c5ed32a93a86e27cf160cada1104be73239dc47f26258ad285c
6
+ metadata.gz: 5430d1c18141a9f6cff373f5b98efcf6c1cd4dbb7aa6cd877c963c57f6fee11b93e2ff49a6f0eae3ae096e6533b6b49af1432a31f1cba4e860332c686cda67e4
7
+ data.tar.gz: 178688de7e29b2fdbfd99e191a4681b69e3a2bca222635e251284f7215ec6afeed236a43f517fc1527c72ddffad87430e78f51048a8ec31e5e97c4c82330870a
data/.travis.yml CHANGED
@@ -5,6 +5,7 @@ rvm:
5
5
  - 2.0.0
6
6
  - ruby-head
7
7
  - jruby-19mode
8
+ - jruby-20mode
8
9
  - jruby-head
9
10
  - rbx-19mode
10
11
  matrix:
data/README.md CHANGED
@@ -26,19 +26,73 @@ Or install it yourself as:
26
26
  $ gem install rescue-dog
27
27
  ```
28
28
 
29
- ## Simple CRUD Actions
29
+ ## Declare CRUD Actions
30
30
 
31
31
  1. Include `Rescue::Controller` (Rescue::Controller::Static` or `Rescue::Controller::Dynamic`).
32
32
  2. Call `rescue_controller` method.
33
+ 3. Declare `xxx_params` method. (cf. Rails 4 / Strong Parameters)
33
34
 
34
- ### Define Actions
35
+ ### Simple CRUD Actions
35
36
 
36
37
  ```ruby
37
38
  class UsersController < ApplicationController
38
39
  rescue_controller User, :new, :edit, :show,
39
40
  create: { render: lambda { redirect_to edit_user_path(@user) } ,rescue: lambda { render :new } },
40
41
  update: { render: lambda { redirect_to edit_user_path(@user) } ,rescue: lambda { render :edit } },
41
- delete: { render: lambda { redirect_to root_path } ,rescue: lambda { render :edit } }
42
+ destroy: { render: lambda { redirect_to root_path } ,rescue: lambda { render :edit } }
43
+
44
+ ...
45
+
46
+ private
47
+ def create_params
48
+ params.require(:user).permit(
49
+ :name, :email, :password
50
+ )
51
+ end
52
+
53
+ def update_params
54
+ params.require(:user).permit(
55
+ :email, :password
56
+ )
57
+ end
58
+
59
+ # Destroyed condition variable object
60
+ def destroy_params
61
+ { id: params[:id] }
62
+ end
63
+ end
64
+ ```
65
+
66
+ ### Customized Actions
67
+
68
+ ```ruby
69
+ class UsersController < ApplicationController
70
+ rescue_controller User, :new, :edit, :show
71
+
72
+ def create
73
+ rescue_respond(:customized_create_call, create_params,
74
+ render: lambda { redirect_to edit_user_path(@user) },
75
+ rescue: lambda { render :new } }
76
+ )
77
+ end
78
+
79
+ private
80
+ def create_params
81
+ params.require(:user).permit(
82
+ :name, :email, :password
83
+ )
84
+ end
85
+
86
+ def customized_create_call params
87
+ if User.exists?(params)
88
+ raise UserDuplicationError, "your email is duplicated!"
89
+ else
90
+ @user = User.new(params)
91
+ @user.save!
92
+ end
93
+ end
94
+
95
+ end
42
96
  ```
43
97
 
44
98
  ## Render Errors
@@ -4,6 +4,7 @@ module Rescue
4
4
 
5
5
  def self.included(base)
6
6
  base.extend Rescue::Controller::ClassMethods
7
+ base.send(:private, :rescue_respond)
7
8
  end
8
9
 
9
10
  def rescue_respond call, params, options = {}
@@ -31,7 +31,7 @@ module Rescue
31
31
  names.each do |name|
32
32
  options = args[name]||{}
33
33
  type = options[:type]||name
34
- raise RuntimeError "`name` is already defined." if object.private_method_defined?(name)
34
+ raise RuntimeError, "`#{name}` is already defined." if object.method_defined?(name)
35
35
  case type
36
36
  when :show, :edit
37
37
  object.send(:define_method, name) do
@@ -46,11 +46,15 @@ module Rescue
46
46
  end
47
47
  when :create, :update, :destroy
48
48
  object.send(:define_method, name) do
49
- params = send(options[:params]||:"#{name}_params")
50
- rescue_respond(:"#{type}_call", params, options)
49
+ begin
50
+ params = send(options[:params]||:"#{name}_params")
51
+ rescue_respond(:"#{type}_call", params, options)
52
+ rescue NoMethodError => e
53
+ raise NoParameterMethodError.new(self.class, e.name)
54
+ end
51
55
  end
52
56
  else
53
- raise RuntimeError "Undefined action type `#{type}`."
57
+ raise Rescue::NoActionError.new(type)
54
58
  end
55
59
  end
56
60
  end
@@ -0,0 +1,16 @@
1
+ module Rescue
2
+
3
+
4
+ class NoActionError < ArgumentError
5
+ def initialize type
6
+ super "Undefined `#{type}` type. Please specify :show, :edit, :new, :create, :update, or :destory on the :type argument."
7
+ end
8
+ end
9
+
10
+ class NoParameterMethodError < NoMethodError
11
+ def initialize clazz, name
12
+ super "Undefined method `#{name}` for #{clazz.name}. Please define private method `#{name}` to return the permissible parameters."
13
+ end
14
+ end
15
+
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Rescue
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.5"
3
3
  end
data/lib/rescue-dog.rb CHANGED
@@ -6,6 +6,7 @@ require "rescue/controllers/action.rb"
6
6
  require "rescue/controllers/flash.rb"
7
7
  require 'rescue/exceptions/application_error.rb'
8
8
  require 'rescue/exceptions/respond_error.rb'
9
+ require 'rescue/errors.rb'
9
10
 
10
11
  module Rescue
11
12
  class Bind
data/rescue-dog.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Rescue::VERSION
9
9
  gem.authors = ["yulii"]
10
10
  gem.email = ["yuliinfo@gmail.com"]
11
- gem.description = %q{respond to an exception raised in Rails}
12
- gem.summary = %q{define respond methods}
11
+ gem.description = %q{Responds status and error handling for Rails}
12
+ gem.summary = %q{Declare simple CRUD and respond error methods}
13
13
  gem.homepage = "https://github.com/yulii/rescue-dog"
14
14
  gem.license = 'MIT'
15
15
 
@@ -3,19 +3,19 @@ require 'spec_helper'
3
3
 
4
4
  describe Rescue::Controller do
5
5
 
6
- let(:object) do
6
+ let(:controller) do
7
7
  Class.new ApplicationController do
8
8
  include Rescue::Controller
9
9
  rescue_controller RescueModel,
10
10
  show: [:show_action],
11
11
  edit: [:edit_action],
12
12
  new: [:new_action],
13
- create: { render: lambda { } ,rescue: lambda { } },
14
- update: { render: lambda { } ,rescue: lambda { } },
15
- destroy: { render: lambda { } ,rescue: lambda { } },
16
- create_action: { type: :create ,render: lambda { } ,rescue: lambda { } },
17
- update_action: { type: :update ,render: lambda { } ,rescue: lambda { } },
18
- destroy_action: { type: :destroy ,render: lambda { } ,rescue: lambda { } }
13
+ create: { render: lambda { } ,rescue: lambda { raise $! } },
14
+ update: { render: lambda { } ,rescue: lambda { raise $! } },
15
+ destroy: { render: lambda { } ,rescue: lambda { raise $! } },
16
+ create_action: { type: :create ,render: lambda { } ,rescue: lambda { raise $! } ,params: :customized_params },
17
+ update_action: { type: :update ,render: lambda { } ,rescue: lambda { raise $! } ,params: :customized_params },
18
+ destroy_action: { type: :destroy ,render: lambda { } ,rescue: lambda { raise $! } ,params: :customized_params }
19
19
 
20
20
  def customized_create_action
21
21
  rescue_respond(:create_call, create_params,
@@ -26,7 +26,7 @@ describe Rescue::Controller do
26
26
 
27
27
  def customized_update_action
28
28
  rescue_respond(:update_call, update_params,
29
- render: lambda { return true },
29
+ render: lambda { },
30
30
  rescue: lambda { }
31
31
  )
32
32
  end
@@ -37,76 +37,129 @@ describe Rescue::Controller do
37
37
  rescue: lambda { }
38
38
  )
39
39
  end
40
+
41
+ def execute
42
+ rescue_respond(:execute_call, execute_params,
43
+ render: lambda { },
44
+ rescue: lambda { }
45
+ )
46
+ end
40
47
  end
41
48
  end
42
49
 
43
- it "should be defined `rescue_respond`" do
44
- expect(object.method_defined? :rescue_respond).to be_true
50
+ # Stub: flash object
51
+ let(:flash) do
52
+ flash = Hash.new
53
+ flash.stub(:now).and_return({})
54
+ flash
45
55
  end
46
56
 
47
57
  [:rescue_associate, :rescue_controller].each do |name|
48
- it "should be able to call method `#{name}`" do
49
- expect(object.public_methods.include? name).to be_true
58
+ it "should declare `#{name}` method" do
59
+ expect(controller.public_methods.include? name).to be_true
60
+ end
61
+ end
62
+
63
+ [ :rescue_respond,
64
+ :find_call, :new_call, :create_call, :update_call, :destroy_call].each do |name|
65
+ it "should declare `#{name}` method as private" do
66
+ expect(controller.private_instance_methods.include? name).to be_true
67
+ end
68
+ end
69
+
70
+ [ :new, :edit, :show, :create, :update, :destroy,
71
+ :show_action, :edit_action, :new_action,
72
+ :create_action, :update_action, :destroy_action ].each do |name|
73
+ it "should declare `#{name}` method as public" do
74
+ expect(controller.public_instance_methods.include? name).to be_true
50
75
  end
51
76
  end
52
77
 
78
+ context "when action is executed" do
79
+ let(:object) do
80
+ object = controller.new
81
+ # Stub: flash message
82
+ object.stub(:flash).and_return(flash)
83
+ # Stub: controller/action name for flash messages
84
+ object.stub(:controller_path).and_return('')
85
+ object.stub(:controller_name).and_return('')
86
+ object.stub(:action_name).and_return('')
87
+
88
+ # Stub: parameter methods
89
+ object.stub(:create_params).and_return({})
90
+ object.stub(:update_params).and_return({})
91
+ object.stub(:destroy_params).and_return({})
92
+ object.stub(:execute_params).and_return({})
93
+ object.stub(:customized_params).and_return({})
94
+
95
+ # Stub: call methods
96
+ object.stub(:find_call).with(any_args()).and_raise('find_call execute')
97
+ object.stub(:new_call).and_raise('new_call execute')
98
+ object.stub(:create_call).and_raise('create_call execute')
99
+ object.stub(:update_call).and_raise('update_call execute')
100
+ object.stub(:destroy_call).and_raise('destroy_call execute')
101
+ object.stub(:execute_call).and_raise()
102
+ object
103
+ end
104
+
105
+ # Action Type Relation
106
+ { find: [:show, :edit, :show_action, :edit_action],
107
+ new: [:new, :new_action],
108
+ create: [:create, :create_action],
109
+ update: [:update, :update_action],
110
+ destroy: [:destroy, :destroy_action]
111
+ }.each do |type, names|
112
+ names.each do |name|
113
+ it "should run `#{type}_call` when `#{name}` is executed" do
114
+ expect { object.send(name) }.to raise_error(RuntimeError, "#{type}_call execute")
115
+ end
116
+ end
117
+ end
118
+
119
+ #it "should be performed within 500 microseconds" do
120
+ # n = 10000
121
+ # measure = Benchmark.measure do
122
+ # n.times { object.execute }
123
+ # end
124
+ # expect(measure.total).to be < (500 * 1.0e-06 * n)
125
+ #end
126
+ end
53
127
 
54
128
  describe "#rescue_respond" do
55
129
  let(:object) do
56
130
  clazz = Class.new ApplicationController do
57
131
  include Rescue::Controller
132
+
133
+ # Subjects
134
+ def render_execute
135
+ rescue_respond(:stub_call, {},
136
+ render: lambda { 'render' },
137
+ rescue: lambda { 'rescue' }
138
+ )
139
+ end
140
+
141
+ def rescue_execute
142
+ rescue_respond(:stub_call, {},
143
+ render: lambda { raise RuntimeError },
144
+ rescue: lambda { 'rescue' }
145
+ )
146
+ end
58
147
  end
148
+
59
149
  object = clazz.new
60
- object.stub(:flash).and_return({})
150
+ object.stub(:flash).and_return(flash)
61
151
  object.stub(:controller_path).and_return('rescue')
62
152
  object.stub(:action_name).and_return('action')
63
153
  object.stub(:stub_call).with({}).and_return(nil)
64
154
  object
65
155
  end
66
156
 
67
- it "should be able to render" do
68
- expect(
69
- object.send(:rescue_respond, :stub_call, {},
70
- render: lambda { return 'render' },
71
- rescue: lambda { return 'rescue' })
72
- ).to eq('render')
157
+ it "should render with no error" do
158
+ expect(object.render_execute).to eq('render')
73
159
  end
74
160
 
75
- end
76
-
77
- describe Rescue::Controller::ClassMethods do
78
- describe "#rescue_controller" do
79
-
80
- let(:object) do
81
- Class.new ApplicationController do
82
- extend Rescue::Controller::ClassMethods
83
- rescue_controller RescueModel,
84
- show: [:show_action],
85
- edit: [:edit_action],
86
- new: [:new_action],
87
- create: { render: lambda { } ,rescue: lambda { } },
88
- update: { render: lambda { } ,rescue: lambda { } },
89
- destroy: { render: lambda { } ,rescue: lambda { } },
90
- create_action: { type: :create ,render: lambda { } ,rescue: lambda { } },
91
- update_action: { type: :update ,render: lambda { } ,rescue: lambda { } },
92
- destroy_action: { type: :destroy ,render: lambda { } ,rescue: lambda { } }
93
- end
94
- end
95
-
96
- [:find_call, :new_call, :create_call, :update_call, :destroy_call].each do |name|
97
- it "should be defined private method `#{name}`" do
98
- expect(object.private_instance_methods.include? name).to be_true
99
- end
100
- end
101
-
102
- [ :new, :edit, :show, :create, :update, :destroy,
103
- :show_action, :edit_action, :new_action,
104
- :create_action, :update_action, :destroy_action ].each do |name|
105
- it "should be defined public method `#{name}`" do
106
- expect(object.public_instance_methods.include? name).to be_true
107
- end
108
- end
109
-
161
+ it "should render by the rescue block" do
162
+ expect(object.rescue_execute).to eq('rescue')
110
163
  end
111
164
  end
112
165
 
@@ -17,7 +17,7 @@ describe Rescue::Controller::Action do
17
17
  clazz
18
18
  end
19
19
 
20
- describe "#define" do
20
+ describe "#define_call" do
21
21
  let(:controller) do # Fake Controller
22
22
  clazz = model
23
23
  Class.new do
@@ -27,7 +27,6 @@ describe Rescue::Controller::Action do
27
27
 
28
28
  let(:object) do
29
29
  object = controller.new
30
- object.stub(:rescuemodel_params).and_return({})
31
30
  object.stub(:params).and_return({})
32
31
  object
33
32
  end
@@ -35,15 +34,96 @@ describe Rescue::Controller::Action do
35
34
  let(:params) { {} }
36
35
 
37
36
  [:find_call, :new_call, :create_call, :update_call, :destroy_call].each do |name|
38
- it "should be defined private method `#{name}`" do
37
+ it "should declare `#{name}` method as private" do
39
38
  expect(controller.private_instance_methods.include? name).to be_true
40
39
  end
41
40
  it "should not raise error" do
42
41
  expect { object.send(name, params) }.not_to raise_error
43
42
  end
44
43
  end
45
-
46
44
  end
47
45
 
46
+ describe "#define" do
47
+ let(:controller) do # Fake Controller
48
+ clazz = model
49
+ Class.new do
50
+ Rescue::Controller::Action.define_call self, clazz, :@rescue
51
+ Rescue::Controller::Action.define self,
52
+ [:show, :edit, :new, :create, :update, :destroy],
53
+ show: { render: lambda { 'show' } },
54
+ edit: { render: lambda { 'edit' } },
55
+ new: { render: lambda { 'new' } },
56
+ create: { render: lambda { 'create' } },
57
+ update: { render: lambda { 'update' } },
58
+ destroy: { render: lambda { 'destroy' } }
59
+ end
60
+ end
61
+
62
+ let(:object) do
63
+ object = controller.new
64
+ object.stub(:params).and_return({})
65
+ object.stub(:create_params).and_return({})
66
+ object.stub(:update_params).and_return({})
67
+ object.stub(:destroy_params).and_return({})
68
+
69
+ # Stub: rescue_respond
70
+ object.stub(:rescue_respond) do |call, params, options|
71
+ options[:render].call
72
+ end
73
+ object
74
+ end
75
+
76
+ [:show, :edit, :new, :create, :update, :destroy].each do |name|
77
+ it "should declare `#{name}` method" do
78
+ expect(controller.public_instance_methods.include? name).to be_true
79
+ end
80
+ it "should render" do
81
+ expect(object.send(name)).to eq(name.to_s)
82
+ end
83
+ end
84
+
85
+ context "when unknown types are specified" do
86
+ it do
87
+ expect {
88
+ Class.new do
89
+ Rescue::Controller::Action.define self, [:action],
90
+ action: { type: :undefined ,render: lambda { 'render' } ,rescue: lambda { 'rescue' } }
91
+ end
92
+ }.to raise_error(Rescue::NoActionError, Rescue::NoActionError.new(:undefined).message)
93
+ end
94
+ end
95
+
96
+ context "when an action name is duplicated" do
97
+ it "should not raise error for the first time" do
98
+ expect {
99
+ Class.new do
100
+ Rescue::Controller::Action.define self, [:action],
101
+ action: { type: :show ,render: lambda { 'render' } ,rescue: lambda { 'rescue' } }
102
+ end
103
+ }.not_to raise_error
104
+ end
105
+ it "should raise RuntimeError with \"`action` is already defined.\"" do
106
+ expect {
107
+ Class.new do
108
+ def action ; end
109
+ Rescue::Controller::Action.define self, [:action],
110
+ action: { type: :show ,render: lambda { 'render' } ,rescue: lambda { 'rescue' } }
111
+ end
112
+ }.to raise_error(RuntimeError, "`action` is already defined.")
113
+ end
114
+ end
115
+
116
+ context "when an parameter method is undefined" do
117
+ { create: :create_params,
118
+ update: :update_params,
119
+ destroy: :destroy_params }.each do |name, params|
120
+ it do
121
+ expect {
122
+ controller.new.send(name)
123
+ }.to raise_error(Rescue::NoParameterMethodError, Rescue::NoParameterMethodError.new(controller, params).message)
124
+ end
125
+ end
126
+ end
127
+ end
48
128
  end
49
129
 
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,8 @@ require File.join(File.dirname(__FILE__), 'fake_rails')
7
7
  require File.join(File.dirname(__FILE__), 'test_case')
8
8
  require 'capybara/rails'
9
9
 
10
+ #require 'benchmark'
11
+
10
12
  Coveralls.wear!('rails')
11
13
  RSpec.configure do |config|
12
14
  config.mock_with :rspec
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rescue-dog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - yulii
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-18 00:00:00.000000000 Z
11
+ date: 2013-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.0
27
- description: respond to an exception raised in Rails
27
+ description: Responds status and error handling for Rails
28
28
  email:
29
29
  - yuliinfo@gmail.com
30
30
  executables: []
@@ -52,6 +52,7 @@ files:
52
52
  - lib/rescue/controllers/dynamic.rb
53
53
  - lib/rescue/controllers/flash.rb
54
54
  - lib/rescue/controllers/static.rb
55
+ - lib/rescue/errors.rb
55
56
  - lib/rescue/exceptions/application_error.rb
56
57
  - lib/rescue/exceptions/respond_error.rb
57
58
  - lib/rescue/version.rb
@@ -97,7 +98,7 @@ rubyforge_project:
97
98
  rubygems_version: 2.0.7
98
99
  signing_key:
99
100
  specification_version: 4
100
- summary: define respond methods
101
+ summary: Declare simple CRUD and respond error methods
101
102
  test_files:
102
103
  - spec/fake_rails.rb
103
104
  - spec/rescue/controller_spec.rb