Parent

Hash

Public Instance Methods

deebeefy_values() click to toggle source

Converts values of a Hash in such a way that they can be easily stored in the database: hashes and arrays are jsonified, symbols - stringified

# File lib/netzke/core_ext/hash.rb, line 32
def deebeefy_values
  inject({}) do |options, (k, v)|
    options[k] = v.is_a?(Symbol) ? v.to_s : (v.is_a?(Hash) || v.is_a?(Array)) ? v.to_json : v
    options
  end
end
deep_convert_keys(█) click to toggle source

Recursively convert the keys. Example: {:bla_bla => 1, “wow_now” => {:look_ma => true}}.deep_convert_keys{|k| k.to_s.camelize.to_sym}

=> {:BlaBla => 1, "WowNow" => {:LookMa => true}}
# File lib/netzke/core_ext/hash.rb, line 11
def deep_convert_keys(&block)
  block_given? ? self.inject({}) do |h,(k,v)|
    h[yield(k)] = v.respond_to?('deep_convert_keys') ? v.deep_convert_keys(&block) : v
    h
  end : self
end
deep_each_pair(█) click to toggle source
# File lib/netzke/core_ext/hash.rb, line 2
def deep_each_pair(&block)
  self.each_pair do |k,v|
    v.respond_to?('deep_each_pair') ? v.deep_each_pair(&block) : yield(k,v)
  end
end
deep_freeze() click to toggle source
# File lib/netzke/core_ext/hash.rb, line 67
def deep_freeze
  each { |k,v| v.deep_freeze if v.respond_to? :deep_freeze }
  freeze
end
flatten_with_type(preffix = "") click to toggle source

add flatten_with_type method to Hash

# File lib/netzke/core_ext/hash.rb, line 50
def flatten_with_type(preffix = "")
  res = []
  self.each_pair do |k,v|
    name = ((preffix.to_s.empty? ? "" : preffix.to_s + "__") + k.to_s).to_sym
    if v.is_a?(Hash)
      res += v.flatten_with_type(name)
    else
      res << {
        :name => name,
        :value => v,
        :type => (["TrueClass", "FalseClass"].include?(v.class.name) ? 'Boolean' : v.class.name).to_sym
      }
    end
  end
  res
end
jsonify() click to toggle source
# File lib/netzke/core_ext/hash.rb, line 18
def jsonify
  self.inject({}) do |h,(k,v)|
    new_key = k.instance_of?(String) || k.instance_of?(Symbol) ? k.jsonify : k
    new_value = v.instance_of?(Array) || v.instance_of?(Hash) ? v.jsonify : v
    h.merge(new_key => new_value)
  end
end
literalize_keys() click to toggle source
# File lib/netzke/core_ext/hash.rb, line 81
def literalize_keys
  update_keys{ |k| k.to_s.l }
  self
end
recursive_delete_if_nil() click to toggle source

We don’t need to pass null values in JSON, they are null by simply being absent

# File lib/netzke/core_ext/hash.rb, line 40
def recursive_delete_if_nil
  self.inject({}) do |h,(k,v)|
    if !v.nil?
      h[k] = v.respond_to?('recursive_delete_if_nil') ? v.recursive_delete_if_nil : v
    end
    h
  end
end
to_nifty_json() click to toggle source

First camelizes the keys, then convert the whole hash to JSON

# File lib/netzke/core_ext/hash.rb, line 27
def to_nifty_json
  self.recursive_delete_if_nil.jsonify.to_json
end
update_keys() click to toggle source

From rubyworks.github.com/facets

# File lib/netzke/core_ext/hash.rb, line 73
def update_keys #:yield:
  if block_given?
    keys.each { |old_key| store(yield(old_key), delete(old_key)) }
  else
    to_enum(:update_keys)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.