Module: Useful::RubyExtensions::Hash::InstanceMethods

Defined in:
lib/useful/ruby_extensions/hash.rb

Instance Method Summary collapse

Instance Method Details

#check_value?(*keys_array) ⇒ Boolean

Determines if a value exists for the provided key(s). Allows searching in nested hashes

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/useful/ruby_extensions/hash.rb', line 78

def check_value?(*keys_array)
  val = self.get_value(keys_array)
  val && !val.empty? ? true : false
end

#except(*keys) ⇒ Object



50
51
52
# File 'lib/useful/ruby_extensions/hash.rb', line 50

def except(*keys)
  self.class.except(self.dup, keys)
end

#except!(*keys) ⇒ Object



54
55
56
# File 'lib/useful/ruby_extensions/hash.rb', line 54

def except!(*keys)
  self.class.except(self, keys)
end

#get_value(*keys_array) ⇒ Object Also known as: search

Returns the value for the provided key(s). Allows searching in nested hashes



66
67
68
69
70
71
72
73
74
# File 'lib/useful/ruby_extensions/hash.rb', line 66

def get_value(*keys_array)
  keys = keys_array.flatten
  if !keys.respond_to?('empty?') || keys.empty?
    nil
  else
    val = self[keys.shift]
    val.respond_to?('get_value') && !keys.empty? ? val.get_value(keys) : val
  end
end

#nillifyObject



58
59
60
# File 'lib/useful/ruby_extensions/hash.rb', line 58

def nillify
  self.class.nillify(self.dup)
end

#nillify!Object



61
62
63
# File 'lib/useful/ruby_extensions/hash.rb', line 61

def nillify!
  self.class.nillify(self)
end

#only(*keys) ⇒ Object



43
44
45
# File 'lib/useful/ruby_extensions/hash.rb', line 43

def only(*keys)
  self.class.only(self.dup, keys)
end

#only!(*keys) ⇒ Object



46
47
48
# File 'lib/useful/ruby_extensions/hash.rb', line 46

def only!(*keys)
  self.class.only(self, keys)
end

#to_html_attrsObject



118
119
120
# File 'lib/useful/ruby_extensions/hash.rb', line 118

def to_html_attrs
  self.empty? ? '' : self.sort{|a,b| a[0].to_s <=> b[0].to_s}.collect{|k_v| "#{k_v[0]}=\"#{k_v[1]}\""}.join(' ')
end

#to_http_query_str(opts = {}) ⇒ Object

Returns string formatted for HTTP URL encoded name-value pairs. For example,

{:id => 'thomas_hardy'}.to_http_query_str 
# => "?id=thomas_hardy"
{:id => 23423, :since => Time.now}.to_http_query_str
# => "?since=Thu,%2021%20Jun%202007%2012:10:05%20-0500&id=23423"
{:id => [1,2]}.to_http_query_str
# => "?id[]=1&id[]=2"
{:poo => {:foo => 1, :bar => 2}}.to_http_query_str
# => "?poo[bar]=2&poo[foo]=1"
{:poo => {:foo => 1, :bar => {:bar1 => 1, :bar2 => "nasty"}}}.to_http_query_str
"?poo[bar][bar1]=1&poo[bar][bar2]=nasty&poo[foo]=1"


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/useful/ruby_extensions/hash.rb', line 95

def to_http_query_str(opts = {})
  opts[:prepend] ||= '?'
  opts[:append] ||= ''
  opts[:key_ns] ||= nil
  opt_strings = self.sort{|a,b| a[0].to_s <=> b[0].to_s}.collect do |key_val|
    key = key_val[0]
    val = key_val[1]
    key_s = opts[:key_ns] ? "#{opts[:key_ns]}[#{key.to_s}]" : key.to_s
    if val.kind_of?(::Array)
      val.sort.collect{|i| "#{key_s}[]=#{i.to_s.cgi_escape}"}.join('&')
    elsif val.respond_to?('to_http_query_str')
      val.to_http_query_str({
        :prepend => '',
        :key_ns => key_s,
        :append => ''
      })
    else
      "#{key_s}=#{val.to_s.cgi_escape}"
    end
  end 
  self.empty? ? '' : "#{opts[:prepend]}#{opt_strings.join('&')}#{opts[:append]}"
end