Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created March 18, 2009 01:52
Show Gist options
  • Save mwmitchell/80884 to your computer and use it in GitHub Desktop.
Save mwmitchell/80884 to your computer and use it in GitHub Desktop.
#
# A hash modifier that creates method readers from key names.
# NOTE: reader methods are created recursively.
# The method names are the same as the key names,
# except that the values are snake-cased, for example:
# - QTime -> q_time
# - debugQuery -> debug_query
#
class RSolr::Ext::HashMethodizer
class << self
def snake_case(v)
v = v.to_s
return v.downcase if v =~ /^[A-Z]+$/
v.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
return $+.downcase
end
def methodize!(h)
h.keys.each do |k|
meth = snake_case(k)
meth = meth.gsub(/[^_a-z]/i, '_').gsub(/^_+/i, '')
val_key = case k
when String
"'#{k}'"
when Symbol
":#{k}"
else
raise 'Supports only string/symbol keys!'
end
h.instance_eval <<-RUBY
def #{meth}
val = self[#{val_key}]
if val.respond_to?(:each_pair)
RSolr::Ext::HashMethodizer.methodize!(val)
elsif val.is_a?(Array)
val.each do |item|
RSolr::Ext::HashMethodizer.methodize!(item) if item.respond_to?(:each_pair)
end
end
val
end
RUBY
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment