Sha256: 6ce1b9876532b241d219775a254d79abc2191b2b6798abb7582a8227290aeb5f

Contents?: true

Size: 964 Bytes

Versions: 5

Compression:

Stored size: 964 Bytes

Contents

class Hash
  # convert hash to string like class="class val" name='name val'
  def to_attr_format(split = ' ')
    res = []
    each do |key, value|
      res << "#{key} = \"#{value.to_s.gsub('"', '\"')}\""
    end
    res.join(split)
  end

  # convert hash to attributes for url_path
  def to_attr_url_format
    res = []
    each do |key, value|
      res << ":#{key} => \"#{value.to_s.gsub('"', '\"')}\""
    end
    res.join ','
  end

  # used for hash of objects
  def find_by(val, attr = 'id')
    each_value do |p|
      return p if p[attr].to_s == val.to_s
    end
    nil
  end

  def to_sym
    symbolize(self)
  end

  private

  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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
camaleon_cms-2.9.0 lib/ext/hash.rb
camaleon_cms-2.8.3 lib/ext/hash.rb
camaleon_cms-2.8.2 lib/ext/hash.rb
camaleon_cms-2.8.1 lib/ext/hash.rb
camaleon_cms-2.8.0 lib/ext/hash.rb