Sha256: 6020330c86bae32be37f7e01f63e6a308abe390bb80aa4d8d0660a95eac415f5

Contents?: true

Size: 916 Bytes

Versions: 3

Compression:

Stored size: 916 Bytes

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eee-c-couch_design_docs-1.0.0 lib/couch_design_docs/directory.rb
eee-c-couch_design_docs-1.0.1 lib/couch_design_docs/directory.rb
eee-c-couch_design_docs-1.0.2 lib/couch_design_docs/directory.rb