lib/apidiesel/dsl.rb in apidiesel-0.8 vs lib/apidiesel/dsl.rb in apidiesel-0.9
- old
+ new
@@ -134,10 +134,31 @@
end
alias_method :time, :datetime
alias_method :date, :datetime
+ # Defines an object parameter
+ #
+ #
+ # @example
+ # expects do
+ # object :contract, klass: Contract
+ # end
+ #
+ # @param (see #string)
+ # @option *args [Class] :klass
+ # @option (see #string)
+ def object(param_name, **args)
+ type_check = ->(value, param_name) {
+ unless value.is_a?(args[:klass])
+ raise Apidiesel::InputError, "arg #{param_name} must be a #{args[:klass].name}"
+ end
+ }
+
+ validation_builder(type_check, param_name, **args)
+ end
+
protected
def validation_builder(duck_typing_check, param_name, *args)
options = args.extract_options!
@@ -154,10 +175,15 @@
options[:optional] = given_params[ options[:required_if_present] ].present? ? false : true
end
unless options.has_key?(:optional) && options[:optional] == true
raise Apidiesel::InputError, "missing arg: #{param_name} - options: #{options.inspect}" unless given_params.has_key?(param_name) && !given_params[param_name].nil?
- raise Apidiesel::InputError, "invalid arg #{param_name}: must respond to #{duck_typing_check}" unless given_params[param_name].respond_to?(duck_typing_check)
+
+ if duck_typing_check.is_a?(Proc)
+ duck_typing_check.call(given_params[param_name], param_name)
+ else
+ raise Apidiesel::InputError, "invalid arg #{param_name}: must respond to #{duck_typing_check}" unless given_params[param_name].respond_to?(duck_typing_check)
+ end
end
if options.has_key?(:allowed_values) && !given_params[param_name].blank?
unless options[:allowed_values].include?(given_params[param_name])
raise Apidiesel::InputError, "value '#{given_params[param_name]}' is not a valid value for #{param_name}"
\ No newline at end of file