live_resource 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,13 @@
|
|
1
1
|
module LiveResource
|
2
|
+
|
3
|
+
# Protocols are used by the Resource class to notify people interested in the resource that something's happened.
|
4
|
+
# This module defines the public behaviour of a protocol, in terms of the internal method #publish_message.
|
5
|
+
#
|
6
|
+
# Protocol classes must implement the #publish_message and #encode_identifier methods.
|
7
|
+
# - #publish_message(resource_id, message) publishes `message` to the resource matching the identifier (where
|
8
|
+
# message is a hash)
|
9
|
+
# - #encode_identifier(resource_id) transforms an identifier string as appropriate for the protocol (e.g.
|
10
|
+
# sanitizing unsafe characters or obfuscating the actual resource ID)
|
2
11
|
module Protocol
|
3
12
|
|
4
13
|
def publish_resource_reset(identifier)
|
@@ -10,7 +10,9 @@ module LiveResource
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def identifier(*context)
|
13
|
-
raise "You must define an identifier method for the resource '#{@name}'"
|
13
|
+
raise "You must define an identifier method for the resource '#{@name}'" unless respond_to?(:_identifier)
|
14
|
+
raw_identifier = _identifier(*context)
|
15
|
+
@protocol.encode_identifier(raw_identifier)
|
14
16
|
end
|
15
17
|
|
16
18
|
def push(*context)
|
@@ -125,7 +125,7 @@ describe LiveResource::Builder do
|
|
125
125
|
|
126
126
|
it "should define a new identifier method on the resource subclass" do
|
127
127
|
subject
|
128
|
-
expect(builder.resource.
|
128
|
+
expect(builder.resource._identifier(1, 2)).to eq([1, 2])
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
@@ -3,7 +3,8 @@ require "live_resource/resource"
|
|
3
3
|
|
4
4
|
describe LiveResource::Resource do
|
5
5
|
|
6
|
-
let(:
|
6
|
+
let(:resource_class) { Class.new(LiveResource::Resource) }
|
7
|
+
let(:resource) { resource_class.new(resource_name, protocol) }
|
7
8
|
let(:resource_name) { "some_resource" }
|
8
9
|
let(:protocol) { double(LiveResource::Protocol) }
|
9
10
|
|
@@ -20,9 +21,38 @@ describe LiveResource::Resource do
|
|
20
21
|
describe "#identifier" do
|
21
22
|
subject { resource.identifier(:a, :b) }
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
context "when #_identifier is undefined" do
|
25
|
+
it "should raise an exception" do
|
26
|
+
expect(lambda { subject }).to raise_exception
|
27
|
+
end
|
25
28
|
end
|
29
|
+
|
30
|
+
context "when #_identifier is defined" do
|
31
|
+
let(:raw_identifier) { 'raw_identifier' }
|
32
|
+
let(:encoded_identifier) { 'encoded_identifier' }
|
33
|
+
|
34
|
+
before do
|
35
|
+
_raw_identifier = raw_identifier
|
36
|
+
resource_class.instance_eval do
|
37
|
+
define_method(:_identifier) { |*args| _raw_identifier }
|
38
|
+
end
|
39
|
+
protocol.stub(encode_identifier: encoded_identifier)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not raise an exception" do
|
43
|
+
expect(lambda { subject }).to_not raise_exception
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should encode the identifier" do
|
47
|
+
protocol.should_receive(:encode_identifier).with(raw_identifier)
|
48
|
+
subject
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return the encoded identifier" do
|
52
|
+
expect(subject).to eq encoded_identifier
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
26
56
|
end
|
27
57
|
|
28
58
|
describe "#push" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: live_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -118,12 +118,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
118
|
- - ! '>='
|
119
119
|
- !ruby/object:Gem::Version
|
120
120
|
version: '0'
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
hash: -3868480263717602722
|
121
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
125
|
none: false
|
123
126
|
requirements:
|
124
127
|
- - ! '>='
|
125
128
|
- !ruby/object:Gem::Version
|
126
129
|
version: '0'
|
130
|
+
segments:
|
131
|
+
- 0
|
132
|
+
hash: -3868480263717602722
|
127
133
|
requirements: []
|
128
134
|
rubyforge_project:
|
129
135
|
rubygems_version: 1.8.25
|