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')
  each_value do |p|
    return p if p[attr].to_s == val.to_s
  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 = []
  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 = []
  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 28
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 66
def to_translate
  res = []
  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 34
def symbolize(obj)
  if obj.is_a? Hash
    return obj.each_with_object({}) do |(k, v), memo|
             memo[k.to_sym] = symbolize(v)
           end
  end
  if obj.is_a? Array
    return obj.each_with_object([]) do |v, memo|
             memo << symbolize(v)
           end
  end

  obj
end