Class: Hash

Inherits:
Object show all
Defined in:
lib/quickbooks/extlib/hash.rb

Instance Method Summary collapse

Instance Method Details

#add_html_class!(html_class) ⇒ Object

Examples:

hash #=> nil

hash.add_html_class!(:selected)

hash #=> “selected”

hash.add_html_class!(“class1 class2”)

hash #=> “selected class1 class2”

Parameters:

  • html_class (#to_s)

    The HTML class to add to the :class key. The html_class will be concatenated to any existing classes.



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.

Examples:

{ :name => "Bob", :contact => { :email => "[email protected]" } }.environmentize_keys!
  #=> { "NAME" => "Bob", "CONTACT" => { :email => "[email protected]" } }

Returns:

  • (Hash)

    The newly environmentized hash.



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 }

Parameters:

  • *rejected (Array[String, Symbol])

    The hash keys to exclude.

Returns:

  • (Hash)

    A new hash without the selected keys.



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&"

Parameters:

  • key (Object)

    The key for the param.

  • value (Object)

    The value for the param.

Returns:

  • (String)

    This key value pair as a param



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 }

Parameters:

  • *allowed (Array[String, Symbol])

    The hash keys to include.

Returns:

  • (Hash)

    A new hash with only the selected keys.



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.

Examples:

hash = { One => 1, Two => 2 }.proctect_keys!
hash # => { "One" => 1, "Two" => 2 }

Returns:

  • (Array)

    An array of they hash’s keys



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_paramsString

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."

Returns:

  • (String)

    This hash as a query string



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_attributesString Also known as: to_html_attributes

Returns The hash as attributes for an XML tag.

Examples:

{ :one => 1, "two"=>"TWO" }.to_xml_attributes
  #=> 'one="1" two="TWO"'

Returns:

  • (String)

    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.

Examples:

# Provided that classes One and Two are declared in this scope:
hash = { "One" => 1, "Two" => 2 }.unproctect_keys!
hash # => { One => 1, Two => 2 }


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