lib/ratatouille/ratifier.rb in ratatouille-1.3.8 vs lib/ratatouille/ratifier.rb in ratatouille-1.4.0

- old
+ new

@@ -11,11 +11,11 @@ def initialize(obj, options={}, &block) @errors = { "/" => [] } @ratifiable_object = obj self.name = options[:name] - @is_a ||= options[:is_a] + parse_options(options) case obj when Hash then extend Ratatouille::HashMethods when Array then extend Ratatouille::ArrayMethods end @@ -55,11 +55,11 @@ @name end # Add validation error. Useful for custom validations. - # @param [String] str + # @param [String] err_in # @param [String] context # @return [void] def validation_error(err_in, context="/") case err_in when String @@ -88,11 +88,11 @@ rescue Exception => e validation_error("#{e.message}", '/') end#cleanup_errors - # @param [Hash] hsh Hash to act upon. + # @param [Hash] item Hash to act upon. # @return [Array] def errors_array(item = @errors) all_errs = [] case item @@ -148,17 +148,25 @@ # Check if ratifiable object is a TrueClass or FalseClass. # Any other class will result in a failed validation. # # @return [Boolean] - def is_boolean(&block) - case @ratifiable_object - when TrueClass, FalseClass + def is_boolean(options={}, &block) + parse_options(options) + + unless @skip == true + unless @unwrap_block == true + case @ratifiable_object + when TrueClass, FalseClass + # OK to enter block + else + validation_error("object is not a boolean") + return + end + end + instance_eval(&block) if block_given? - else - validation_error("object is not a boolean") - return end rescue Exception => e validation_error("#{e.message}", "/") end#is_boolean? @@ -167,59 +175,59 @@ # validation methods defined in various places. # # @param [Hash] options # @option options [Class] :is_a (nil) # @option options [Boolean] :required (false) + # @option options [Boolean] :skip (false) # @option options [Boolean] :unwrap_block (false) # Perform block validation only -- skip method validation logic. def parse_options(options={}) - @is_a = options.fetch(:is_a, nil) - @required = options.fetch(:required, false) - @unwrap_block = options.fetch(:unwrap_block, false) + if Hash === options + @is_a = options.fetch(:is_a, nil) + @required = options.fetch(:required, false) + @skip = options.fetch(:skip, false) + @unwrap_block = options.fetch(:unwrap_block, false) + end end#parse_options # Override Method Missing for a Ratifier to generate errors for invalid # methods called on incorrect objects (hash validation on arrays, etc.) # as well as some catch-all methods for boolean validations (is_* and is_not_*) def method_missing(id, *args, &block) - should_unwrap = false - if args.first.respond_to?(:keys) - if args.first.fetch(:unwrap_block, false) == true - should_unwrap = true - end - end + parse_options(args.first) - case - when should_unwrap == true - # Perform no validation logic - # Skip to block evaluation - when id.to_s =~ /^is_not_(.*)$/ - if @ratifiable_object.respond_to?("#{$1}?") - if @ratifiable_object.send("#{$1}?") == true - validation_error("is #{$1}") - return + unless @skip == true + case + when @unwrap_block == true + # Perform no validation logic + # Skip to block evaluation + when id.to_s =~ /^is_not_(.*)$/ + if @ratifiable_object.respond_to?("#{$1}?") + if @ratifiable_object.send("#{$1}?") == true + validation_error("is #{$1}") + return + end end - end - when id.to_s =~ /^is_(.*)$/ - if @ratifiable_object.respond_to?("#{$1}?") - if @ratifiable_object.send("#{$1}?") == false - validation_error("is not #{$1}") + when id.to_s =~ /^is_(.*)$/ + if @ratifiable_object.respond_to?("#{$1}?") + if @ratifiable_object.send("#{$1}?") == false + validation_error("is not #{$1}") + return + end + end + else + begin + super return + rescue Exception => e + validation_error("#{id} is not supported for the given object (#{@ratifiable_object.class})") + return e end end - else - begin - super - return - rescue Exception => e - validation_error("#{id} is not supported for the given object (#{@ratifiable_object.class})") - return e - end - end - instance_eval(&block) if block_given? - return + instance_eval(&block) if block_given? + end#skip end#method_missing # Properly prepend namespace definition to array of errors #