Class: Hash
Instance Method Summary collapse
- #add_html_class!(html_class) ⇒ Object
-
#environmentize_keys! ⇒ Hash
Destructively and non-recursively convert each key to an uppercase string, deleting nil values along the way.
-
#except(*rejected) ⇒ Hash
Create a hash with all key/value pairs in receiver except
rejected
. -
#normalize_param(key, value) ⇒ String
Convert a key, value pair into a URL query param string.
-
#only(*allowed) ⇒ Hash
Create a hash with only key/value pairs in receiver and
allowed
. -
#protect_keys! ⇒ Array
Converts all keys into string values.
-
#to_params ⇒ String
Convert to URL query param string.
-
#to_xml_attributes ⇒ String
(also: #to_html_attributes)
The hash as attributes for an XML tag.
-
#unprotect_keys! ⇒ Object
Attempts to convert all string keys into Class keys.
Instance Method Details
#add_html_class!(html_class) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/quickbooks/extlib/hash.rb', line 114 def add_html_class!(html_class) if self[:class] self[:class] = "#{self[:class]} #{html_class}" else self[:class] = html_class.to_s end end |
#environmentize_keys! ⇒ Hash
Destructively and non-recursively convert each key to an uppercase string, deleting nil values along the way.
155 156 157 158 159 160 161 162 |
# File 'lib/quickbooks/extlib/hash.rb', line 155 def environmentize_keys! keys.each do |key| val = delete(key) next if val.nil? self[key.to_s.upcase] = val end self end |
#except(*rejected) ⇒ Hash
Create a hash with all key/value pairs in receiver except rejected
{ :one => 1, :two => 2, :three => 3 }.except(:one)
#=> { :two => 2, :three => 3 }
86 87 88 89 90 |
# File 'lib/quickbooks/extlib/hash.rb', line 86 def except(*rejected) hash = self.dup rejected.each {|k| hash.delete(k) } hash end |
#normalize_param(key, value) ⇒ String
Convert a key, value pair into a URL query param string
normalize_param(:name, "Bob") #=> "name=Bob&"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/quickbooks/extlib/hash.rb', line 34 def normalize_param(key, value) param = '' stack = [] if value.is_a?(Array) param << value.map { |element| normalize_param("#{key}[]", element) }.join elsif value.is_a?(Hash) stack << [key,value] else param << "#{key}=#{value}&" end stack.each do |parent, hash| hash.each do |key, value| if value.is_a?(Hash) stack << ["#{parent}[#{key}]", value] else param << normalize_param("#{parent}[#{key}]", value) end end end param end |
#only(*allowed) ⇒ Hash
Create a hash with only key/value pairs in receiver and allowed
{ :one => 1, :two => 2, :three => 3 }.only(:one) #=> { :one => 1 }
69 70 71 72 73 |
# File 'lib/quickbooks/extlib/hash.rb', line 69 def only(*allowed) hash = {} allowed.each {|k| hash[k] = self[k] if self.has_key?(k) } hash end |
#protect_keys! ⇒ Array
Converts all keys into string values. This is used during reloading to prevent problems when classes are no longer declared.
130 131 132 |
# File 'lib/quickbooks/extlib/hash.rb', line 130 def protect_keys! keys.each {|key| self[key.to_s] = delete(key) } end |
#to_params ⇒ String
Convert to URL query param string
{ :name => "Bob",
:address => {
:street => '111 Ruby Ave.',
:city => 'Ruby Central',
:phones => ['111-111-1111', '222-222-2222']
}
}.to_params
#=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
17 18 19 20 21 |
# File 'lib/quickbooks/extlib/hash.rb', line 17 def to_params params = self.map { |k,v| normalize_param(k,v) }.join params.chop! # trailing & params end |
#to_xml_attributes ⇒ String Also known as: to_html_attributes
Returns The hash as attributes for an XML tag.
97 98 99 100 101 |
# File 'lib/quickbooks/extlib/hash.rb', line 97 def to_xml_attributes map do |k,v| %{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"} end.join(' ') end |
#unprotect_keys! ⇒ Object
Attempts to convert all string keys into Class keys. We run this after reloading to convert protected hashes back into usable hashes.
141 142 143 144 145 |
# File 'lib/quickbooks/extlib/hash.rb', line 141 def unprotect_keys! keys.each do |key| (self[Object.full_const_get(key)] = delete(key)) rescue nil end end |