rid 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -9,12 +9,12 @@ Currently Rid supports Rails style Generators you will love, using the same awes
9
9
 
10
10
  == Why?
11
11
 
12
- Why do we need a new Couchdb Application Development Suite? We already have the powerful {RidApp}[http://github.com/ridapp/ridapp].
12
+ Why do we need a new Couchdb Application Development Suite? We already have the powerful {CouchApp}[http://github.com/couchapp/couchapp].
13
13
 
14
- I miss some beauty. I miss some elegance on the command line.
14
+ I need more beauty. I want some elegance on the command line.
15
15
 
16
16
  I know my attempt is almost a small little step towards the beauty I aspire.
17
- I try to pilfer the most of now: From RidApp and Ruby on Rails.
17
+ I try to pilfer the most of now: From CouchApp and Ruby on Rails.
18
18
  So here we go:
19
19
 
20
20
  <em>Rid is designed to structure standalone Couchdb application development for maximum application portability.</em>
@@ -32,7 +32,7 @@ Building standalone Couchdb applications according to correct principles affords
32
32
  == What comes next
33
33
 
34
34
  At the moment, Rid supports generating a scaffold application, pushing to a Couchdb server and pulling from a Couchdb.
35
- Rid injects the !code and !json makros introduced by RidApp.
35
+ Rid injects the !code and !json makros introduced by CouchApp.
36
36
 
37
37
  The next big steps are:
38
38
 
@@ -7,42 +7,51 @@ module Rid
7
7
  ".css" => "text/css",
8
8
  }
9
9
 
10
- def reduce_attachments!
11
- return hash unless hash["_attachments"]
12
- attachments = {}
13
- hash["_attachments"].each do |key, value|
14
- data = value["data"]
15
- next unless data
16
- attachments.update key => decode_attachment(data)
17
- end
18
- hash.update "_attachments" => attachments
19
- end
20
10
 
11
+ # encode attachments and add meta data
12
+ #
21
13
  def map_attachments!
22
- return unless hash["_attachments"]
23
- attachments = {}
24
- flatten_attachements(hash["_attachments"]).each do |key, value|
25
- attachments.update key => {
14
+ return if attachments.empty?
15
+
16
+ doc = {}
17
+ attachments.flatten.each do |key, value|
18
+ doc[key] = {
26
19
  "data" => encode_attachment(value),
27
20
  "content_type" => mime_type_for(key)
28
21
  }
29
22
  end
30
- self.hash.update "_attachments" => attachments
23
+
24
+ self.attachments = doc
31
25
  end
32
26
 
33
- def flatten_attachements(doc, base = nil)
34
- result = {}
35
- doc.each do |key, value|
36
- new_base = base ? [base, key].join('/') : key
37
- if value.is_a?(Hash)
38
- result.update flatten_attachements(value, new_base)
39
- else
40
- result.update new_base => value
41
- end
27
+ # decode attachments and flatten meta data hash
28
+ #
29
+ def reduce_attachments!
30
+ return if attachments.empty?
31
+
32
+ doc = {}
33
+ attachments.each do |key, value|
34
+ data = value["data"]
35
+ next unless data
36
+ doc[key] = decode_attachment(data)
42
37
  end
43
- result
38
+
39
+ self.attachments = doc
44
40
  end
45
41
 
42
+ private
43
+
44
+ # accessor for attachments hash
45
+ #
46
+ def attachments
47
+ hash["_attachments"] || {}
48
+ end
49
+
50
+ def attachments=(value)
51
+ self.hash["_attachments"] = value
52
+ end
53
+
54
+
46
55
  def decode_attachment(data)
47
56
  data.unpack("m").first
48
57
  end
@@ -0,0 +1,57 @@
1
+ class Hash
2
+ DEFAULT_SEPERATOR = '/'
3
+
4
+ def flatten(seperator = DEFAULT_SEPERATOR, prefix = nil)
5
+ new_hash = {}
6
+
7
+ each do |key, value|
8
+ new_key = [prefix, key].compact.join(seperator)
9
+ if value.is_a?(self.class)
10
+ new_hash.update value.flatten(seperator, new_key)
11
+ else
12
+ new_hash.update new_key => value
13
+ end
14
+ end
15
+
16
+ new_hash
17
+ end
18
+
19
+ def flatten!(*args)
20
+ replace flatten(*args)
21
+ end
22
+
23
+ def at(path, seperator = DEFAULT_SEPERATOR)
24
+ current = self
25
+
26
+ parts = path.split(DEFAULT_SEPERATOR)
27
+ key = parts.pop
28
+
29
+ return unless key
30
+
31
+ parts.each do |part|
32
+ current = current[part]
33
+
34
+ return unless current.is_a?(self.class)
35
+ end
36
+
37
+ current[key]
38
+ end
39
+
40
+ def update_at(path, value, seperator = DEFAULT_SEPERATOR)
41
+ current = self
42
+
43
+ parts = path.split(DEFAULT_SEPERATOR)
44
+ key = parts.pop
45
+
46
+ return self unless key
47
+
48
+ parts.each do |part|
49
+ current[part] ||= {}
50
+ current = current[part]
51
+
52
+ raise 'updating value at %s failed!' % inspect unless current.is_a?(self.class)
53
+ end
54
+
55
+ current[key] = value
56
+ end
57
+ end
@@ -1,3 +1,4 @@
1
+ require 'rid/core_ext/hash'
1
2
  require 'rid/attachments'
2
3
  require 'rid/makros'
3
4
 
@@ -49,7 +50,7 @@ module Rid
49
50
  # strip extname from javascript files
50
51
  key.sub!(/\.js$/, '') if filename =~ /#{JAVASCRIPT_FILES.join('|')}/
51
52
 
52
- set_hash_at key, block.call(filename)
53
+ hash.update_at key, block.call(filename)
53
54
  end
54
55
 
55
56
  map_attachments!
@@ -71,20 +72,16 @@ module Rid
71
72
  # the value holds base64 encoded data as well as other metadata.
72
73
  # This data will gets decoded and used as value for the key.
73
74
  #
74
- def write(directory = nil, doc = nil, &block)
75
+ def write(&block)
75
76
  reduce_attachments!
76
77
  reject_makros!
77
78
 
78
- doc ||= hash
79
- doc.each do |key, value|
80
- filename = directory ? File.join(directory, key) : key.dup
81
- if value.is_a?(Hash)
82
- write(filename, value, &block)
83
- else
84
- # append extname to javascript files
85
- filename << '.js' if filename =~ /#{JAVASCRIPT_FILES.join('|')}/
86
- block.call(filename, value)
87
- end
79
+ hash.flatten.each do |key, value|
80
+ filename = key.dup
81
+ # append extname to javascript files
82
+ filename << '.js' if filename =~ /#{JAVASCRIPT_FILES.join('|')}/
83
+
84
+ block.call(filename, value)
88
85
  end
89
86
  end
90
87
 
@@ -137,35 +134,8 @@ module Rid
137
134
  base_url + build_options_string(options)
138
135
  end
139
136
 
140
- private
141
-
142
- def hash_at(path)
143
- current_hash = hash
144
-
145
- parts = path.split('/')
146
- key = parts.pop
147
-
148
- parts.each do |part|
149
- current_hash[part] ||= {}
150
- current_hash = current_hash[part]
151
- end
152
-
153
- current_hash[key]
154
- end
155
-
156
- def set_hash_at(path, value)
157
- current_hash = hash
158
137
 
159
- parts = path.split('/')
160
- key = parts.pop
161
-
162
- parts.each do |part|
163
- current_hash[part] ||= {}
164
- current_hash = current_hash[part]
165
- end
166
-
167
- current_hash[key] = value
168
- end
138
+ private
169
139
 
170
140
  def build_options_string(options)
171
141
  return '' if options.empty?
data/lib/rid/makros.rb CHANGED
@@ -1,105 +1,94 @@
1
1
  module Rid
2
2
  module Makros
3
+ # inject makros, that is:
4
+ # replace makro with content from libs
5
+ #
3
6
  def inject_makros!
4
- self.hash = inject_code_makro(hash)
5
- self.hash = inject_json_makro(hash)
7
+ inject_makro! "code" do |filename|
8
+ value = libs.at(filename)
9
+ raise "code makro injection failed!\nNo lib available for %s" % filename unless value
10
+
11
+ value
12
+ end
13
+
14
+ inject_makro! "json" do |filename|
15
+ value = libs.at(filename)
16
+ raise "json makro injection failed!\nNo lib available for %s" % filename unless value
17
+
18
+ expand_json_makro filename, value
19
+ end
6
20
  end
7
21
 
22
+ # reject makros, that is:
23
+ # replace makro generated content with makro
24
+ #
8
25
  def reject_makros!
9
- return if libs.empty?
10
- # Attention: replace json makros first!
11
- self.hash = reject_json_makro(hash, libs)
12
- self.hash = reject_code_makro(hash, libs)
13
- end
26
+ return if injections.empty?
14
27
 
15
- def libs
16
- @libs ||= flatten_libs
17
- end
28
+ injections.reverse.each do |inj|
29
+ makro, target, lib, start, size = inj["makro"], inj["target"], inj["lib"], inj["start"], inj["size"]
18
30
 
19
- def flatten_libs(libs = hash["lib"] || {}, key = nil)
20
- result = {}
21
- libs.each do |name, value|
22
- new_key = key ? File.join(key, name) : name
23
- if value.is_a?(Hash)
24
- result.update flatten_libs(value, new_key)
25
- else
26
- result.update new_key => value
27
- end
31
+ value = hash.at(target)
32
+ value[start, size] = "// !#{makro} #{lib}"
28
33
  end
29
- result
30
34
  end
31
35
 
32
36
 
33
- def inject_code_makro(doc)
34
- doc.each do |key, value|
35
- doc[key] = if value.is_a?(String)
36
- value.gsub(/\/\/\s*!code.*$/) do |match|
37
- filename = match.sub(/^.*!code\s*(\S+).*$/, '\1')
38
- libs[filename]
39
- end
40
- elsif value.is_a?(Hash)
41
- inject_code_makro(value)
42
- else
43
- value
44
- end
45
- end
37
+ private
38
+
39
+ # provide access to lib hash
40
+ #
41
+ def libs
42
+ hash["lib"] || {}
43
+ end
46
44
 
47
- doc
45
+ # provide access to injections array
46
+ #
47
+ def injections
48
+ hash["injections"] || []
48
49
  end
49
50
 
50
- def inject_json_makro(doc)
51
- doc.each do |key, value|
52
- doc[key] = if value.is_a?(String)
53
- value.gsub(/\/\/\s*!json.*$/) do |match|
54
- filename = match.sub(/^.*!json\s*(\S+).*$/, '\1')
55
- 'var %s = %s;' % [filename.sub(/\..*$/, ''), libs[filename].to_json]
56
- end
57
- elsif value.is_a?(Hash)
58
- inject_json_makro(value)
59
- else
60
- value
61
- end
62
- end
63
51
 
64
- doc
52
+ # builds a javascript variable assignment line for filename with value
53
+ def expand_json_makro(filename, value)
54
+ 'var %s = %s;' % [filename.split(/[\.\/]/).join('_'), value.to_json]
65
55
  end
66
56
 
57
+ # injects a makro and yields block with the filenames for found makros
58
+ #
59
+ def inject_makro!(makro, &block)
60
+ hash.flatten.each do |key, value|
61
+ next unless value.is_a?(String)
62
+
63
+ # do not try to inject makros in attachments, libs and injections array
64
+ next if key =~ /^_attachments|libs|injections/
65
+
66
+ new_value = value.gsub(/\/\/\s*!#{makro}\s+[\S]+$/) do |match|
67
+ start = Regexp.last_match.begin(0)
68
+ filename = match.sub(/^.*!#{makro}\s*(\S+).*$/, '\1')
67
69
 
68
- def reject_code_makro(doc, libs)
69
- doc = doc.dup
70
- doc.each do |key, value|
71
- next if key == "lib"
72
-
73
- if value.is_a?(String)
74
- libs.each do |name, content|
75
- # only try substituting strings
76
- next unless content.is_a?(String)
77
- next unless value.include?(content)
78
- doc[key] = value.gsub(content, "// !code #{name}")
79
- end
80
- elsif value.is_a?(Hash)
81
- doc[key] = reject_code_makro(value, libs)
70
+ value = block.call(filename)
71
+
72
+ store_injection makro, key, filename, start, value.size
73
+
74
+ value
82
75
  end
76
+
77
+ self.hash.update_at key, new_value
83
78
  end
84
- doc
85
79
  end
86
80
 
87
- def reject_json_makro(doc, libs)
88
- doc.each do |key, value|
89
- next if key == "lib"
90
- if value.is_a?(String)
91
- libs.each do |name, content|
92
- # only try substituting strings
93
- next unless content.is_a?(String)
94
- json = 'var %s = %s;' % [name.sub(/\..*$/, ''), content.to_json]
95
- next unless value.include?(json)
96
- doc[key] = value.gsub(json, "// !json #{name}")
97
- end
98
- elsif value.is_a?(Hash)
99
- doc[key] = reject_json_makro(value, libs)
100
- end
101
- end
102
- doc
81
+ # Store injection values in injections array
82
+ #
83
+ def store_injection(makro, target, lib, start, size)
84
+ hash["injections"] ||= []
85
+ hash["injections"] << {
86
+ "makro" => makro,
87
+ "target" => target,
88
+ "lib" => lib,
89
+ "start" => start,
90
+ "size" => size
91
+ }
103
92
  end
104
93
  end
105
94
  end
data/lib/rid/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rid
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/rid.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rid}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Johannes J. Schmidt"]
12
- s.date = %q{2010-03-11}
12
+ s.date = %q{2010-03-12}
13
13
  s.default_executable = %q{rid}
14
14
  s.description = %q{With Couch you can easy build a standalone CouchDB application. Couch aims to bring some of the Rails beauty to CouchDB. Currently Couch supports Rails style Generators you will love, using the same awesome Thor library used in Rails3.}
15
15
  s.email = %q{schmidt@netzmerk.com}
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
37
37
  "lib/rid/commands/pull.rb",
38
38
  "lib/rid/commands/push.rb",
39
39
  "lib/rid/commands/routes.rb",
40
+ "lib/rid/core_ext/hash.rb",
40
41
  "lib/rid/design_document.rb",
41
42
  "lib/rid/generators.rb",
42
43
  "lib/rid/generators/application/USAGE",
@@ -68,7 +69,10 @@ Gem::Specification.new do |s|
68
69
  "lib/rid/makros.rb",
69
70
  "lib/rid/version.rb",
70
71
  "rid.gemspec",
72
+ "spec/rid/attachments_spec.rb",
73
+ "spec/rid/core_ext/hash_spec.rb",
71
74
  "spec/rid/design_document_spec.rb",
75
+ "spec/rid/makros_spec.rb",
72
76
  "spec/rid_spec.rb",
73
77
  "spec/spec.opts",
74
78
  "spec/spec_helper.rb"
@@ -82,6 +86,9 @@ Gem::Specification.new do |s|
82
86
  s.test_files = [
83
87
  "spec/spec_helper.rb",
84
88
  "spec/rid_spec.rb",
89
+ "spec/rid/core_ext/hash_spec.rb",
90
+ "spec/rid/makros_spec.rb",
91
+ "spec/rid/attachments_spec.rb",
85
92
  "spec/rid/design_document_spec.rb"
86
93
  ]
87
94
 
@@ -0,0 +1,38 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'rid/design_document'
3
+
4
+ describe "Attachments" do
5
+ before do
6
+ @doc = Rid::DesignDocument.new
7
+ end
8
+
9
+ describe "map" do
10
+ describe "_attachments encoding and content_type" do
11
+ it "should proper encode and add plain text content type" do
12
+ @doc.hash = { "_attachments" => { "key" => "value" } }
13
+ @doc.map_attachments!
14
+ @doc.hash.should == { "_attachments" => { "key" => { "data" => "dmFsdWU=", "content_type" => "text/plain" } } }
15
+ end
16
+
17
+ it "should proper encode and add html content type" do
18
+ @doc.hash = { "_attachments" => { "key.html" => "value" } }
19
+ @doc.map_attachments!
20
+ @doc.hash.should == { "_attachments" => { "key.html" => { "data" => "dmFsdWU=", "content_type" => "text/html" } } }
21
+ end
22
+
23
+ it "should proper map nested attachments" do
24
+ @doc.hash = { "_attachments" => { "hash" => { "key" => "value" } } }
25
+ @doc.map_attachments!
26
+ @doc.hash.should == { "_attachments" => { "hash/key" => { "data" => "dmFsdWU=", "content_type" => "text/plain" } } }
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "reduce" do
32
+ it "should decode _attachments data" do
33
+ @doc.hash = { "_attachments" => { "key" => { "data" => "dmFsdWU=" } } }
34
+ @doc.reduce_attachments!
35
+ @doc.hash.should == { "_attachments" => { "key" => "value" } }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,71 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require 'rid/core_ext/hash'
3
+
4
+ describe "Hash" do
5
+ describe "flatten" do
6
+ it "should untouch flat hash" do
7
+ hash = { "a" => 1, "b" => 2 }
8
+ hash.flatten.should == hash
9
+ end
10
+
11
+ it "should flaten simple nested hash" do
12
+ hash = { "a" => { "b" => 1 } }
13
+ hash.flatten.should == { "a/b" => 1 }
14
+ end
15
+
16
+ it "should flaten deep nested hash" do
17
+ hash = { "a" => { "b" => { "c" => 1 } } }
18
+ hash.flatten.should == { "a/b/c" => 1 }
19
+ end
20
+ end
21
+
22
+ describe "flatten!" do
23
+ it "should change receiver" do
24
+ hash = { "a" => { "b" => 1 } }
25
+ hash.flatten!
26
+ hash.should == { "a/b" => 1 }
27
+ end
28
+ end
29
+
30
+ describe "at" do
31
+ it "should retrieve value at top level" do
32
+ hash = { "a" => 1 }
33
+ hash.at("a").should == 1
34
+ end
35
+
36
+ it "should retrieve value at nested level" do
37
+ hash = { "a" => { "b" => 1 } }
38
+ hash.at("a/b").should == 1
39
+ end
40
+
41
+ it "should retrieve value at deep nested level" do
42
+ hash = { "a" => { "b" => { "c" => 1 } } }
43
+ hash.at("a/b/c").should == 1
44
+ end
45
+
46
+ it "should return nil if not present" do
47
+ hash = { "a" => { "c" => { "c" => 1 } } }
48
+ hash.at("a/b/c").should == nil
49
+ end
50
+ end
51
+
52
+ describe "update_at" do
53
+ it "should insert value at top level" do
54
+ hash = { }
55
+ hash.update_at "a", 1
56
+ hash.should == { "a" => 1 }
57
+ end
58
+
59
+ it "should insert value at nested level" do
60
+ hash = { }
61
+ hash.update_at "a/b", 1
62
+ hash.should == { "a" => { "b" => 1 } }
63
+ end
64
+
65
+ it "should insert value at deep nested level" do
66
+ hash = { }
67
+ hash.update_at "a/b/c", 1
68
+ hash.should == { "a" => { "b" => { "c" => 1 } } }
69
+ end
70
+ end
71
+ end
@@ -6,6 +6,7 @@ describe "DesignDocument" do
6
6
  @doc = Rid::DesignDocument.new
7
7
  end
8
8
 
9
+
9
10
  describe "read" do
10
11
  it "should assign key-value pair" do
11
12
  @doc.read("key") do |filename|
@@ -42,28 +43,6 @@ describe "DesignDocument" do
42
43
  @doc.hash.should == { "hash" => { "hash" => { "key" => "value" } } }
43
44
  end
44
45
 
45
- describe "_attachments encoding and content_type" do
46
- it "should proper encode and add plain text content type" do
47
- @doc.read("_attachments/key") do |filename|
48
- "value"
49
- end
50
- @doc.hash.should == { "_attachments" => { "key" => { "data" => "dmFsdWU=", "content_type" => "text/plain" } } }
51
- end
52
-
53
- it "should proper encode and add html content type" do
54
- @doc.read("_attachments/key.html") do |filename|
55
- "value"
56
- end
57
- @doc.hash.should == { "_attachments" => { "key.html" => { "data" => "dmFsdWU=", "content_type" => "text/html" } } }
58
- end
59
-
60
- it "should proper encode nested keys" do
61
- @doc.read("_attachments/hash/key") do |filename|
62
- "value"
63
- end
64
- @doc.hash.should == { "_attachments" => { "hash/key" => { "data" => "dmFsdWU=", "content_type" => "text/plain" } } }
65
- end
66
- end
67
46
 
68
47
  describe "exclude files should not be mapped" do
69
48
  it "should not map README" do
@@ -96,149 +75,59 @@ describe "DesignDocument" do
96
75
  @doc.hash.should == { "views" => { "my_view" => { "map" => "value" } } }
97
76
  end
98
77
  end
99
-
100
- describe "code makro" do
101
- it "should expand code" do
102
- idx = 0
103
- @doc.read("key", "lib/code.js") do |filename|
104
- case idx += 1
105
- when 1
106
- "// !code code.js"
107
- when 2
108
- "value"
109
- end
110
- end
111
- @doc.hash.should == { "key" => "value", "lib" => { "code.js" => "value" } }
112
- end
113
- end
114
-
115
- describe "json makro" do
116
- it "should expand json" do
117
- idx = 0
118
- @doc.read("key", "lib/json.json") do |filename|
119
- case idx += 1
120
- when 1
121
- "// !json json.json"
122
- when 2
123
- "value"
124
- end
125
- end
126
- @doc.hash.should == { "key" => "var json = \"value\";", "lib" => { "json.json" => "value" } }
127
- end
128
- end
129
78
  end
130
79
 
80
+
131
81
  describe "write" do
132
82
  it "should return key-value pair" do
133
83
  @doc.hash = { "key" => "value" }
134
- filename, content = nil, nil
135
84
  @doc.write do |key, value|
136
- filename, content = key, value
85
+ key.should == "key"
86
+ value.should == "value"
137
87
  end
138
- filename.should == "key"
139
- content.should == "value"
140
88
  end
141
89
 
142
90
  it "should return subdirectory for nested hash" do
143
91
  @doc.hash = { "hash" => { "key" => "value" } }
144
- filename, content = nil, nil
145
92
  @doc.write do |key, value|
146
- filename, content = key, value
93
+ key.should == "hash/key"
94
+ value.should == "value"
147
95
  end
148
- filename.should == "hash/key"
149
- content.should == "value"
150
96
  end
151
97
 
152
98
  it "should return subdirectory for nested hash" do
153
99
  @doc.hash = { "hash" => { "hash" => { "key" => "value" } } }
154
- filename, content = nil, nil
155
100
  @doc.write do |key, value|
156
- filename, content = key, value
101
+ key.should == "hash/hash/key"
102
+ value.should == "value"
157
103
  end
158
- filename.should == "hash/hash/key"
159
- content.should == "value"
160
- end
161
-
162
- it "should return decoded _attachments data" do
163
- @doc.hash = { "_attachments" => { "key" => { "data" => "dmFsdWU=" } } }
164
- filename, content = nil, nil
165
- @doc.write do |key, value|
166
- filename, content = key, value
167
- end
168
- filename.should == "_attachments/key"
169
- content.should == "value"
170
104
  end
171
105
 
172
106
  describe "javascript extensions" do
173
107
  it "should append validate_doc_update" do
174
108
  @doc.hash = { "validate_doc_update" => "value" }
175
- filename = nil
176
109
  @doc.write do |key, value|
177
- filename = key
110
+ key.should == "validate_doc_update.js"
178
111
  end
179
- filename.should == "validate_doc_update.js"
180
112
  end
181
113
 
182
114
  it "should append lists/my_list" do
183
115
  @doc.hash = { "lists" => { "my_list" => "value" } }
184
- filename = nil
185
116
  @doc.write do |key, value|
186
- filename = key
117
+ key.should == "lists/my_list.js"
187
118
  end
188
- filename.should == "lists/my_list.js"
189
119
  end
190
120
 
191
121
  it "should append views/my_view/map" do
192
122
  @doc.hash = { "views" => { "my_view" => { "map" => "value" } } }
193
- filename = nil
194
- @doc.write do |key, value|
195
- filename = key
196
- end
197
- filename.should == "views/my_view/map.js"
198
- end
199
- end
200
-
201
- describe "code makro" do
202
- it "should reject code" do
203
- @doc.hash = { "key" => "value", "lib" => { "code.js" => "value" } }
204
- content = nil
205
- @doc.write do |key, value|
206
- content = value
207
- end
208
- content.should == "// !code code.js"
209
- end
210
-
211
- it "should reject nested code" do
212
- @doc.hash = { "key" => "value", "lib" => { "hash" => { "code.js" => "value" } } }
213
- content = nil
214
123
  @doc.write do |key, value|
215
- content = value
124
+ key.should == "views/my_view/map.js"
216
125
  end
217
- content.should == "// !code hash/code.js"
218
- end
219
- end
220
-
221
- describe "json makro" do
222
- it "should reject json" do
223
- @doc.hash = { "key" => "var json = \"value\";", "lib" => { "json.json" => "value" } }
224
- content = nil
225
- @doc.write do |key, value|
226
- content = value
227
- end
228
- content.should == "// !json json.json"
229
- end
230
-
231
- it "should reject nested json" do
232
- @doc.hash = { "key" => "var json = \"value\";", "lib" => { "hash" => { "json.json" => "value" } } }
233
- content = nil
234
- @doc.write do |key, value|
235
- content = value
236
- end
237
- content.should == "// !json hash/json.json"
238
126
  end
239
127
  end
240
128
  end
241
129
 
130
+
242
131
  describe "json" do
243
132
  it "should convert key-value pair" do
244
133
  @doc.hash = { "key" => "value" }
@@ -263,6 +152,7 @@ describe "DesignDocument" do
263
152
  end
264
153
  end
265
154
 
155
+
266
156
  describe "id accessor" do
267
157
  it "should return id from hash" do
268
158
  @doc.hash = { "_id" => "my_id" }
@@ -302,6 +192,7 @@ describe "DesignDocument" do
302
192
  end
303
193
  end
304
194
 
195
+
305
196
  describe "base url" do
306
197
  it "should combine database and id" do
307
198
  @doc.should_receive(:id).and_return("my_id")
@@ -0,0 +1,84 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'rid/design_document'
3
+
4
+ describe "Makros" do
5
+ before do
6
+ @doc = Rid::DesignDocument.new
7
+ end
8
+
9
+ describe "inject" do
10
+ describe "code makro" do
11
+ it "should expand code" do
12
+ @doc.hash = { "hash" => { "key" => "// !code code.js" }, "lib" => { "code.js" => "value" } }
13
+ @doc.inject_makros!
14
+ @doc.hash["hash"]["key"].should == "value"
15
+ end
16
+
17
+ it "should not remove code that follows" do
18
+ @doc.hash = { "hash" => { "key" => "// !code code.js\nvalue" }, "lib" => { "code.js" => "value" } }
19
+ @doc.inject_makros!
20
+ @doc.hash["hash"]["key"].should == "value\nvalue"
21
+ end
22
+
23
+ it "should store injection" do
24
+ @doc.hash = { "hash" => { "key" => "// !code code.js" }, "lib" => { "code.js" => "value" } }
25
+ @doc.inject_makros!
26
+ @doc.hash["injections"].should == [{ "makro" => "code", "target" => "hash/key", "lib" => "code.js", "start" => 0, "size" => 5 }]
27
+ end
28
+ end
29
+
30
+ describe "json makro" do
31
+ it "should expand json" do
32
+ @doc.hash = { "hash" => { "key" => "// !json json.json" }, "lib" => { "json.json" => "value" } }
33
+ @doc.inject_makros!
34
+ @doc.hash["hash"]["key"].should == 'var json_json = "value";'
35
+ end
36
+
37
+ it "should not remove code that follows" do
38
+ @doc.hash = { "hash" => { "key" => "// !json json.json\nvalue" }, "lib" => { "json.json" => "value" } }
39
+ @doc.inject_makros!
40
+ @doc.hash["hash"]["key"].should == "var json_json = \"value\";\nvalue"
41
+ end
42
+
43
+ it "should store injection" do
44
+ @doc.hash = { "hash" => { "key" => "// !json json.json" }, "lib" => { "json.json" => "value" } }
45
+ @doc.inject_makros!
46
+ @doc.hash["injections"].should == [{ "makro" => "json", "target" => "hash/key", "lib" => "json.json", "start" => 0, "size" => 24 }]
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "reject" do
52
+ describe "code makro" do
53
+ it "should reject code" do
54
+ @doc.hash = { "lib" => { "code.js" => "value" }, "key" => "// !code code.js" }
55
+ @doc.inject_makros!
56
+ @doc.reject_makros!
57
+ @doc.hash["key"].should == "// !code code.js"
58
+ end
59
+
60
+ it "should reject nested code" do
61
+ @doc.hash = { "lib" => { "hash" => { "code.js" => "value" } }, "key" => "// !code hash/code.js" }
62
+ @doc.inject_makros!
63
+ @doc.reject_makros!
64
+ @doc.hash["key"].should == "// !code hash/code.js"
65
+ end
66
+ end
67
+
68
+ describe "json makro" do
69
+ it "should reject json" do
70
+ @doc.hash = { "lib" => { "json.json" => "value" }, "key" => "// !json json.json" }
71
+ @doc.inject_makros!
72
+ @doc.reject_makros!
73
+ @doc.hash["key"].should == "// !json json.json"
74
+ end
75
+
76
+ it "should reject nested json" do
77
+ @doc.hash = { "lib" => { "hash" => { "json.json" => "value" } }, "key" => "// !json hash/json.json" }
78
+ @doc.inject_makros!
79
+ @doc.reject_makros!
80
+ @doc.hash["key"].should == "// !json hash/json.json"
81
+ end
82
+ end
83
+ end
84
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Johannes J. Schmidt
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-11 00:00:00 +01:00
17
+ date: 2010-03-12 00:00:00 +01:00
18
18
  default_executable: rid
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -116,6 +116,7 @@ files:
116
116
  - lib/rid/commands/pull.rb
117
117
  - lib/rid/commands/push.rb
118
118
  - lib/rid/commands/routes.rb
119
+ - lib/rid/core_ext/hash.rb
119
120
  - lib/rid/design_document.rb
120
121
  - lib/rid/generators.rb
121
122
  - lib/rid/generators/application/USAGE
@@ -147,7 +148,10 @@ files:
147
148
  - lib/rid/makros.rb
148
149
  - lib/rid/version.rb
149
150
  - rid.gemspec
151
+ - spec/rid/attachments_spec.rb
152
+ - spec/rid/core_ext/hash_spec.rb
150
153
  - spec/rid/design_document_spec.rb
154
+ - spec/rid/makros_spec.rb
151
155
  - spec/rid_spec.rb
152
156
  - spec/spec.opts
153
157
  - spec/spec_helper.rb
@@ -184,4 +188,7 @@ summary: Standalone Couchdb Application Development Suite
184
188
  test_files:
185
189
  - spec/spec_helper.rb
186
190
  - spec/rid_spec.rb
191
+ - spec/rid/core_ext/hash_spec.rb
192
+ - spec/rid/makros_spec.rb
193
+ - spec/rid/attachments_spec.rb
187
194
  - spec/rid/design_document_spec.rb