require 'pp' class Hash def deep_merge(other) self.merge(other) do |key, oldval, newval| oldval.deep_merge(newval) end end end module CouchDesignDocs class Directory attr_accessor :couch_view_dir def self.a_to_hash(a) key = a.first if (a.length > 2) { key => a_to_hash(a[1,a.length]) } else { key => a.last } end end def initialize(path) Dir.new(path) # Just checkin' @couch_view_dir = path end def to_hash Dir["#{couch_view_dir}/**/*.js"].inject({}) do |memo, filename| Directory. a_to_hash(expand_file(filename)). deep_merge(memo) end end def expand_file(filename) File.dirname(filename). gsub(/#{couch_view_dir}\/?/, ''). split(/\//) + [ File.basename(filename, '.js'), File.new(filename).read ] end end end