Sha256: 00e5ca4b5553d5694ec660c5557529098f5c983aa165508fe782b12895f5759d

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

#
# 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mwmitchell-rsolr-ext-0.5.5 lib/rsolr-ext/hash_methodizer.rb
mwmitchell-rsolr-ext-0.5.6 lib/rsolr-ext/hash_methodizer.rb