Module: Useful::RubyExtensions::Hash
- Included in:
- Hash
- Defined in:
- lib/useful/ruby_extensions/hash.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#check_value?(*keys) ⇒ Boolean
Determines if a value exists for the provided key(s).
-
#except(*keys) ⇒ Object
Return a new hash with only keys not in *keys.
-
#except!(*keys) ⇒ Object
Destructively remove all keys in *keys.
-
#get_value(*keys) ⇒ Object
Returns the value for the provided key(s).
-
#nillify! ⇒ Object
takes any empty values and makes them nil inline.
-
#only(*keys) ⇒ Object
Return a new hash with only keys in *keys.
-
#only!(*keys) ⇒ Object
Destructively remove all keys not in *keys.
- #to_html_attrs ⇒ Object
- #to_http_query_str(opts = {}) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
8 9 10 |
# File 'lib/useful/ruby_extensions/hash.rb', line 8 def self.included(klass) klass.extend(ClassMethods) if klass.kind_of?(Class) end |
Instance Method Details
#check_value?(*keys) ⇒ Boolean
Determines if a value exists for the provided key(s). Allows searching in nested hashes
55 56 57 58 |
# File 'lib/useful/ruby_extensions/hash.rb', line 55 def check_value?(*keys) val = self.get_value(keys) val && !val.empty? ? true : false end |
#except(*keys) ⇒ Object
Return a new hash with only keys not in *keys
40 41 42 |
# File 'lib/useful/ruby_extensions/hash.rb', line 40 def except(*keys) self.class.except(self.clone, keys) end |
#except!(*keys) ⇒ Object
Destructively remove all keys in *keys
44 45 46 |
# File 'lib/useful/ruby_extensions/hash.rb', line 44 def except!(*keys) self.class.except(self, keys) end |
#get_value(*keys) ⇒ Object
Returns the value for the provided key(s). Allows searching in nested hashes
49 50 51 52 53 |
# File 'lib/useful/ruby_extensions/hash.rb', line 49 def get_value(*keys) val = self[keys.first] || self[keys.first.to_s] val = self[keys.first.to_s.intern] unless val || keys.first.to_s.empty? || keys.first.kind_of?(Symbol) val.kind_of?(Hash) && keys.length > 1 ? val.get_value?(keys[1..-1]) : val end |
#nillify! ⇒ Object
takes any empty values and makes them nil inline
61 62 63 |
# File 'lib/useful/ruby_extensions/hash.rb', line 61 def nillify! self.each { |key,value| self[key] = nil if !value.nil? && value.to_s.empty? } end |
#only(*keys) ⇒ Object
Return a new hash with only keys in *keys
31 32 33 |
# File 'lib/useful/ruby_extensions/hash.rb', line 31 def only(*keys) self.class.only(self.clone, keys) end |
#only!(*keys) ⇒ Object
Destructively remove all keys not in *keys
35 36 37 |
# File 'lib/useful/ruby_extensions/hash.rb', line 35 def only!(*keys) self.class.only(self, keys) end |
#to_html_attrs ⇒ Object
101 102 103 |
# File 'lib/useful/ruby_extensions/hash.rb', line 101 def to_html_attrs self.empty? ? '' : self.collect{|key, val| "#{key}=\"#{val}\""}.join(' ') end |
#to_http_query_str(opts = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/useful/ruby_extensions/hash.rb', line 78 def to_http_query_str(opts = {}) require 'cgi' unless defined?(::CGI) && defined?(::CGI::escape) opts[:prepend] ||= '?' opts[:append] ||= '' opts[:key_ns] ||= nil opt_strings = self.collect do |key, val| key_s = opts[:key_ns] ? "#{opts[:key_ns]}[#{key.to_s}]" : key.to_s if val.kind_of?(::Array) val.collect{|i| "#{key_s}[]=#{::CGI.escape(i.to_s)}"}.join('&') elsif val.kind_of?(::Hash) val.to_http_query_str({ :prepend => '', :key_ns => key_s, :append => '' }) else "#{key_s}=#{::CGI.escape(val.to_s)}" end end self.empty? ? '' : "#{opts[:prepend]}#{opt_strings.join('&')}#{opts[:append]}" end |