Sha256: eba90d2043a060d8f229fe7f10ac2f3164f761ee3d67d1fb4004a9a7d27ea2eb

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'set'

class Set
  def push(*args)
    send(:<<, *args)
  end
end

class Hash
  def to_mash
    Hashie::Mash.new(self)
  end

  def lookup_path dot_separated_path, create=false
    parts = dot_separated_path.to_s.split('.')

    parts.reduce(self) do |memo, part|
      memo && memo[part.to_sym] ||= memo[part.to_s] || (create if create)
    end
  end

  def make_path dot_separated_path, blank_value, overwrite=false
    dot_separated_path = dot_separated_path.to_s
    existing = lookup_path(dot_separated_path)

    return existing if existing && !overwrite

    parts = dot_separated_path.split(".")

    current = self

    if parts.length == 1
      return self[parts.first] ||= blank_value
    end

    key = parts.pop

    parts.each do |part|
      current = current[part.to_sym] = current.delete(part.to_s) || {}
    end

    current[key.to_sym] = blank_value
    lookup_path(dot_separated_path, false)
  end
end

class Pathname
  def to_pathname
    self
  end
end

class String
  def to_pathname
    Pathname(self)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stylish-0.3.1 lib/stylish/core_ext.rb
stylish-0.3.0 lib/stylish/core_ext.rb