safety-pin 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ jruby-1.7.0-rc2
data/README.md CHANGED
@@ -24,22 +24,63 @@ Or install it yourself as:
24
24
 
25
25
  You can interact with the JCR using Ruby code in ways you would expect:
26
26
 
27
+ ### Getting Started
28
+
27
29
  ```ruby
28
30
  require 'safety-pin'
29
31
  include SafetyPin
30
32
 
33
+ # Login to a JCR
34
+ JCR.login(hostname: "http://localhost:4502", username: "admin", password: "admin")
35
+
36
+ ```
37
+
38
+ ### Navigating
39
+
40
+ ```ruby
31
41
  node = Node.find("/content") # returns node at path
32
42
  node.child("foo") # returns child by name
33
43
  node.children # returns array of children
44
+ node.parent # returns parent node
45
+ ```
46
+
47
+ ### Properties
34
48
 
49
+ ```ruby
50
+ node = Node.find("/content")
35
51
  node.properties # returns hash of unprotected properties
36
52
  node.properties = {"bar" => "baz"} # replaces unprotected properties
37
53
 
38
54
  node["foo"] # returns value of property
39
55
  node["foo"] = "bar" # assigns value to property
56
+ ```
57
+
58
+ ### Saving to JCR
59
+
60
+ ```ruby
61
+ node = Node.find("/content")
62
+ node["foo"] = "Hello JCR" # set value in session
63
+ node.save # persisted changes to JCR
64
+
65
+ node["foo"] = "baz"
66
+ node["foo"] # => "baz"
67
+ node.refresh # reloads node from JCR
68
+ node["foo"] # => "Hello JCR"
69
+ ```
70
+
71
+ ### Mass-assign Property Values
72
+
73
+ ```ruby
74
+ node = Node.find("/content")
75
+ node["foo"] # => "bar"
76
+ node.children.first["foo"] # => "child foo"
77
+
78
+ node.replace_property name: "foo", target: /.+/, replacement: "new value", recursive: true
79
+
80
+ node["foo"] # => "new value"
81
+ node.children.first["foo"] # => "new value"
40
82
 
41
- node.save # saves changes to session
42
- node.refresh # reloads node from JCR
83
+ node.save # persist all changes to JCR
43
84
  ```
44
85
 
45
86
  ## Interactive Shell
@@ -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 => "admin")
39
+ login(:hostname => "http://localhost:4502", :username => "admin", :password => "cq4me")
40
40
  end
41
41
  end
42
42
  end
@@ -360,18 +360,22 @@ module SafetyPin
360
360
  NodeBlueprint.new(:path => path.to_s, :properties => properties, :primary_type => primary_type)
361
361
  end
362
362
 
363
- def replace_property(opts)
363
+ def replace_property(opts, &block)
364
364
  opts = {recursive: false}.merge(opts)
365
- name = opts.fetch(:name)
366
- target = opts.fetch(:target)
367
- replacement = opts.fetch(:replacement)
365
+ name_pattern = opts.fetch(:name)
366
+ target_pattern = opts.fetch(:target)
367
+ replacement_block = block || lambda {|value| opts.fetch(:replacement) }
368
368
 
369
- modified_nodes = []
370
- if has_property(name) and self[name].match(target)
371
- self[name] = replacement
372
- modified_nodes << self
369
+ modified = false
370
+ properties.each do |name, value|
371
+ if name.match(name_pattern) and value.match(target_pattern)
372
+ self[name] = replacement_block.call(value)
373
+ modified = true
374
+ end
373
375
  end
374
376
 
377
+ modified_nodes = []
378
+ modified_nodes << self if modified
375
379
  modified_nodes << children.map {|child_node| child_node.replace_property(opts) } if opts.fetch(:recursive)
376
380
 
377
381
  modified_nodes.flatten
@@ -1,3 +1,3 @@
1
1
  module SafetyPin
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -709,4 +709,56 @@ describe SafetyPin::Node do
709
709
  expect { SafetyPin::Node.find("/").parent }.to raise_error(SafetyPin::NodeError)
710
710
  end
711
711
  end
712
+
713
+ describe "#replace_property" do
714
+ let(:node) do
715
+ properties = {"bar" => "baz", "another" => "just for completeness", "barbell" => "for regex detail"}
716
+ blueprint = SafetyPin::NodeBlueprint.new(:path => "/content/foo", properties: properties)
717
+ SafetyPin::Node.create(blueprint)
718
+ end
719
+
720
+ it "requires a property name, a target regex/string, and a replacement value or block" do
721
+ expect { node.replace_property({}) }.to raise_error(KeyError)
722
+ expect { node.replace_property(name: "bar") }.to raise_error(KeyError)
723
+ expect { node.replace_property(name: "bar", target: /baz/) }.to raise_error(KeyError)
724
+ expect { node.replace_property(name: "bar", target: /baz/, replacement: "NOT BAZ") }.to_not raise_error
725
+ expect { node.replace_property(name: "bar", target: /baz/) {|value| "bob"} }.to_not raise_error
726
+ end
727
+
728
+ it "can take a regex property name" do
729
+ node.replace_property(name: /.+/, target: /.+/, replacement: "FOOFOO")
730
+ node.properties.values.each {|value| value.should == "FOOFOO" }
731
+ end
732
+
733
+ it "only replaces property values when the name matches" do
734
+ node.replace_property(name: /^bar.*/, target: /.+/, replacement: "FOOFOO")
735
+ node["bar"].should == "FOOFOO"
736
+ node["barbell"].should == "FOOFOO"
737
+ node["another"].should_not == "FOOFOO"
738
+ end
739
+
740
+ context "when given a block" do
741
+ it "yields the property value to the block and sets the property with the return of the block" do
742
+ node.replace_property(name: "bar", target: /.+/) {|value| value.upcase }
743
+ node["bar"].should == "BAZ"
744
+ end
745
+
746
+ it "ignores replacement value" do
747
+ node.replace_property(name: "bar", target: /.+/, replacement: "replacement value") {|value| value.upcase }
748
+ node["bar"].should == "BAZ"
749
+ end
750
+ end
751
+
752
+ it "returns modified nodes" do
753
+ node.replace_property(name: "bar", target: /.+/, replacement: "whatever").should have(1).item
754
+ node.replace_property(name: "asdf", target: /.+/, replacement: "whatever").should be_empty
755
+ end
756
+
757
+ it "does not save changes to JCR" do
758
+ node.replace_property(name: "bar", target: /.+/, replacement: "BAZ")
759
+ node["bar"].should == "BAZ"
760
+ node.reload
761
+ node["bar"].should_not == "BAZ"
762
+ end
763
+ end
712
764
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safety-pin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ prerelease:
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jordan Raine
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-08 00:00:00.000000000 Z
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: An easy-to-use JCR connector for JRuby
15
15
  email:
@@ -19,6 +19,7 @@ executables:
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
+ - .rbenv-version
22
23
  - .rspec
23
24
  - .rvmrc
24
25
  - Gemfile
@@ -139,26 +140,28 @@ files:
139
140
  - spec/where_condition_spec.rb
140
141
  homepage: https://github.com/jnraine/safety-pin
141
142
  licenses: []
142
- post_install_message:
143
+ post_install_message:
143
144
  rdoc_options: []
144
145
  require_paths:
145
146
  - lib
146
147
  required_ruby_version: !ruby/object:Gem::Requirement
147
- none: false
148
148
  requirements:
149
149
  - - ! '>='
150
150
  - !ruby/object:Gem::Version
151
- version: '0'
152
- required_rubygems_version: !ruby/object:Gem::Requirement
151
+ version: !binary |-
152
+ MA==
153
153
  none: false
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
155
  requirements:
155
156
  - - ! '>='
156
157
  - !ruby/object:Gem::Version
157
- version: '0'
158
+ version: !binary |-
159
+ MA==
160
+ none: false
158
161
  requirements: []
159
- rubyforge_project:
160
- rubygems_version: 1.8.23
161
- signing_key:
162
+ rubyforge_project:
163
+ rubygems_version: 1.8.24
164
+ signing_key:
162
165
  specification_version: 3
163
166
  summary: JCR connector for JRuby
164
167
  test_files:
@@ -169,4 +172,3 @@ test_files:
169
172
  - spec/query_spec.rb
170
173
  - spec/spec_helper.rb
171
174
  - spec/where_condition_spec.rb
172
- has_rdoc: