safety_pin 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rspec CHANGED
@@ -1 +1 @@
1
- --format progress --color
1
+ --format p --color
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm use --create jruby-1.6.7@safety_pin
1
+ rvm use --create jruby-1.6.6@safety_pin
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "rspec", "~> 2.10.0"
3
+ gem "rspec", "~> 2.10.0"
4
+ gem "nyan-cat-formatter"
data/Gemfile.lock CHANGED
@@ -2,6 +2,7 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  diff-lcs (1.1.3)
5
+ nyan-cat-formatter (0.0.7)
5
6
  rspec (2.10.0)
6
7
  rspec-core (~> 2.10.0)
7
8
  rspec-expectations (~> 2.10.0)
@@ -12,7 +13,9 @@ GEM
12
13
  rspec-mocks (2.10.1)
13
14
 
14
15
  PLATFORMS
16
+ java
15
17
  ruby
16
18
 
17
19
  DEPENDENCIES
20
+ nyan-cat-formatter
18
21
  rspec (~> 2.10.0)
data/bin/console ADDED
@@ -0,0 +1 @@
1
+ irb -r './console_loader' --simple-prompt
data/console_loader.rb ADDED
@@ -0,0 +1,4 @@
1
+ $:<<File.dirname(__FILE__)
2
+ require 'lib/safety_pin'
3
+ include SafetyPin
4
+ JCR.dev_login
@@ -1,3 +1,5 @@
1
+ require 'pathname'
2
+
1
3
  module SafetyPin
2
4
  class Node
3
5
  include_class 'javax.jcr.PropertyType'
@@ -13,13 +15,22 @@ module SafetyPin
13
15
  nil
14
16
  end
15
17
 
18
+ def self.find_or_create(path)
19
+ find(path) || create(path)
20
+ end
21
+
22
+ def self.exists?(path)
23
+ find(path) != nil
24
+ end
25
+
16
26
  def self.session
17
27
  JCR.session
18
28
  end
19
29
 
20
- def self.build(path, node_type = nil)
30
+ def self.build(path, node_type = nil, properties = nil)
21
31
  path = path.to_s
22
32
  node_type ||= "nt:unstructured"
33
+ properties ||= {}
23
34
  rel_path = nil
24
35
  if path.start_with?("/")
25
36
  rel_path = path.sub("/","")
@@ -30,18 +41,39 @@ module SafetyPin
30
41
  if session.root_node.has_node(rel_path)
31
42
  raise NodeError.new("Node already exists at path: #{path}")
32
43
  else
33
- self.new(session.root_node.add_node(rel_path, node_type))
44
+ node = self.new(session.root_node.add_node(rel_path, node_type))
45
+ node.properties = properties
46
+
47
+ node
34
48
  end
35
49
  rescue javax.jcr.PathNotFoundException => e
36
50
  raise NodeError.new("Cannot add a new node to a non-existing parent at #{path}")
37
51
  end
38
52
 
39
- def self.create(path, node_type = nil)
40
- node = self.build(path, node_type)
53
+ def self.create(path, node_type = nil, properties = nil)
54
+ node = self.build(path, node_type, properties)
41
55
  node.save
42
56
  node
43
57
  end
44
58
 
59
+ def self.create_parents(path)
60
+ intermediate_paths = []
61
+
62
+ current_intermediate_path = Pathname(path)
63
+ while(current_intermediate_path.to_s != "/")
64
+ current_intermediate_path = current_intermediate_path.parent
65
+ intermediate_paths.push(current_intermediate_path)
66
+ end
67
+
68
+ results = intermediate_paths.reverse.map do |intermediate_path|
69
+ create(intermediate_path) unless exists?(intermediate_path)
70
+ end
71
+
72
+ session.save
73
+
74
+ results
75
+ end
76
+
45
77
  def initialize(j_node)
46
78
  @j_node = j_node
47
79
  end
@@ -63,7 +95,7 @@ module SafetyPin
63
95
  end
64
96
 
65
97
  def child(relative_path)
66
- child_j_node = j_node.get_node(relative_path)
98
+ child_j_node = j_node.get_node(relative_path.to_s)
67
99
  Node.new(child_j_node)
68
100
  rescue javax.jcr.PathNotFoundException
69
101
  nil
@@ -140,8 +172,14 @@ module SafetyPin
140
172
  calendar_value = Calendar.instance
141
173
  calendar_value.set_time(value.to_java)
142
174
  j_node.set_property(name, calendar_value)
175
+ elsif value.is_a? Symbol
176
+ j_node.set_property(name, value.to_s)
143
177
  else
144
- j_node.set_property(name, value)
178
+ begin
179
+ j_node.set_property(name, value)
180
+ rescue NameError
181
+ raise SafetyPin::PropertyTypeError.new("Property value type of #{value.class} is unsupported")
182
+ end
145
183
  end
146
184
  end
147
185
 
@@ -202,12 +240,13 @@ module SafetyPin
202
240
  end
203
241
 
204
242
  def properties=(new_props)
205
- # props.each do |name, value|
206
- # self[name] = value
207
- # end
208
243
  property_names = (properties.keys + new_props.keys).uniq
209
244
  property_names.each do |name|
210
- self[name] = new_props[name]
245
+ if new_props[name].is_a?(Hash)
246
+ build(name, nil, new_props[name])
247
+ else
248
+ self[name] = new_props[name]
249
+ end
211
250
  end
212
251
  end
213
252
 
@@ -241,10 +280,19 @@ module SafetyPin
241
280
  self["jcr:primaryType"]
242
281
  end
243
282
 
283
+ def find_or_create(name, type = nil)
284
+ child(name) || create(name, type)
285
+ end
286
+
244
287
  # Create and return a child node with a given name
245
- def create(name, type = nil)
246
- path = Pathname(self.path) + name
247
- Node.create(path, type)
288
+ def create(name, type = nil, properties = nil)
289
+ path = Pathname(self.path) + name.to_s
290
+ Node.create(path, type, properties)
291
+ end
292
+
293
+ def build(name, type = nil, properties = nil)
294
+ path = Pathname(self.path) + name.to_s
295
+ Node.build(path, type, properties)
248
296
  end
249
297
  end
250
298
 
@@ -1,3 +1,3 @@
1
1
  module SafetyPin
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/spec/jcr_spec.rb CHANGED
@@ -1,7 +1,15 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
3
  describe SafetyPin::JCR do
4
- it "should login to a remote SafetyPin::JCR" do
4
+ before(:all) do
5
+ SafetyPin::JCR.logout
6
+ end
7
+
8
+ after(:all) do
9
+ SafetyPin::JCR.dev_login
10
+ end
11
+
12
+ it "should login to a remote JCR" do
5
13
  SafetyPin::JCR.login(:hostname => "http://localhost:4502", :username => "admin", :password => "admin")
6
14
  SafetyPin::JCR.session.should be_a(Java::JavaxJcr::Session)
7
15
  SafetyPin::JCR.should be_logged_in
@@ -1,18 +1,6 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
- describe "JCR-SQL2 example queries" do
4
- before(:all) do
5
- SafetyPin::JCR.login(:hostname => "http://localhost:4502", :username => "admin", :password => "admin")
6
- end
7
-
8
- before do
9
- SafetyPin::JCR.session.refresh(false)
10
- end
11
-
12
- after(:all) do
13
- SafetyPin::JCR.logout
14
- end
15
-
3
+ describe "JCR-SQL2 example queries" do
16
4
  before do
17
5
  @node = SafetyPin::Node.create("/content/foo")
18
6
  @node.properties = {"bar" => "baz", "qux" => "qax"}
data/spec/node_spec.rb CHANGED
@@ -1,19 +1,7 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
- describe SafetyPin::Node do
4
- before(:all) do
5
- SafetyPin::JCR.login(:hostname => "http://localhost:4502", :username => "admin", :password => "admin")
6
- end
7
-
8
- before do
9
- SafetyPin::JCR.session.refresh(false)
10
- end
11
-
12
- after(:all) do
13
- SafetyPin::JCR.logout
14
- end
15
-
16
- context ".find" do
3
+ describe SafetyPin::Node do
4
+ describe ".find" do
17
5
  context "given a node name" do
18
6
  context "that exists" do
19
7
  it "should return a node with a matching path" do
@@ -33,13 +21,40 @@ describe SafetyPin::Node do
33
21
  end
34
22
  end
35
23
 
36
- context ".session" do
24
+ describe ".find_or_create" do
25
+ context "given a node path that exists" do
26
+ it "should return the node at that path" do
27
+ SafetyPin::Node.create("/content/foo")
28
+ SafetyPin::Node.find_or_create("/content/foo").path.should eql "/content/foo"
29
+ end
30
+ end
31
+
32
+ context "give a node path that doesn't exist" do
33
+ it "should create a node at the path and return it" do
34
+ SafetyPin::Node.find("/content/foo").should be_nil
35
+ SafetyPin::Node.find_or_create("/content/foo").path.should eql "/content/foo"
36
+ end
37
+ end
38
+ end
39
+
40
+ describe ".exists?" do
41
+ it "returns true if node exists at path" do
42
+ SafetyPin::Node.create("/content/foo")
43
+ SafetyPin::Node.exists?("/content/foo").should be_true
44
+ end
45
+
46
+ it "returns false if node does not exist" do
47
+ SafetyPin::Node.exists?("/content/foo").should be_false
48
+ end
49
+ end
50
+
51
+ describe ".session" do
37
52
  it "should return a session" do
38
53
  SafetyPin::Node.session.should be_a(Java::JavaxJcr::Session)
39
54
  end
40
55
  end
41
56
 
42
- context "#session" do
57
+ describe "#session" do
43
58
  it "should return a session" do
44
59
  SafetyPin::Node.find("/content").session.should be_a(Java::JavaxJcr::Session)
45
60
  end
@@ -51,13 +66,13 @@ describe SafetyPin::Node do
51
66
  end
52
67
  end
53
68
 
54
- context "#children" do
69
+ describe "#children" do
55
70
  it "should return an array of child nodes" do
56
71
  SafetyPin::Node.find("/content").children.first.should be_a(SafetyPin::Node)
57
72
  end
58
73
  end
59
74
 
60
- context "#child" do
75
+ describe "#child" do
61
76
  context "given a node name" do
62
77
  let(:node) { SafetyPin::Node.find("/") }
63
78
 
@@ -69,9 +84,12 @@ describe SafetyPin::Node do
69
84
  it "should return a grandchild node given a relative path" do
70
85
  SafetyPin::Node.create("/content/foo")
71
86
  node.child("content/foo").name.should eql("foo")
72
- SafetyPin::Node.find("/content/foo").destroy
73
87
  end
74
88
  end
89
+
90
+ it "should coerce non-string name to string and return child" do
91
+ node.child(:content).name.should == "content"
92
+ end
75
93
 
76
94
  context "that doesn't exist" do
77
95
  it "should return nil" do
@@ -81,7 +99,24 @@ describe SafetyPin::Node do
81
99
  end
82
100
  end
83
101
 
84
- context "#name" do
102
+ describe "#find_or_create_child" do
103
+ let(:parent) { SafetyPin::Node.create("/content/foo") }
104
+
105
+ context "an existing node path" do
106
+ it "should return the child node" do
107
+ parent.create("bar")
108
+ parent.find_or_create("bar").path.should eql "/content/foo/bar"
109
+ end
110
+ end
111
+
112
+ context "a non-existing node path" do
113
+ it "should create a node and return it" do
114
+ parent.find_or_create("bar").path.should eql "/content/foo/bar"
115
+ end
116
+ end
117
+ end
118
+
119
+ describe "#name" do
85
120
  it "should return a string name" do
86
121
  SafetyPin::Node.find("/content").name.should eql("content")
87
122
  end
@@ -93,8 +128,6 @@ describe SafetyPin::Node do
93
128
  @node = SafetyPin::Node.create("/content/foo")
94
129
  @node["bar"] = "baz"
95
130
  end
96
-
97
- after { @node.destroy }
98
131
 
99
132
  it "should save the changes to the JCR" do
100
133
  @node.save
@@ -112,7 +145,6 @@ describe SafetyPin::Node do
112
145
  it "should save the node" do
113
146
  node = SafetyPin::Node.build("/content/foo")
114
147
  node.save.should be_true
115
- node.destroy
116
148
  end
117
149
 
118
150
  it "should save changes in parent node" do
@@ -122,54 +154,51 @@ describe SafetyPin::Node do
122
154
  parent_node.should be_changed
123
155
  node.save
124
156
  parent_node.should_not be_changed
125
- node.destroy
126
- parent_node.destroy
127
157
  end
128
158
  end
129
159
  end
130
160
 
131
161
  describe "#read_attribute" do
132
162
  context "on an existing node" do
133
- before { @node = SafetyPin::Node.create("/content/foo") }
134
- after { @node.destroy }
163
+ let(:node) { SafetyPin::Node.create("/content/foo") }
135
164
 
136
165
  it "should return the string value of a string property" do
137
- @node["foo"] = "bar"
138
- @node.read_attribute("foo").should eql("bar")
166
+ node["foo"] = "bar"
167
+ node.read_attribute("foo").should eql("bar")
139
168
  end
140
169
 
141
170
  it "should return the boolean value of a boolean property" do
142
- @node["foo"] = true
143
- @node.read_attribute("foo").should eql(true)
171
+ node["foo"] = true
172
+ node.read_attribute("foo").should eql(true)
144
173
  end
145
174
 
146
175
  it "should return the double value of a double (or Ruby float) property" do
147
- @node["foo"] = 3.14
148
- @node.read_attribute("foo").should eql(3.14)
176
+ node["foo"] = 3.14
177
+ node.read_attribute("foo").should eql(3.14)
149
178
  end
150
179
 
151
180
  it "should return the long value of a long (or Ruby Fixnum) property" do
152
- @node["foo"] = 42
153
- @node.read_attribute("foo").should eql(42)
181
+ node["foo"] = 42
182
+ node.read_attribute("foo").should eql(42)
154
183
  end
155
184
 
156
185
  it "should return the time value of a date property" do
157
186
  time = Time.now
158
- @node["foo"] = time
159
- @node.read_attribute("foo").to_s.should eql(time.to_s)
187
+ node["foo"] = time
188
+ node.read_attribute("foo").to_s.should eql(time.to_s)
160
189
  end
161
190
 
162
191
  context "given a multi-value property" do
163
192
  it "should return an array of values" do
164
- @node["foo"] = ["one", "two"]
165
- @node.read_attribute("foo").should eql(["one", "two"])
193
+ node["foo"] = ["one", "two"]
194
+ node.read_attribute("foo").should eql(["one", "two"])
166
195
  end
167
196
  end
168
197
 
169
198
  context "given a non-string name" do
170
199
  it "should co-erce the name into a string and retrieve the property" do
171
- @node["foo"] = "bar"
172
- @node.read_attribute(:foo).should eql("bar")
200
+ node["foo"] = "bar"
201
+ node.read_attribute(:foo).should eql("bar")
173
202
  end
174
203
  end
175
204
  end
@@ -184,33 +213,58 @@ describe SafetyPin::Node do
184
213
  end
185
214
 
186
215
  context "#write_attribute" do
187
- before { @node = SafetyPin::Node.create("/content/foo") }
188
- after { @node.destroy }
216
+ let(:node) { SafetyPin::Node.create("/content/foo") }
189
217
 
190
218
  context "given a single value" do
191
219
  it "should set a string property value" do
192
- @node.write_attribute("foo", "bar")
193
- @node.save
194
- @node.reload
195
- @node["foo"].should eql("bar")
220
+ node.write_attribute("foo", "bar")
221
+ node.save
222
+ node.reload
223
+ node["foo"].should eql("bar")
196
224
  end
197
225
 
198
- context "given a Time object" do
226
+ context "given a Time object value" do
199
227
  it "should set a date property value" do
200
228
  time = Time.now
201
- @node.write_attribute("foo", time)
202
- @node.save
203
- @node.reload
204
- @node["foo"].to_s.should eql(time.to_s)
229
+ node.write_attribute("foo", time)
230
+ node.save
231
+ node.reload
232
+ node["foo"].to_s.should eql(time.to_s)
233
+ end
234
+ end
235
+
236
+ context "given a symbol value" do
237
+ it "coerces value to string" do
238
+ node.write_attribute("foo", :bar)
239
+ node["foo"].should == "bar"
240
+ end
241
+ end
242
+
243
+ context "given another supported value" do
244
+ it "sets the property" do
245
+ node.write_attribute("foo", 1)
246
+ node["foo"].should == 1
247
+ node.write_attribute("foo", 1.1)
248
+ node["foo"].should == 1.1
249
+ node.write_attribute("foo", true)
250
+ node["foo"].should == true
251
+ node.write_attribute("foo", "bar")
252
+ node["foo"].should == "bar"
253
+ end
254
+ end
255
+
256
+ context "given an unsupported value" do
257
+ it "raises an error" do
258
+ lambda { node.write_attribute("foo", {unsupported: :value_type}) }.should raise_error(SafetyPin::PropertyTypeError)
205
259
  end
206
260
  end
207
261
 
208
262
  context "given a non-string name" do
209
263
  it "should co-erce name into string before setting property" do
210
- @node.write_attribute(:foo, "bar")
211
- @node.save
212
- @node.reload
213
- @node["foo"].should eql("bar")
264
+ node.write_attribute(:foo, "bar")
265
+ node.save
266
+ node.reload
267
+ node["foo"].should eql("bar")
214
268
  end
215
269
  end
216
270
  end
@@ -218,51 +272,50 @@ describe SafetyPin::Node do
218
272
  context "given an array of values" do
219
273
  context "of the same type" do
220
274
  it "should set a multi-value string array" do
221
- @node.write_attribute("foo", ["one", "two"])
222
- @node.save
223
- @node.reload
224
- @node["foo"].should eql(["one", "two"])
275
+ node.write_attribute("foo", ["one", "two"])
276
+ node.save
277
+ node.reload
278
+ node["foo"].should eql(["one", "two"])
225
279
  end
226
280
  end
227
281
  end
228
282
 
229
283
  context "given a null value" do
230
284
  it "should remove the property" do
231
- @node["foo"] = "bar"
232
- @node.write_attribute("foo", nil)
233
- lambda { @node["foo"] }.should raise_error(SafetyPin::NilPropertyError)
285
+ node["foo"] = "bar"
286
+ node.write_attribute("foo", nil)
287
+ lambda { node["foo"] }.should raise_error(SafetyPin::NilPropertyError)
234
288
  end
235
289
 
236
290
  context "given a non-existent property and a null value" do
237
291
  it "should return nil" do
238
- @node.write_attribute("foo", nil).should be_nil
292
+ node.write_attribute("foo", nil).should be_nil
239
293
  end
240
294
  end
241
295
  end
242
296
 
243
297
  context "changing jcr:primaryType property" do
244
298
  it "should raise an error" do
245
- lambda { @node.write_attribute("jcr:primaryType", "nt:folder") }.should raise_error(SafetyPin::PropertyError)
299
+ lambda { node.write_attribute("jcr:primaryType", "nt:folder") }.should raise_error(SafetyPin::PropertyError)
246
300
  end
247
301
  end
248
302
  end
249
303
 
250
304
  context "#reload" do
251
- before { @node = SafetyPin::Node.create("/content/foo") }
252
- after { @node.destroy }
305
+ let(:node) { SafetyPin::Node.create("/content/foo") }
253
306
 
254
307
  it "should discard pending changes" do
255
- @node["foo"] = "bar"
256
- @node.reload
257
- lambda { @node.read_attribute("foo") }.should raise_error(SafetyPin::NilPropertyError)
308
+ node["foo"] = "bar"
309
+ node.reload
310
+ lambda { node.read_attribute("foo") }.should raise_error(SafetyPin::NilPropertyError)
258
311
  end
259
312
 
260
313
  it "should not discard changes for another node" do
261
- @node["bar"] = "baz"
314
+ node["bar"] = "baz"
262
315
  another_node = SafetyPin::Node.find("/content")
263
316
  another_node["bar"] = "baz"
264
- @node.reload
265
- lambda { @node["bar"] }.should raise_error(SafetyPin::NilPropertyError)
317
+ node.reload
318
+ lambda { node["bar"] }.should raise_error(SafetyPin::NilPropertyError)
266
319
  another_node["bar"].should eql("baz")
267
320
  end
268
321
  end
@@ -273,7 +326,6 @@ describe SafetyPin::Node do
273
326
  node.write_attribute("bar","baz")
274
327
  node.save
275
328
  node["bar"].should eql("baz")
276
- node.destroy
277
329
  end
278
330
  end
279
331
 
@@ -317,18 +369,22 @@ describe SafetyPin::Node do
317
369
  end
318
370
 
319
371
  describe "#properties=" do
320
- before { @node = SafetyPin::Node.create("/content/foo") }
321
- after { @node.destroy }
372
+ let(:node) { SafetyPin::Node.create("/content/foo") }
322
373
 
323
374
  it "should set the properties of a node" do
324
- @node.properties = {"foo" => "bar"}
325
- @node.properties.should eql({"foo" => "bar"})
375
+ node.properties = {"foo" => "bar"}
376
+ node.properties.should == {"foo" => "bar"}
326
377
  end
327
378
 
328
379
  it "should set unset properties not specified in hash" do
329
- @node["foo"] = "bar"
330
- @node.properties = {"baz" => "qux"}
331
- @node.properties.should eql({"baz" => "qux"})
380
+ node["foo"] = "bar"
381
+ node.properties = {"baz" => "qux"}
382
+ node.properties.should eql({"baz" => "qux"})
383
+ end
384
+
385
+ it "should create child nodes for hash values" do
386
+ node.properties = {foo: {bar: "baz"}}
387
+ node.child(:foo).properties.should == {"bar" => "baz"}
332
388
  end
333
389
  end
334
390
 
@@ -339,55 +395,48 @@ describe SafetyPin::Node do
339
395
  end
340
396
 
341
397
  describe "#mixin_types" do
342
- before do
343
- @node = SafetyPin::Node.create("/content/foo")
344
- @node.j_node.add_mixin("mix:created")
345
- @node.save
346
- end
347
-
348
- after { @node.destroy }
349
-
350
398
  it "should return the mixin types of a node" do
351
- @node.mixin_types.should eql(["mix:created"])
399
+ node = SafetyPin::Node.create("/content/foo")
400
+ node.j_node.add_mixin("mix:created")
401
+ node.save
402
+ node.mixin_types.should eql(["mix:created"])
352
403
  end
353
404
  end
354
405
 
355
406
  describe "#add_mixin" do
356
- before { @node = SafetyPin::Node.create("/content/foo") }
357
- after { @node.destroy }
407
+ let(:node) { SafetyPin::Node.create("/content/foo") }
358
408
 
359
409
  it "should add a mixin type to node" do
360
- @node.add_mixin("mix:created")
361
- @node.save
362
- @node.mixin_types.should eql(["mix:created"])
410
+ node.add_mixin("mix:created")
411
+ node.save
412
+ node.mixin_types.should eql(["mix:created"])
363
413
  end
364
414
 
365
415
  it "should require a save before the mixin addition is detected" do
366
- @node.add_mixin("mix:created")
367
- @node.mixin_types.should eql([])
416
+ node.add_mixin("mix:created")
417
+ node.mixin_types.should eql([])
368
418
  end
369
419
  end
370
420
 
371
421
  describe "#remove_mixin" do
372
- before do
373
- @node = SafetyPin::Node.create("/content/foo")
374
- @node.add_mixin("mix:created")
375
- @node.save
422
+ let(:node) do
423
+ node = SafetyPin::Node.create("/content/foo")
424
+ node.add_mixin("mix:created")
425
+ node.save
426
+ node
376
427
  end
377
428
 
378
- after { @node.destroy }
379
-
380
429
  it "should remove a mixin type from a node" do
381
- @node.mixin_types.should eql(["mix:created"])
382
- @node.remove_mixin("mix:created")
383
- @node.save
384
- @node.mixin_types.should eql([])
430
+ node.mixin_types.should eql(["mix:created"])
431
+ node.remove_mixin("mix:created")
432
+ node.save
433
+ node.mixin_types.should eql([])
385
434
  end
386
435
 
387
436
  it "should require a save before the mixin removal is detected" do
388
- @node.remove_mixin("mix:created")
389
- @node.mixin_types.should eql(["mix:created"])
390
- @node.reload
437
+ node.remove_mixin("mix:created")
438
+ node.mixin_types.should eql(["mix:created"])
439
+ node.reload
391
440
  end
392
441
  end
393
442
 
@@ -398,6 +447,15 @@ describe SafetyPin::Node do
398
447
  node.should be_new
399
448
  node.properties.should eql({})
400
449
  end
450
+
451
+
452
+
453
+ context "given properties" do
454
+ it "should build a node with properties" do
455
+ node = SafetyPin::Node.build("/content/foo", nil, {foo: "bar"})
456
+ node.properties.should == {"foo" => "bar"}
457
+ end
458
+ end
401
459
 
402
460
  context "and a node type string" do
403
461
  it "should create an unsaved node of the given type" do
@@ -446,10 +504,16 @@ describe SafetyPin::Node do
446
504
  it "should build and save a node of the specified type" do
447
505
  node = SafetyPin::Node.create("/content/foo", "nt:folder")
448
506
  SafetyPin::Node.find("/content/foo").should_not be_nil
449
- node.destroy
450
507
  end
451
508
  end
452
509
  end
510
+
511
+ describe ".create_parents" do
512
+ it "creates parent nodes if they do not exist" do
513
+ SafetyPin::Node.create_parents("/content/foo/bar/baz")
514
+ SafetyPin::Node.create("/content/foo/bar/baz").should_not be_nil
515
+ end
516
+ end
453
517
 
454
518
  context "#value_factory" do
455
519
  it "should return a value factory instance" do
@@ -464,7 +528,6 @@ describe SafetyPin::Node do
464
528
  node.save
465
529
  property = node.j_node.get_property("bar")
466
530
  node.property_is_multi_valued?(property).should be_true
467
- node.destroy
468
531
  end
469
532
 
470
533
  it "should return false if property is not multi-valued" do
@@ -473,7 +536,6 @@ describe SafetyPin::Node do
473
536
  node.save
474
537
  property = node.j_node.get_property("bar")
475
538
  node.property_is_multi_valued?(property).should be_false
476
- node.destroy
477
539
  end
478
540
  end
479
541
 
@@ -492,48 +554,64 @@ describe SafetyPin::Node do
492
554
  parent_node.should be_changed
493
555
  node.destroy
494
556
  parent_node.should_not be_changed
495
- parent_node.destroy
496
557
  end
497
558
 
498
- context "when it fails" do
499
- before do
500
- @node = SafetyPin::Node.create("/content/foo")
501
- @node.add_mixin("mix:created")
502
- @node.save
503
- end
504
-
505
- after { @node.reload; @node.destroy }
506
-
559
+ context "when it fails" do
507
560
  it "should raise an error" do
508
- @node.remove_mixin("mix:created") # make node unremoveable
509
- lambda { @node.destroy }.should raise_error(SafetyPin::NodeError)
561
+ node = SafetyPin::Node.create("/content/foo")
562
+ node.add_mixin("mix:created")
563
+ node.save
564
+ node.remove_mixin("mix:created") # make node unremoveable
565
+ lambda { node.destroy }.should raise_error(SafetyPin::NodeError)
510
566
  end
511
567
  end
512
568
  end
513
569
 
514
570
  describe "#primary_type" do
515
- before { @node = SafetyPin::Node.create("/content/foo") }
516
- after { @node.destroy }
571
+ let(:node) { SafetyPin::Node.create("/content/foo") }
517
572
 
518
573
  it "should return the primary type of the node" do
519
- @node.primary_type.should eql("nt:unstructured")
574
+ node.primary_type.should eql("nt:unstructured")
520
575
  end
521
576
  end
522
577
 
523
- describe "#create" do
524
- before { @node = SafetyPin::Node.create("/content/foo") }
525
- after { @node.destroy }
578
+ describe "#build" do
579
+ let(:node) { SafetyPin::Node.create("/content/foo") }
526
580
 
527
581
  it "should create a child node with a given name" do
528
- @node.create("bar")
529
- SafetyPin::Node.find("/content/foo/bar").should be_a(SafetyPin::Node)
582
+ node.build("bar").path.should == "/content/foo/bar"
530
583
  end
531
584
 
532
585
  it "should create a child node with a given name and node type" do
533
- @node.create("bar", "nt:folder")
534
- child_node = SafetyPin::Node.find("/content/foo/bar")
586
+ child_node = node.build("bar", "nt:folder")
535
587
  child_node.should be_a(SafetyPin::Node)
536
588
  child_node.primary_type.should eql("nt:folder")
537
589
  end
590
+
591
+ it "should create a child node with a name, node type, and properties" do
592
+ node.build("bar", nil, {foo: "bar"}).properties.should == {"foo" => "bar"}
593
+ end
594
+ end
595
+
596
+ describe "#create" do
597
+ let(:node) { SafetyPin::Node.create("/content/foo") }
598
+
599
+ it "should create a child node with a given name" do
600
+ child_node = node.create("bar")
601
+ child_node.path.should == "/content/foo/bar"
602
+ child_node.should_not
603
+ end
604
+
605
+ it "should create a child node with a given name and node type" do
606
+ child_node = node.create("bar", "nt:folder")
607
+ child_node.should_not be_new
608
+ child_node.primary_type.should eql("nt:folder")
609
+ end
610
+
611
+ it "should create a child node with a name, node type, and properties" do
612
+ child_node = node.create("bar", nil, {foo: "bar"})
613
+ child_node.should_not be_new
614
+ child_node.properties.should == {"foo" => "bar"}
615
+ end
538
616
  end
539
617
  end
data/spec/query_spec.rb CHANGED
@@ -1,18 +1,6 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
- describe SafetyPin::Query do
4
- before(:all) do
5
- SafetyPin::JCR.login(:hostname => "http://localhost:4502", :username => "admin", :password => "admin")
6
- end
7
-
8
- before do
9
- SafetyPin::JCR.session.refresh(false)
10
- end
11
-
12
- after(:all) do
13
- SafetyPin::JCR.logout
14
- end
15
-
3
+ describe SafetyPin::Query do
16
4
  let(:query) { SafetyPin::Query.new }
17
5
 
18
6
  describe ".execute" do
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,27 @@
1
1
  $:<<File.join(File.dirname(__FILE__), '/../lib')
2
2
 
3
- require 'safety_pin'
3
+ require 'safety_pin'
4
+
5
+ RSpec.configure do |config|
6
+ config.before(:all) do
7
+ SafetyPin::JCR.dev_login
8
+ end
9
+
10
+ config.before do
11
+ SafetyPin::JCR.session.refresh(false) if SafetyPin::JCR.logged_in?
12
+ end
13
+
14
+ config.after(:all) do
15
+ SafetyPin::JCR.logout
16
+ end
17
+
18
+ config.after do
19
+ if SafetyPin::JCR.logged_in?
20
+ node = SafetyPin::Node.find("/content/foo")
21
+ unless node.nil?
22
+ node.reload if node.changed?
23
+ node.destroy
24
+ end
25
+ end
26
+ end
27
+ end
metadata CHANGED
@@ -1,79 +1,75 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: safety_pin
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.5
6
6
  platform: ruby
7
- authors:
8
- - Jordan Raine
9
- autorequire:
7
+ authors:
8
+ - Jordan Raine
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-05-18 00:00:00 Z
12
+ date: 2012-08-23 00:00:00.000000000 Z
14
13
  dependencies: []
15
-
16
14
  description: An easy-to-use JCR connector for JRuby
17
- email:
18
- - jnraine@gmail.com
19
- executables: []
20
-
15
+ email:
16
+ - jnraine@gmail.com
17
+ executables:
18
+ - console
21
19
  extensions: []
22
-
23
20
  extra_rdoc_files: []
24
-
25
- files:
26
- - .rspec
27
- - .rvmrc
28
- - Gemfile
29
- - Gemfile.lock
30
- - LICENSE
31
- - README.md
32
- - Rakefile
33
- - lib/safety_pin.rb
34
- - lib/safety_pin/jcr.rb
35
- - lib/safety_pin/node.rb
36
- - lib/safety_pin/query.rb
37
- - lib/safety_pin/query/where_condition.rb
38
- - lib/safety_pin/version.rb
39
- - safety_pin.gemspec
40
- - spec/jcr_spec.rb
41
- - spec/jcr_sql2_spec.rb
42
- - spec/node_spec.rb
43
- - spec/query_spec.rb
44
- - spec/spec_helper.rb
45
- - spec/where_condition_spec.rb
46
- homepage: ""
21
+ files:
22
+ - .rspec
23
+ - .rvmrc
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - console_loader.rb
31
+ - lib/safety_pin.rb
32
+ - lib/safety_pin/jcr.rb
33
+ - lib/safety_pin/node.rb
34
+ - lib/safety_pin/query.rb
35
+ - lib/safety_pin/query/where_condition.rb
36
+ - lib/safety_pin/version.rb
37
+ - safety_pin.gemspec
38
+ - spec/jcr_spec.rb
39
+ - spec/jcr_sql2_spec.rb
40
+ - spec/node_spec.rb
41
+ - spec/query_spec.rb
42
+ - spec/spec_helper.rb
43
+ - spec/where_condition_spec.rb
44
+ homepage: ''
47
45
  licenses: []
48
-
49
- post_install_message:
46
+ post_install_message:
50
47
  rdoc_options: []
51
-
52
- require_paths:
53
- - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
55
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- version: "0"
60
- required_rubygems_version: !ruby/object:Gem::Requirement
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
61
  none: false
62
- requirements:
63
- - - ">="
64
- - !ruby/object:Gem::Version
65
- version: "0"
66
62
  requirements: []
67
-
68
- rubyforge_project:
69
- rubygems_version: 1.8.15
70
- signing_key:
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.24
65
+ signing_key:
71
66
  specification_version: 3
72
67
  summary: JCR connector for JRuby
73
- test_files:
74
- - spec/jcr_spec.rb
75
- - spec/jcr_sql2_spec.rb
76
- - spec/node_spec.rb
77
- - spec/query_spec.rb
78
- - spec/spec_helper.rb
79
- - spec/where_condition_spec.rb
68
+ test_files:
69
+ - spec/jcr_spec.rb
70
+ - spec/jcr_sql2_spec.rb
71
+ - spec/node_spec.rb
72
+ - spec/query_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/where_condition_spec.rb
75
+ ...