Sha256: 3a0f6ed9d2bb45619fc11d882ef49288401f9c4092d943aa6dce8e9244782b4c

Contents?: true

Size: 908 Bytes

Versions: 3

Compression:

Stored size: 908 Bytes

Contents

class Hash
  def deep_merge(other)
    self.merge(other) do |key, oldval, newval|
      oldval.deep_merge(newval)
    end
  end
end

module CouchDocs
  class DesignDirectory

    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|
        DesignDirectory.
          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 & 2 rubygems

Version Path
eee-c-couch_docs-0.9.0 lib/couch_docs/design_directory.rb
eee-c-couch_docs-1.0.0 lib/couch_docs/design_directory.rb
couch_docs-1.0.0 lib/couch_docs/design_directory.rb