Class: Hash

Inherits:
Object show all
Defined in:
lib/core_extensions.rb

Direct Known Subclasses

HashDealer::Hash

Instance Method Summary collapse

Instance Method Details

#matcher(opts = {}) ⇒ Object

recursively get a matcher for each value



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/core_extensions.rb', line 129

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
      # Still try to pathify strings if this is an array or hash
      if v.is_a?(Array) || v.is_a?(Hash)
        ret[k] = v.pathify_strings
      else
        ret[k] = v
      end
    end
  end
  ret
end

#pathify_stringsObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/core_extensions.rb', line 118

def pathify_strings
  self.each_pair do |k,v|
    if v.is_a?(Array) || v.is_a?(Hash)
      self[k] = v.pathify_strings
    elsif v.instance_of?(String) || v.is_a?(Numeric)
      self[k] = PathString.new(URI.decode(v.to_s))
    end
  end
  self
end