Sha256: 6e8b94813759d4aca76e730b2056a1490f913dcf28dc33da9537f867624d5abe

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

class Object
  # allows us to call matcher blindly
  def matcher(opts = {})
    return self
  end
end

# The only really important matcher
class String
  def matcher(opts = {})
    ":#{self}"
  end
end

class Hash
  def pathify_strings!
    self.each_pair do |k,v|
      if v.is_a?(Array) || v.is_a?(Hash)
        v.pathify_strings!
      elsif v.instance_of?(String)
        self[k] = PathString.new(URI.decode(v))
      end
    end
  end
  # recursively get a matcher for each value
  def matcher(opts = {})
    opts[:only] ||= self.keys.collect(&:to_sym)
    opts[:only] = opts[:only].collect(&:to_sym)
    opts[:only] -= (opts[:except] || []).collect(&:to_sym)
    
    ret = self.class.new
    self.each_pair do |k,v|
      if opts[:only].include?(k.to_sym)
        ret[k] = v.matcher(opts[k.to_sym] || {})
      else
        ret[k] = v
      end
    end
    ret
  end
end
class Array
  def pathify_strings!
    self.each_with_index do |v,k|
      if v.is_a?(Array) || v.is_a?(Hash)
        v.pathify_strings!
      elsif v.instance_of?(String)
        self[k] = PathString.new(URI.decode(v))
      end
    end
  end
  # call matcher on all of the elements
  def matcher(opts = {})
    self.collect(&:matcher)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hash_dealer-1.2.0 lib/core_extensions.rb
hash_dealer-1.1.3 lib/core_extensions.rb
hash_dealer-1.1.2 lib/core_extensions.rb