Skip to content

Commit

Permalink
organizing hash decorations into modules that we will include with di…
Browse files Browse the repository at this point in the history
…scretion
  • Loading branch information
Philip (flip) Kromer committed May 29, 2011
1 parent 53d1802 commit ca4bf7b
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 73 deletions.
31 changes: 0 additions & 31 deletions lib/gorillib/hash/compact.rb

This file was deleted.

42 changes: 0 additions & 42 deletions lib/gorillib/hash/keys.rb

This file was deleted.

31 changes: 31 additions & 0 deletions lib/gorillib/hashlike/compact.rb
@@ -1,3 +1,34 @@
require 'gorillib/object/blank'

class Hash
#
# remove all key-value pairs where the value is nil
#
def compact
reject{|key,val| val.nil? }
end unless method_defined?(:compact)

#
# Replace the hash with its compacted self
#
def compact!
replace(compact)
end unless method_defined?(:compact!)

#
# remove all key-value pairs where the value is blank
#
def compact_blank
reject{|key,val| val.blank? }
end unless method_defined?(:compact_blank)

#
# Replace the hash with its compact_blank'ed self
#
def compact_blank!
replace(compact_blank)
end unless method_defined?(:compact_blank!)
end
#
# # delete all attributes where the value is blank?, and return self. Contrast with compact!
# def compact_blank!
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions lib/gorillib/hashlike/keys.rb
@@ -1,3 +1,45 @@
class Hash
# Return a new hash with all keys converted to strings.
def stringify_keys
dup.stringify_keys!
end unless method_defined?(:stringify_keys)

# Destructively convert all keys to strings.
def stringify_keys!
keys.each do |key|
self[key.to_s] = delete(key)
end
self
end unless method_defined?(:stringify_keys!)

# Return a new hash with all keys converted to symbols, as long as
# they respond to +to_sym+.
def symbolize_keys
dup.symbolize_keys!
end unless method_defined?(:symbolize_keys)

# Destructively convert all keys to symbols, as long as they respond
# to +to_sym+.
def symbolize_keys!
keys.each do |key|
self[(key.to_sym rescue key) || key] = delete(key)
end
self
end unless method_defined?(:symbolize_keys!)

# Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
# Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
# as keys, this will fail.
#
# ==== Examples
# { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
# { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
# { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
def assert_valid_keys(*valid_keys)
unknown_keys = keys - [valid_keys].flatten
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
end unless method_defined?(:assert_valid_keys)
end
# # @return [Hash] the object as a Hash with symbolized keys.
# def symbolize_keys() to_hash ; end
# # @return [Hash] the object as a Hash with string keys.
Expand Down
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
9 changes: 9 additions & 0 deletions notes/fancy_hashes_and_receivers.textile
@@ -1,4 +1,13 @@

* gorillib/hashlike
* gorillib/hashlike/slice
* gorillib/hashlike/deep_dup
* gorillib/hashlike/
* gorillib/hashlike/hashlike_via_accessors
* gorillib/struct/acts_as_hash
* gorillib/struct/iterates_as_hash



h4. acts_as_hash

Expand Down

0 comments on commit ca4bf7b

Please sign in to comment.