weak_parameters 0.0.1 → 0.0.2

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: 31ad205c14c4aa3637527ae17246de22941fdad6
4
- data.tar.gz: 8f3fc4e4a94706371fbb446f18fecc4a7bee540a
3
+ metadata.gz: 487fdb7064411169290876e998a0711514aa381f
4
+ data.tar.gz: 1ba63113f459198581df79d0f90437a9298945f0
5
5
  SHA512:
6
- metadata.gz: be122aa99d692c94c16b6b43ec111b8689ae2f277216491ba4018436913e62e7070356ad68092be9e8a86cc1ded7f894124e6680965b1e2bb07b3461e32e64a2
7
- data.tar.gz: 4d6c0afcf6111f8d2c28049735069009234bbb2b52155d4f064f93145b3603894ecf9019eafb61be71a6b5055a767c13a6cae0392d15fb9443c91b7f4104bff9
6
+ metadata.gz: 1b91ff714bddccc4b7e4f631c4497e28432358e81d0d5ad47c2b2f39fa293590f161523d38c771ee4477318db01b7be0b1e9dabcddaa588134530d086b0bde0a
7
+ data.tar.gz: fc851acd2965b9814cb8bf7b6916ce57906ce0b1afedfd64f8de08a05d86742e96ee3de0ef5d03ec3e3496305c67d8ce5e4c5105ebf3a61aeddac89f2ccd474f
data/.gitignore CHANGED
@@ -7,7 +7,6 @@ Gemfile.lock
7
7
  InstalledFiles
8
8
  _yardoc
9
9
  coverage
10
- doc/
11
10
  lib/bundler/man
12
11
  log/*.log
13
12
  pkg
data/Gemfile CHANGED
@@ -3,6 +3,7 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  group :test do
6
+ gem "autodoc"
6
7
  gem "pry-rails"
7
8
  gem "rspec-rails"
8
9
  end
data/README.md CHANGED
@@ -35,12 +35,7 @@ irb(main):005:0> app.post "/recipes", name: "alice", type: "bob"
35
35
  ```
36
36
 
37
37
  ## Tips
38
- WeakParameters.stats returns its definition of validations.
39
- This is useful for auto-generating API documents.
38
+ WeakParameters.stats returns its validation metadata, and this is useful for auto-generating API documents.
39
+ With [autodoc](https://github.com/r7kamura/autodoc), you can auto-generate API documents with params information.
40
40
 
41
- ```ruby
42
- WeakParameters::stats[:recipes][:create].validators[0].key #=> :name
43
- WeakParameters::stats[:recipes][:create].validators[0].required? #=> true
44
- WeakParameters::stats[:recipes][:create].validators[1].key #=> :type
45
- WeakParameters::stats[:recipes][:create].validators[1].required? #=> false
46
- ```
41
+ https://github.com/r7kamura/autodoc
@@ -0,0 +1,13 @@
1
+ module WeakParameters
2
+ class ArrayValidator < WeakParameters::BaseValidator
3
+ private
4
+
5
+ def valid_type?
6
+ value.is_a?(Array)
7
+ end
8
+
9
+ def error_message
10
+ "params[#{key.inspect}] must be an Array"
11
+ end
12
+ end
13
+ end
@@ -9,7 +9,7 @@ module WeakParameters
9
9
  end
10
10
 
11
11
  def validate
12
- raise_required_error if required? && nil?
12
+ raise_error if required? && nil? || exist? && invalid_type?
13
13
  end
14
14
 
15
15
  def required?
@@ -26,12 +26,28 @@ module WeakParameters
26
26
  value.nil?
27
27
  end
28
28
 
29
+ def exist?
30
+ !nil?
31
+ end
32
+
29
33
  def value
30
34
  params[key]
31
35
  end
32
36
 
33
- def raise_required_error
34
- raise WeakParameters::ValidationError, "params[#{key.inspect}] is required"
37
+ def raise_error
38
+ raise WeakParameters::ValidationError, error_message
39
+ end
40
+
41
+ def error_message
42
+ "params[#{key.inspect}] is required"
43
+ end
44
+
45
+ def valid_type?
46
+ true
47
+ end
48
+
49
+ def invalid_type?
50
+ !valid_type?
35
51
  end
36
52
  end
37
53
  end
@@ -0,0 +1,17 @@
1
+ module WeakParameters
2
+ class BooleanValidator < WeakParameters::BaseValidator
3
+ def type
4
+ :boolean
5
+ end
6
+
7
+ private
8
+
9
+ def valid_type?
10
+ %w[0 1 false true].include?(value)
11
+ end
12
+
13
+ def error_message
14
+ "params[#{key.inspect}] must be 0, 1, false or true"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module WeakParameters
2
+ class HashValidator < WeakParameters::BaseValidator
3
+ private
4
+
5
+ def valid_type?
6
+ value.is_a?(Hash)
7
+ end
8
+
9
+ def error_message
10
+ "params[#{key.inspect}] must be an Hash"
11
+ end
12
+ end
13
+ end
@@ -1,22 +1,11 @@
1
1
  module WeakParameters
2
2
  class IntegerValidator < WeakParameters::BaseValidator
3
- def validate
4
- super
5
- raise_integer_error if !nil? && !integer?
6
- end
7
-
8
- def type
9
- :integer
10
- end
11
-
12
- private
13
-
14
- def integer?
3
+ def valid_type?
15
4
  /\A-?\d+\z/ === value
16
5
  end
17
6
 
18
- def raise_integer_error
19
- raise WeakParameters::ValidationError, "params[#{key.inspect}] must be an Integer"
7
+ def error_message
8
+ "params[#{key.inspect}] must be an Integer"
20
9
  end
21
10
  end
22
11
  end
@@ -0,0 +1,4 @@
1
+ module WeakParameters
2
+ class StringValidator < WeakParameters::BaseValidator
3
+ end
4
+ end
@@ -15,6 +15,10 @@ module WeakParameters
15
15
  @validators ||= []
16
16
  end
17
17
 
18
+ def type
19
+ self.class.name.split("::").last.sub(/Validator$/, "").underscore.to_sym
20
+ end
21
+
18
22
  private
19
23
 
20
24
  def string(key, options = {})
@@ -24,5 +28,17 @@ module WeakParameters
24
28
  def integer(key, options = {})
25
29
  validators << WeakParameters::IntegerValidator.new(params, key, options)
26
30
  end
31
+
32
+ def boolean(key, options = {})
33
+ validators << WeakParameters::BooleanValidator.new(params, key, options)
34
+ end
35
+
36
+ def hash(key, options = {})
37
+ validators << WeakParameters::HashValidator.new(params, key, options)
38
+ end
39
+
40
+ def array(key, options = {})
41
+ validators << WeakParameters::ArrayValidator.new(params, key, options)
42
+ end
27
43
  end
28
44
  end
@@ -1,3 +1,3 @@
1
1
  module WeakParameters
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,8 +2,12 @@ require "action_controller"
2
2
  require "active_support/hash_with_indifferent_access"
3
3
 
4
4
  require "weak_parameters/base_validator"
5
- require "weak_parameters/controller"
5
+ require "weak_parameters/array_validator"
6
+ require "weak_parameters/boolean_validator"
7
+ require "weak_parameters/hash_validator"
6
8
  require "weak_parameters/integer_validator"
9
+ require "weak_parameters/string_validator"
10
+ require "weak_parameters/controller"
7
11
  require "weak_parameters/validation_error"
8
12
  require "weak_parameters/validator"
9
13
  require "weak_parameters/version"
@@ -2,13 +2,12 @@ class RecipesController < ApplicationController
2
2
  validates :create do
3
3
  string :name, required: true
4
4
  integer :type
5
- end
6
-
7
- def show
8
- respond_with Recipe.find(params[:id])
5
+ boolean :flag
6
+ hash :config
7
+ array :tags
9
8
  end
10
9
 
11
10
  def create
12
- respond_with Recipe.create(params.slice(:name, :type))
11
+ head 201
13
12
  end
14
13
  end
@@ -1,3 +1,3 @@
1
1
  Dummy::Application.routes.draw do
2
- resources :recipes, only: [:show, :create]
2
+ resources :recipes, only: :create
3
3
  end
@@ -1,14 +1,13 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "Recipes" do
4
- let(:env) do
5
- { "HTTP_ACCEPT" => "application/json" }
6
- end
7
-
8
4
  let(:params) do
9
5
  {
10
6
  name: "name",
11
7
  type: 1,
8
+ flag: true,
9
+ config: {},
10
+ tags: [],
12
11
  }
13
12
  end
14
13
 
@@ -19,18 +18,51 @@ describe "Recipes" do
19
18
  end
20
19
 
21
20
  it "returns 400" do
22
- post "/recipes", params, env
21
+ post "/recipes", params
23
22
  response.status.should == 400
24
23
  end
25
24
  end
26
25
 
27
- context "with other typed param" do
26
+ context "with wrong integer param" do
28
27
  before do
29
28
  params[:type] = "x"
30
29
  end
31
30
 
32
31
  it "returns 400" do
33
- post "/recipes", params, env
32
+ post "/recipes", params
33
+ response.status.should == 400
34
+ end
35
+ end
36
+
37
+ context "with wrong boolean param" do
38
+ before do
39
+ params[:flag] = "x"
40
+ end
41
+
42
+ it "returns 400" do
43
+ post "/recipes", params
44
+ response.status.should == 400
45
+ end
46
+ end
47
+
48
+ context "with wrong array param" do
49
+ before do
50
+ params[:tags] = "x"
51
+ end
52
+
53
+ it "returns 400" do
54
+ post "/recipes", params
55
+ response.status.should == 400
56
+ end
57
+ end
58
+
59
+ context "with wrong hash param" do
60
+ before do
61
+ params[:config] = "x"
62
+ end
63
+
64
+ it "returns 400" do
65
+ post "/recipes", params
34
66
  response.status.should == 400
35
67
  end
36
68
  end
@@ -41,14 +73,14 @@ describe "Recipes" do
41
73
  end
42
74
 
43
75
  it "creates a new recipe" do
44
- post "/recipes", params, env
76
+ post "/recipes", params
45
77
  response.status.should == 201
46
78
  end
47
79
  end
48
80
 
49
- context "with valid condition" do
81
+ context "with valid condition", :autodoc do
50
82
  it "creates a new recipe" do
51
- post "/recipes", params, env
83
+ post "/recipes", params
52
84
  response.status.should == 201
53
85
  end
54
86
  end
data/spec/spec_helper.rb CHANGED
@@ -13,4 +13,6 @@ RSpec.configure do |config|
13
13
  # automatically. This will be the default behavior in future versions of
14
14
  # rspec-rails.
15
15
  config.infer_base_class_for_anonymous_controllers = false
16
+
17
+ config.treat_symbols_as_metadata_keys_with_true_values = true
16
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weak_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-07 00:00:00.000000000 Z
11
+ date: 2013-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -79,13 +79,16 @@ files:
79
79
  - README.md
80
80
  - Rakefile
81
81
  - lib/weak_parameters.rb
82
+ - lib/weak_parameters/array_validator.rb
82
83
  - lib/weak_parameters/base_validator.rb
84
+ - lib/weak_parameters/boolean_validator.rb
83
85
  - lib/weak_parameters/controller.rb
86
+ - lib/weak_parameters/hash_validator.rb
84
87
  - lib/weak_parameters/integer_validator.rb
88
+ - lib/weak_parameters/string_validator.rb
85
89
  - lib/weak_parameters/validation_error.rb
86
90
  - lib/weak_parameters/validator.rb
87
91
  - lib/weak_parameters/version.rb
88
- - spec/dummy/README.rdoc
89
92
  - spec/dummy/Rakefile
90
93
  - spec/dummy/app/assets/javascripts/application.js
91
94
  - spec/dummy/app/assets/stylesheets/application.css
@@ -144,12 +147,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
147
  version: '0'
145
148
  requirements: []
146
149
  rubyforge_project:
147
- rubygems_version: 2.0.2
150
+ rubygems_version: 2.0.0
148
151
  signing_key:
149
152
  specification_version: 4
150
153
  summary: Add a validation filter to your controller.
151
154
  test_files:
152
- - spec/dummy/README.rdoc
153
155
  - spec/dummy/Rakefile
154
156
  - spec/dummy/app/assets/javascripts/application.js
155
157
  - spec/dummy/app/assets/stylesheets/application.css
@@ -187,3 +189,4 @@ test_files:
187
189
  - spec/dummy/script/rails
188
190
  - spec/requests/recipes_spec.rb
189
191
  - spec/spec_helper.rb
192
+ has_rdoc:
@@ -1,261 +0,0 @@
1
- == Welcome to Rails
2
-
3
- Rails is a web-application framework that includes everything needed to create
4
- database-backed web applications according to the Model-View-Control pattern.
5
-
6
- This pattern splits the view (also called the presentation) into "dumb"
7
- templates that are primarily responsible for inserting pre-built data in between
8
- HTML tags. The model contains the "smart" domain objects (such as Account,
9
- Product, Person, Post) that holds all the business logic and knows how to
10
- persist themselves to a database. The controller handles the incoming requests
11
- (such as Save New Account, Update Product, Show Post) by manipulating the model
12
- and directing data to the view.
13
-
14
- In Rails, the model is handled by what's called an object-relational mapping
15
- layer entitled Active Record. This layer allows you to present the data from
16
- database rows as objects and embellish these data objects with business logic
17
- methods. You can read more about Active Record in
18
- link:files/vendor/rails/activerecord/README.html.
19
-
20
- The controller and view are handled by the Action Pack, which handles both
21
- layers by its two parts: Action View and Action Controller. These two layers
22
- are bundled in a single package due to their heavy interdependence. This is
23
- unlike the relationship between the Active Record and Action Pack that is much
24
- more separate. Each of these packages can be used independently outside of
25
- Rails. You can read more about Action Pack in
26
- link:files/vendor/rails/actionpack/README.html.
27
-
28
-
29
- == Getting Started
30
-
31
- 1. At the command prompt, create a new Rails application:
32
- <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
-
34
- 2. Change directory to <tt>myapp</tt> and start the web server:
35
- <tt>cd myapp; rails server</tt> (run with --help for options)
36
-
37
- 3. Go to http://localhost:3000/ and you'll see:
38
- "Welcome aboard: You're riding Ruby on Rails!"
39
-
40
- 4. Follow the guidelines to start developing your application. You can find
41
- the following resources handy:
42
-
43
- * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
- * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
-
46
-
47
- == Debugging Rails
48
-
49
- Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
- will help you debug it and get it back on the rails.
51
-
52
- First area to check is the application log files. Have "tail -f" commands
53
- running on the server.log and development.log. Rails will automatically display
54
- debugging and runtime information to these files. Debugging info will also be
55
- shown in the browser on requests from 127.0.0.1.
56
-
57
- You can also log your own messages directly into the log file from your code
58
- using the Ruby logger class from inside your controllers. Example:
59
-
60
- class WeblogController < ActionController::Base
61
- def destroy
62
- @weblog = Weblog.find(params[:id])
63
- @weblog.destroy
64
- logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
- end
66
- end
67
-
68
- The result will be a message in your log file along the lines of:
69
-
70
- Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
-
72
- More information on how to use the logger is at http://www.ruby-doc.org/core/
73
-
74
- Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
- several books available online as well:
76
-
77
- * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
- * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
-
80
- These two books will bring you up to speed on the Ruby language and also on
81
- programming in general.
82
-
83
-
84
- == Debugger
85
-
86
- Debugger support is available through the debugger command when you start your
87
- Mongrel or WEBrick server with --debugger. This means that you can break out of
88
- execution at any point in the code, investigate and change the model, and then,
89
- resume execution! You need to install ruby-debug to run the server in debugging
90
- mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
-
92
- class WeblogController < ActionController::Base
93
- def index
94
- @posts = Post.all
95
- debugger
96
- end
97
- end
98
-
99
- So the controller will accept the action, run the first line, then present you
100
- with a IRB prompt in the server window. Here you can do things like:
101
-
102
- >> @posts.inspect
103
- => "[#<Post:0x14a6be8
104
- @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
- #<Post:0x14a6620
106
- @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
- >> @posts.first.title = "hello from a debugger"
108
- => "hello from a debugger"
109
-
110
- ...and even better, you can examine how your runtime objects actually work:
111
-
112
- >> f = @posts.first
113
- => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
- >> f.
115
- Display all 152 possibilities? (y or n)
116
-
117
- Finally, when you're ready to resume execution, you can enter "cont".
118
-
119
-
120
- == Console
121
-
122
- The console is a Ruby shell, which allows you to interact with your
123
- application's domain model. Here you'll have all parts of the application
124
- configured, just like it is when the application is running. You can inspect
125
- domain models, change values, and save to the database. Starting the script
126
- without arguments will launch it in the development environment.
127
-
128
- To start the console, run <tt>rails console</tt> from the application
129
- directory.
130
-
131
- Options:
132
-
133
- * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
- made to the database.
135
- * Passing an environment name as an argument will load the corresponding
136
- environment. Example: <tt>rails console production</tt>.
137
-
138
- To reload your controllers and models after launching the console run
139
- <tt>reload!</tt>
140
-
141
- More information about irb can be found at:
142
- link:http://www.rubycentral.org/pickaxe/irb.html
143
-
144
-
145
- == dbconsole
146
-
147
- You can go to the command line of your database directly through <tt>rails
148
- dbconsole</tt>. You would be connected to the database with the credentials
149
- defined in database.yml. Starting the script without arguments will connect you
150
- to the development database. Passing an argument will connect you to a different
151
- database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
- PostgreSQL and SQLite 3.
153
-
154
- == Description of Contents
155
-
156
- The default directory structure of a generated Ruby on Rails application:
157
-
158
- |-- app
159
- | |-- assets
160
- | | |-- images
161
- | | |-- javascripts
162
- | | `-- stylesheets
163
- | |-- controllers
164
- | |-- helpers
165
- | |-- mailers
166
- | |-- models
167
- | `-- views
168
- | `-- layouts
169
- |-- config
170
- | |-- environments
171
- | |-- initializers
172
- | `-- locales
173
- |-- db
174
- |-- doc
175
- |-- lib
176
- | |-- assets
177
- | `-- tasks
178
- |-- log
179
- |-- public
180
- |-- script
181
- |-- test
182
- | |-- fixtures
183
- | |-- functional
184
- | |-- integration
185
- | |-- performance
186
- | `-- unit
187
- |-- tmp
188
- | `-- cache
189
- | `-- assets
190
- `-- vendor
191
- |-- assets
192
- | |-- javascripts
193
- | `-- stylesheets
194
- `-- plugins
195
-
196
- app
197
- Holds all the code that's specific to this particular application.
198
-
199
- app/assets
200
- Contains subdirectories for images, stylesheets, and JavaScript files.
201
-
202
- app/controllers
203
- Holds controllers that should be named like weblogs_controller.rb for
204
- automated URL mapping. All controllers should descend from
205
- ApplicationController which itself descends from ActionController::Base.
206
-
207
- app/models
208
- Holds models that should be named like post.rb. Models descend from
209
- ActiveRecord::Base by default.
210
-
211
- app/views
212
- Holds the template files for the view that should be named like
213
- weblogs/index.html.erb for the WeblogsController#index action. All views use
214
- eRuby syntax by default.
215
-
216
- app/views/layouts
217
- Holds the template files for layouts to be used with views. This models the
218
- common header/footer method of wrapping views. In your views, define a layout
219
- using the <tt>layout :default</tt> and create a file named default.html.erb.
220
- Inside default.html.erb, call <% yield %> to render the view using this
221
- layout.
222
-
223
- app/helpers
224
- Holds view helpers that should be named like weblogs_helper.rb. These are
225
- generated for you automatically when using generators for controllers.
226
- Helpers can be used to wrap functionality for your views into methods.
227
-
228
- config
229
- Configuration files for the Rails environment, the routing map, the database,
230
- and other dependencies.
231
-
232
- db
233
- Contains the database schema in schema.rb. db/migrate contains all the
234
- sequence of Migrations for your schema.
235
-
236
- doc
237
- This directory is where your application documentation will be stored when
238
- generated using <tt>rake doc:app</tt>
239
-
240
- lib
241
- Application specific libraries. Basically, any kind of custom code that
242
- doesn't belong under controllers, models, or helpers. This directory is in
243
- the load path.
244
-
245
- public
246
- The directory available for the web server. Also contains the dispatchers and the
247
- default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
- server.
249
-
250
- script
251
- Helper scripts for automation and generation.
252
-
253
- test
254
- Unit and functional tests along with fixtures. When using the rails generate
255
- command, template test files will be generated for you and placed in this
256
- directory.
257
-
258
- vendor
259
- External libraries that the application depends on. Also includes the plugins
260
- subdirectory. If the app has frozen rails, those gems also go here, under
261
- vendor/rails/. This directory is in the load path.