Skip to content

Commit

Permalink
soln for #6 but lacks dumping support
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Carleton committed Apr 25, 2010
1 parent 862ffa3 commit 9850a54
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/couch_docs/command_line.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'optparse'
require 'pp'

require 'rubygems'
require 'directory_watcher'

module CouchDocs
Expand Down
28 changes: 21 additions & 7 deletions lib/couch_docs/design_directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,38 @@ def initialize(path)
# Load

def to_hash
Dir["#{couch_view_dir}/**/*.js"].inject({}) do |memo, filename|
Dir["#{couch_view_dir}/**/*.{js,json}"].inject({}) do |memo, filename|
DesignDirectory.
a_to_hash(expand_file(filename)).
deep_merge(memo)
end
end

def expand_file(filename)
if filename =~ /\.js$/
name_value_pair = [
File.basename(filename, '.js'),
read_js_value(filename)
]
elsif filename =~ /\.json$/
name_value_pair = [
File.basename(filename, '.json'),
read_json_value(filename)
]
end

name_value_pair[0].gsub!(/%2F/, '/')

File.dirname(filename).
gsub(/#{couch_view_dir}\/?/, '').
split(/\//) +
[
File.basename(filename, '.js').gsub(/%2F/, '/'),
read_value(filename)
]
split(/\//) + name_value_pair
end

def read_json_value(filename)
JSON.parse(File.new(filename).read)
end

def read_value(filename)
def read_js_value(filename)
File.
readlines(filename).
map { |line| process_code_macro(line) }.
Expand Down
18 changes: 16 additions & 2 deletions spec/couch_docs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,24 @@
@it = DesignDirectory.new("fixtures/_design")
end

it "should list dirs, basename and contents of a file" do
it "should list dirs, basename and contents of a js file" do
@it.expand_file("fixtures/_design/a/b/c.js").
should == ['a', 'b', 'c', 'function(doc) { return true; }']
end

it "should list dirs, basename and contents of a json file" do
@it.expand_file("fixtures/_design/a/e.json").
should == ['a', 'e', [{"one" => "2"}]]
end

it "should assemble all documents into a single docs structure" do
@it.to_hash['a'].
should == {
'b' => {
'c' => 'function(doc) { return true; }',
'd' => 'function(doc) { return true; }'
}
},
'e' => [{"one" => "2"}]
}
end

Expand All @@ -318,6 +324,11 @@
}
end

it "should ignore macro escape sequence when reading JSON" do
@it.to_hash['j'].
should == {'q' => ["!code foo.js"]}
end

it "should work with absolute !code paths"

it "should replace !code macros with the contents of the referenced file in lib" do
Expand All @@ -341,7 +352,10 @@

@it.read_from_lib("foo.js")
end
end

# FIXME: json valued attributs get mangled when dumping design docs.
context "saving a JSON attribute" do
end

context "saving a JS attribute" do
Expand Down

0 comments on commit 9850a54

Please sign in to comment.