Skip to content

Commit 1e4d4a4

Browse files
committed
validation can be turned off
1 parent af1e281 commit 1e4d4a4

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ gem "sqlite3"
77
group :development, :test do
88
gem "rspec-rails"
99
gem "ZenTest"
10+
gem "rcov"
1011
end
1112

1213
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ GEM
7575
rdoc (~> 3.4)
7676
thor (~> 0.14.4)
7777
rake (0.9.2.2)
78+
rcov (1.0.0)
7879
rdoc (3.12)
7980
json (~> 1.4)
8081
rspec (2.8.0)
@@ -112,5 +113,6 @@ DEPENDENCIES
112113
ZenTest
113114
capybara (>= 0.4.0)
114115
rails (= 3.0.11)
116+
rcov
115117
rspec-rails
116118
sqlite3

lib/restapi/dsl_definition.rb

+20-17
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,30 @@ def method_added(method_name)
7474
Restapi.remove_method_description(resource_name, method_name)
7575
mapi = Restapi.define_method_description(resource_name, method_name)
7676

77-
# redefine method
78-
old_method = instance_method(method_name)
79-
80-
define_method(method_name) do |*args|
81-
82-
mapi.params.each do |_, param|
77+
# redefine method only if validation is turned on
78+
if Restapi.configuration.validate == true
8379

84-
# check if required parameters are present
85-
if param.required && !params.has_key?(param.name)
86-
raise ArgumentError.new("Expecting #{param.name} parameter.")
87-
end
80+
old_method = instance_method(method_name)
81+
82+
define_method(method_name) do |*args|
8883

89-
# params validations
90-
if params.has_key?(param.name)
91-
param.validate(params[:"#{param.name}"])
92-
end
84+
mapi.params.each do |_, param|
85+
86+
# check if required parameters are present
87+
if param.required && !params.has_key?(param.name)
88+
raise ArgumentError.new("Expecting #{param.name} parameter.")
89+
end
90+
91+
# params validations
92+
if params.has_key?(param.name)
93+
param.validate(params[:"#{param.name}"])
94+
end
9395

94-
end # params.each
96+
end # params.each
9597

96-
# run the original method code
97-
old_method.bind(self).call(*args)
98+
# run the original method code
99+
old_method.bind(self).call(*args)
100+
end
98101

99102
end
100103

lib/restapi/restapi_module.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ def self.configuration
2626
end
2727

2828
class Configuration
29-
attr_accessor :app_name, :app_info, :copyright, :markup_language
29+
attr_accessor :app_name, :app_info, :copyright, :markup_language, :validate
3030

3131
def initialize
3232
@markup_language = :rdoc
3333
@app_name = "Another API"
3434
@app_info = "Another API description"
3535
@copyright = nil
36+
@validate = true
3637
end
3738
end
3839

spec/dummy/config/initializers/restapi.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
config.app_name = "Test app"
33
config.app_info = "Application for testing purposes."
44
config.copyright = "Pavel Pokorny"
5+
# config.validate = false
56
end

0 commit comments

Comments
 (0)