lib/knj/arrayext.rb in knjrbfw-0.0.7 vs lib/knj/arrayext.rb in knjrbfw-0.0.8
- old
+ new
@@ -22,11 +22,11 @@
value = value[key] if args[:key]
value = value if !args[:key]
if args[:callback]
- if args[:callback].is_a?(Proc)
+ if args[:callback].is_a?(Proc) or args[:callback].is_a?(Method)
value = args[:callback].call(value)
else
value = Knj::Php.call_user_func(args[:callback], value) if args[:callback]
end
end
@@ -92,13 +92,13 @@
return Digest::MD5.hexdigest(combined_val)
end
#Compares the keys and values of two hashes and returns true if they are different.
def self.hash_diff?(h1, h2, args = {})
- if !args.has_key?("h1_to_h2") or args["h1_to_h2"]
+ if !args.key?("h1_to_h2") or args["h1_to_h2"]
h1.each do |key, val|
- return true if !h2.has_key?(key)
+ return true if !h2.key?(key)
hash_val = h2[key].to_s
hash_val = hash_val.force_encoding("UTF-8") if hash_val.respond_to?(:force_encoding)
val = val.to_s
@@ -106,13 +106,13 @@
return true if hash_val != val
end
end
- if !args.has_key?("h2_to_h1") or args["h2_to_h1"]
+ if !args.key?("h2_to_h1") or args["h2_to_h1"]
h2.each do |key, val|
- return true if !h1.has_key?(key)
+ return true if !h1.key?(key)
hash_val = h1[key].to_s
hash_val = hash_val.force_encoding("UTF-8") if hash_val.respond_to?(:force_encoding)
val = val.to_s
@@ -151,7 +151,16 @@
arr.each do |ele|
hashes << Digest::MD5.hexdigest(ele.to_s)
end
return Digest::MD5.hexdigest(hashes.join("_"))
+ end
+
+ #Validates a hash of data.
+ def self.validate_hash(h, args)
+ h.each do |key, val|
+ if args.key?(:not_empty) and args[:not_empty].index(key) != nil and val.to_s.strip.length <= 0
+ raise Knj::Errors::InvalidData, sprintf(args[:not_empty_error], key)
+ end
+ end
end
end
\ No newline at end of file