class Hash

Public Instance Methods

find_by(val, attr = "id") click to toggle source

used for hash of objects

# File lib/ext/hash.rb, line 21
def find_by(val, attr = "id")
  self.each do |key, p|
    if p[attr].to_s == val.to_s
      return p
    end
  end
  nil
end
to_attr_format(split = " ") click to toggle source

convert hash to string like class=“class val” name='name val'

# File lib/ext/hash.rb, line 3
def to_attr_format(split = " ")
  res = []
  self.each do |key, value|
    res << "#{key} = \"#{value.to_s.gsub('"', '\"')}\""
  end
  res.join(split)
end
to_attr_url_format() click to toggle source

convert hash to attributes for url_path

# File lib/ext/hash.rb, line 12
def to_attr_url_format
  res = []
  self.each do |key, value|
    res << ":#{key} => \"#{value.to_s.gsub('"', '\"')}\""
  end
  res.join ","
end
to_sym() click to toggle source
# File lib/ext/hash.rb, line 30
def to_sym
  symbolize(self)
end
to_translate() click to toggle source

convert hash to translation string structure sample: {es: “hola mundo”, en: “Hello World”}

> <!–:es–>Hola Mundo<!–:–><!–:en–>Hello World<!–:–>

# File lib/ext/translator.rb, line 67
def to_translate
  res = []
  self.each do|key, val|
    res << "<!--:#{key}-->#{val}<!--:-->"
  end
  res.join("")
end

Private Instance Methods

symbolize(obj) click to toggle source
# File lib/ext/hash.rb, line 35
def symbolize(obj)
  return obj.inject({}){|memo,(k,v)| memo[k.to_sym] =  symbolize(v); memo} if obj.is_a? Hash
  return obj.inject([]){|memo,v    | memo           << symbolize(v); memo} if obj.is_a? Array
  return obj
end