Skip to content

Commit

Permalink
bump to 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
CITguy committed Apr 14, 2012
1 parent 685e379 commit ed3d668
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/ratatouille.rb
Expand Up @@ -10,9 +10,10 @@
module Ratatouille

# @param [Hash, Array] obj Object to validate
# @param [Hash] options
# @return [Validatable::Ratifier]
def ratify(obj, &block)
Ratatouille::Ratifier.new(obj, &block)
def ratify(obj, options={}, &block)
Ratatouille::Ratifier.new(obj, options, &block)
end#ratify

end
14 changes: 10 additions & 4 deletions lib/ratatouille/array.rb
Expand Up @@ -5,22 +5,26 @@ module ArrayMethods
# @return [void]
def is_empty(&block)
unless @ratifiable_object.empty?
validation_error("Array is not empty")
validation_error("not empty")
return
end

instance_eval(&block) if block_given?
rescue Exception => e
validation_error("#{e.message}")
end#is_empty


# @return [void]
def is_not_empty(&block)
if @ratifiable_object.empty?
validation_error("Array is empty")
validation_error("empty")
return
end

instance_eval(&block) if block_given?
rescue Exception => e
validation_error("#{e.message}")
end#is_not_empty


Expand Down Expand Up @@ -78,6 +82,8 @@ def length_between(min_size=0, max_size=nil, &block)
end

instance_eval(&block) if block_given?
rescue Exception => e
validation_error("#{e.message}")
end#length_between
private

Expand All @@ -89,7 +95,7 @@ def valid_min_length?(min_size)
end

unless @ratifiable_object.size >= min_size.to_i
validation_error("Array length must be #{min_size} or more")
validation_error("length must be #{min_size} or more")
return false
end
return true
Expand All @@ -106,7 +112,7 @@ def valid_max_length?(max_size)
end

if @ratifiable_object.size > max_size.to_i
validation_error("Array length must be less than #{max_size.to_i}")
validation_error("length must be less than #{max_size.to_i}")
return false
end

Expand Down
25 changes: 19 additions & 6 deletions lib/ratatouille/hash.rb
Expand Up @@ -5,33 +5,42 @@ module HashMethods
# Runs validation in block against object for the given key.
#
# @param [String, Symbol] key
def given_key(key, &block)
# @param [Hash] options
def given_key(key, options={}, &block)
options[:name] = options.fetch(:name, (Symbol === key ? ":#{key}" : key.to_s) )

if @ratifiable_object.has_key?(key) && block_given?
child_object = Ratatouille::Ratifier.new(@ratifiable_object[key], &block)
child_object = Ratatouille::Ratifier.new(@ratifiable_object[key], options, &block)
@errors[key] = child_object.errors unless child_object.valid?
end
rescue Exception => e
validation_error("#{e.message}")
end#given_key


# @return [void]
def is_empty(&block)
unless @ratifiable_object.empty?
validation_error("Hash is not empty")
validation_error("not empty")
return
end

instance_eval(&block) if block_given?
rescue Exception => e
validation_error("#{e.message}")
end#is_empty


# @return [void]
def is_not_empty(&block)
if @ratifiable_object.empty?
validation_error("Hash is empty")
validation_error("empty")
return
end

instance_eval(&block) if block_given?
rescue Exception => e
validation_error("#{e.message}")
end#is_not_empty


Expand All @@ -44,12 +53,12 @@ def required_keys(*req_keys, &block)
common_keys = (@ratifiable_object.keys & req_keys)

if @ratifiable_object.empty?
validation_error("Cannot find required keys in empty hash.")
validation_error("Cannot find required keys")
return
end

if req_keys.nil? || req_keys.empty?
validation_error("No required keys given to compare Hash against.")
validation_error("No required keys given to compare against.")
return
end

Expand All @@ -66,6 +75,8 @@ def required_keys(*req_keys, &block)
end

instance_eval(&block) if block_given?
rescue Exception => e
validation_error("#{e.message}")
end#required_keys


Expand Down Expand Up @@ -123,6 +134,8 @@ def choice_of(choice_size=1, *key_list, &block)
end

instance_eval(&block) if block_given?
rescue Exception => e
validation_error("#{e.message}")
end#choice_of
end#HashMethods

Expand Down
29 changes: 26 additions & 3 deletions lib/ratatouille/ratifier.rb
Expand Up @@ -7,9 +7,11 @@ class Ratifier

# A new instance of Ratifier
# @param [Hash, Array] obj Object to validate
# @param [Hash] options
def initialize(obj, options={}, &block)
@errors = { "/" => [] }
@ratifiable_object = obj
self.name = options[:name]

case obj
when Hash then extend Ratatouille::HashMethods
Expand All @@ -24,6 +26,27 @@ def initialize(obj, options={}, &block)
end#initialize


# Name of instance
#
# @return [String]
def name
@name ||= @ratifiable_object.class.to_s
end


# Set name of instance
#
# @param [String] namein
# @return [String] name of Ratatouille::Ratifier instance
def name=(namein)
case namein
when String
@name = namein unless namein.blank?
end
@name
end


# Add validation error. Useful for custom validations.
# @param [String] str
# @param [String] context
Expand All @@ -33,10 +56,10 @@ def validation_error(err_in, context="/")
when String
return if err_in.blank?
@errors[context] = [] unless @errors[context]
@errors[context] << err_in
@errors[context] << "#{@name}: #{err_in}"
end
rescue Exception => e
@errors["/"] << e.message
@errors["/"] << "#{@name}: #{e.message}"
end#validation_error


Expand All @@ -54,7 +77,7 @@ def valid?
def cleanup_errors
@errors = {} if errors_array.empty?
rescue Exception => e
@errors["/"] << e.message
validation_error("#{e.message}", '/')
end#cleanup_errors


Expand Down
2 changes: 1 addition & 1 deletion lib/ratatouille/version.rb
@@ -1,4 +1,4 @@
module Ratatouille
# Gem Version
VERSION = "1.1.1"
VERSION = "1.2.0"
end

0 comments on commit ed3d668

Please sign in to comment.