safety-pin 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,7 +36,7 @@ module SafetyPin
36
36
  end
37
37
 
38
38
  def self.dev_login
39
- login(:hostname => "http://localhost:4502", :username => "admin", :password => "cq4me")
39
+ login(:hostname => "http://localhost:4502", :username => ENV["JCR_USERNAME"], :password => ENV["JCR_PASSWORD"])
40
40
  end
41
41
  end
42
42
  end
@@ -28,7 +28,13 @@ module SafetyPin
28
28
  JCR.session
29
29
  end
30
30
 
31
- def self.build(node_blueprint)
31
+ def self.build(node_blueprint_or_path_string)
32
+ if node_blueprint_or_path_string.respond_to?(:primary_type, :properties)
33
+ node_blueprint = node_blueprint_or_path_string
34
+ else
35
+ node_blueprint = NodeBlueprint.new(path: node_blueprint_or_path_string)
36
+ end
37
+
32
38
  raise NodeError.new("NodeBlueprint is nil") if node_blueprint.nil?
33
39
  raise NodeError.new("NodeBlueprint has non-absolute path") unless node_blueprint.path.to_s.start_with?("/")
34
40
  raise NodeError.new("Node already exists at path: #{node_blueprint.path}") if Node.exists?(node_blueprint.path)
@@ -51,8 +57,8 @@ module SafetyPin
51
57
  node
52
58
  end
53
59
 
54
- def self.create(node_blueprint)
55
- node = self.build(node_blueprint)
60
+ def self.create(node_blueprint_or_path_string)
61
+ node = self.build(node_blueprint_or_path_string)
56
62
  node.save
57
63
  node
58
64
  end
@@ -116,6 +122,10 @@ module SafetyPin
116
122
  end
117
123
  child_nodes
118
124
  end
125
+
126
+ def descendants
127
+ children.map {|child| [child, child.descendants] }.flatten
128
+ end
119
129
 
120
130
  def child(relative_path)
121
131
  child_j_node = j_node.get_node(relative_path.to_s)
@@ -170,11 +180,25 @@ module SafetyPin
170
180
  Time.at(value.date.time.time / 1000)
171
181
  when "Name"
172
182
  value.string # Not sure if these should be handled differently
183
+ when "Binary"
184
+ value.binary
185
+ when "Decimal"
186
+ value.decimal
187
+ when "Path"
188
+ Pathname(value.string)
173
189
  else
174
190
  raise PropertyTypeError.new("Unknown property type: #{property_type}")
175
191
  end
176
192
  end
177
193
 
194
+ def property_single_valued?(name)
195
+ properties.has_key?(name) and !property_multi_valued?(name)
196
+ end
197
+
198
+ def property_multi_valued?(name)
199
+ properties.has_key?(name) and properties[name].is_a?(Array)
200
+ end
201
+
178
202
  def write_attribute(name, value)
179
203
  raise PropertyError.new("Illegal operation: cannot change jcr:primaryType property") if name == "jcr:primaryType"
180
204
  name = name.to_s
@@ -182,8 +206,16 @@ module SafetyPin
182
206
  if value.nil? and not j_node.has_property(name)
183
207
  return nil
184
208
  end
209
+
210
+ # when going from multi to single value
211
+ if property_multi_valued?(name) and !value.is_a?(Array)
212
+ value = [value]
213
+ end
185
214
 
186
215
  if value.is_a? Array
216
+ if properties.has_key?(name) and property_single_valued?(name) # when going from single to multi value
217
+ j_node.set_property(name, nil)
218
+ end
187
219
  values = value
188
220
  val_fact = value_factory
189
221
  j_values = []
@@ -195,7 +227,7 @@ module SafetyPin
195
227
  calendar_value = Calendar.instance
196
228
  calendar_value.set_time(value.to_java)
197
229
  j_node.set_property(name, calendar_value)
198
- elsif value.is_a? Symbol
230
+ elsif value.is_a?(Symbol) or value.is_a?(Pathname)
199
231
  j_node.set_property(name, value.to_s)
200
232
  else
201
233
  begin
@@ -384,6 +416,10 @@ module SafetyPin
384
416
  def has_property(name)
385
417
  properties.keys.include?(name)
386
418
  end
419
+
420
+ def inspect
421
+ "#<#{self.class} path=#{path}>"
422
+ end
387
423
  end
388
424
 
389
425
  class NodeError < Exception; end
@@ -1,3 +1,3 @@
1
1
  module SafetyPin
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/spec/jcr_spec.rb CHANGED
@@ -10,14 +10,14 @@ describe SafetyPin::JCR do
10
10
  end
11
11
 
12
12
  it "should login to a remote JCR" do
13
- SafetyPin::JCR.login(:hostname => "http://localhost:4502", :username => "admin", :password => "admin")
13
+ SafetyPin::JCR.login(:hostname => "http://localhost:4502", :username => ENV["JCR_USERNAME"], :password => ENV["JCR_PASSWORD"])
14
14
  SafetyPin::JCR.session.should be_a(Java::JavaxJcr::Session)
15
15
  SafetyPin::JCR.should be_logged_in
16
16
  SafetyPin::JCR.logout
17
17
  end
18
18
 
19
19
  it "should logout of a remote SafetyPin::JCR" do
20
- SafetyPin::JCR.login(:hostname => "http://localhost:4502", :username => "admin", :password => "admin")
20
+ SafetyPin::JCR.login(:hostname => "http://localhost:4502", :username => ENV["JCR_USERNAME"], :password => ENV["JCR_PASSWORD"])
21
21
  SafetyPin::JCR.logout
22
22
  SafetyPin::JCR.should be_logged_out
23
23
  end
@@ -2,6 +2,8 @@ require 'spec_helper.rb'
2
2
 
3
3
  describe "JCR-SQL2 example queries" do
4
4
  before do
5
+ lingering_node = SafetyPin::Node.find("/content/foo")
6
+ lingering_node.destroy unless lingering_node.nil?
5
7
  @node = SafetyPin::Node.create("/content/foo")
6
8
  @node.properties = {"bar" => "baz", "qux" => "qax"}
7
9
  @node.save
@@ -46,6 +48,7 @@ describe "JCR-SQL2 example queries" do
46
48
  end
47
49
 
48
50
  it "can lookup a node based on node type" do
51
+ pending "too slow"
49
52
  nodes = SafetyPin::Query.execute("SELECT * FROM [cq:Page]")
50
53
  nodes.first.primary_type.should eql("cq:Page")
51
54
  end
data/spec/node_spec.rb CHANGED
@@ -266,6 +266,13 @@ describe SafetyPin::Node do
266
266
  end
267
267
  end
268
268
 
269
+ context "given a pathname value" do
270
+ it "coers value to string" do
271
+ node.write_attribute("foo", Pathname("/content/foo"))
272
+ node["foo"].should == "/content/foo"
273
+ end
274
+ end
275
+
269
276
  context "given another supported value" do
270
277
  it "sets the property" do
271
278
  node.write_attribute("foo", 1)
@@ -293,6 +300,30 @@ describe SafetyPin::Node do
293
300
  node["foo"].should eql("bar")
294
301
  end
295
302
  end
303
+
304
+ context "when changing a property from a single value to a multivalue" do
305
+ it "should work" do
306
+ node.write_attribute(:foo, "bar")
307
+ node.save
308
+ node.reload
309
+ node.write_attribute(:foo, ["bar"])
310
+ node.save
311
+ node.reload
312
+ node["foo"].should == ["bar"]
313
+ end
314
+ end
315
+
316
+ context "when changing a property from a multi value to a single value" do
317
+ it "wraps the value in an array (can't go from multi to single" do
318
+ node.write_attribute("foo", ["bar"])
319
+ node.save
320
+ node.reload
321
+ node.write_attribute("foo", "not bar")
322
+ node.save
323
+ node.reload
324
+ node["foo"].should == ["not bar"]
325
+ end
326
+ end
296
327
  end
297
328
 
298
329
  context "given an array of values" do
@@ -761,4 +792,19 @@ describe SafetyPin::Node do
761
792
  node["bar"].should_not == "BAZ"
762
793
  end
763
794
  end
795
+
796
+ describe "#descendants" do
797
+ before do
798
+ SafetyPin::Node.create(SafetyPin::NodeBlueprint.new(path: "/content/foo"))
799
+ SafetyPin::Node.create(SafetyPin::NodeBlueprint.new(path: "/content/foo/bar"))
800
+ SafetyPin::Node.create(SafetyPin::NodeBlueprint.new(path: "/content/foo/bar/baz"))
801
+ SafetyPin::Node.create(SafetyPin::NodeBlueprint.new(path: "/content/foo/bar/baz/qux"))
802
+ SafetyPin::Node.create(SafetyPin::NodeBlueprint.new(path: "/content/foo/baz"))
803
+ SafetyPin::Node.create(SafetyPin::NodeBlueprint.new(path: "/content/foo/baz/qux"))
804
+ end
805
+
806
+ it "returns all nodes beneath this one" do
807
+ SafetyPin::Node.find("/content/foo").descendants.should have(5).items
808
+ end
809
+ end
764
810
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: safety-pin
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jordan Raine
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-13 00:00:00.000000000 Z
12
+ date: 2013-06-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: An easy-to-use JCR connector for JRuby
15
15
  email: