roar 0.11.15 → 0.11.16
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +0 -1
- data/CHANGES.markdown +4 -0
- data/README.textile +18 -0
- data/TODO.markdown +2 -1
- data/lib/roar/decorator.rb +7 -0
- data/lib/roar/representer/feature/hypermedia.rb +6 -0
- data/lib/roar/representer/json.rb +3 -1
- data/lib/roar/representer/json/hal.rb +4 -2
- data/lib/roar/representer/xml.rb +3 -1
- data/lib/roar/version.rb +1 -1
- data/roar.gemspec +1 -1
- data/test/decorator_test.rb +80 -9
- data/test/test_helper.rb +1 -0
- metadata +4 -5
- data/gemfiles/Gemfile.representable-1.3 +0 -13
data/.travis.yml
CHANGED
data/CHANGES.markdown
CHANGED
data/README.textile
CHANGED
@@ -281,6 +281,24 @@ order.post(order.links[:self])
|
|
281
281
|
|
282
282
|
This was dead simple since representations can be composed of different documents in Roar.
|
283
283
|
|
284
|
+
h2. Decorators
|
285
|
+
|
286
|
+
You can use `Roar::Decorator` as a representer. More docs coming soon. Note that if you want your represented object to save incoming hypermedia you need to do two things.
|
287
|
+
|
288
|
+
<pre>
|
289
|
+
class SongClient
|
290
|
+
# do whatever here
|
291
|
+
|
292
|
+
attr_accessor :links
|
293
|
+
end
|
294
|
+
|
295
|
+
class SongRepresenter < Roar::Decorator
|
296
|
+
include Roar::Representer::JSON
|
297
|
+
include Roar::Decorator::HypermediaConsumer
|
298
|
+
end
|
299
|
+
</pre>
|
300
|
+
|
301
|
+
|
284
302
|
h2. More Features
|
285
303
|
|
286
304
|
Be sure to check out the bundled features.
|
data/TODO.markdown
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
* Add proxies, so nested models can be lazy-loaded.
|
2
2
|
* move #prepare_links! call to #_links or something so it doesn't need to be called in #serialize.
|
3
3
|
* alias Roar::Representer to Representer
|
4
|
-
* remove #before_serialize and just overwrite #serialize
|
4
|
+
* remove #before_serialize and just overwrite #serialize
|
5
|
+
* abstract ::links_definition_options and move them out of the generic representers (JSON, XML). remove lambdas when :representer_exec calls methods in decorator ctx, also
|
data/lib/roar/decorator.rb
CHANGED
@@ -1,3 +1,10 @@
|
|
1
1
|
class Roar::Decorator < Representable::Decorator
|
2
2
|
extend Roar::Representer::InheritableArray
|
3
|
+
|
4
|
+
module HypermediaConsumer
|
5
|
+
def links_array=(*args)
|
6
|
+
super # TODO: this currently sets #links which is not obvious.
|
7
|
+
represented.links = links
|
8
|
+
end
|
9
|
+
end
|
3
10
|
end
|
@@ -70,6 +70,12 @@ module Roar
|
|
70
70
|
include LinkConfigsMethod
|
71
71
|
|
72
72
|
private
|
73
|
+
def links_definition_options
|
74
|
+
# TODO: this method is never called.
|
75
|
+
[:links_array, {:from => :link, :class => Feature::Hypermedia::Hyperlink, :collection => true,
|
76
|
+
:representer_exec => true, :getter => lambda { |*| links_array }, :setter => lambda { |val,*| links_array=(val) } }] # TODO: merge with JSON.
|
77
|
+
end
|
78
|
+
|
73
79
|
# Setup hypermedia links by invoking their blocks. Usually called by #serialize.
|
74
80
|
def prepare_links!(*args)
|
75
81
|
# TODO: move this method to _links or something so it doesn't need to be called in #serialize.
|
@@ -45,7 +45,9 @@ module Roar
|
|
45
45
|
|
46
46
|
# TODO: move to instance method, or remove?
|
47
47
|
def links_definition_options
|
48
|
-
|
48
|
+
# FIXME: this doesn't belong into the generic JSON representer.
|
49
|
+
[:links_array, {:from => :links, :class => Feature::Hypermedia::Hyperlink, :extend => HyperlinkRepresenter, :collection => true,
|
50
|
+
:representer_exec => true, :getter => lambda { |*| links_array }, :setter => lambda { |val,*| self.links_array=(val) } }]
|
49
51
|
end
|
50
52
|
end
|
51
53
|
|
@@ -157,9 +157,11 @@ module Roar::Representer
|
|
157
157
|
def links_definition_options
|
158
158
|
[:links,
|
159
159
|
{
|
160
|
-
:
|
160
|
+
:as => :links,
|
161
161
|
:extend => HAL::Links::LinkCollectionRepresenter,
|
162
|
-
:instance => lambda { |hsh| LinkCollection.new(link_array_rels) }
|
162
|
+
:instance => lambda { |hsh| LinkCollection.new(link_array_rels) }, # defined in InstanceMethods as this is executed in represented context.
|
163
|
+
:representer_exec => true,
|
164
|
+
:getter => lambda { |*| links }
|
163
165
|
}
|
164
166
|
]
|
165
167
|
end
|
data/lib/roar/representer/xml.rb
CHANGED
@@ -38,7 +38,9 @@ module Roar
|
|
38
38
|
include Representable::XML::ClassMethods
|
39
39
|
|
40
40
|
def links_definition_options
|
41
|
-
|
41
|
+
# FIXME: this doesn't belong into the generic XML representer.
|
42
|
+
[:links_array, {:from => :link, :class => Feature::Hypermedia::Hyperlink, :collection => true, :extend => XML::HyperlinkRepresenter,
|
43
|
+
:representer_exec => true, :getter => lambda { |*| links_array }, :setter => lambda { |val,*| self.links_array=(val) } }] # TODO: merge with JSON.
|
42
44
|
end
|
43
45
|
|
44
46
|
# Generic entry-point for parsing.
|
data/lib/roar/version.rb
CHANGED
data/roar.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
|
-
s.add_runtime_dependency "representable", ">= 1.2
|
22
|
+
s.add_runtime_dependency "representable", ">= 1.4.2"
|
23
23
|
|
24
24
|
s.add_development_dependency "rake"
|
25
25
|
s.add_development_dependency "test_xml", ">= 0.1.4"
|
data/test/decorator_test.rb
CHANGED
@@ -1,21 +1,92 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'roar/decorator'
|
2
3
|
|
3
4
|
class DecoratorTest < MiniTest::Spec
|
4
|
-
|
5
|
-
|
6
5
|
describe "Decorator" do
|
7
6
|
it "exposes ::prepare" do
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
class SongRepresentation < Roar::Decorator
|
8
|
+
include Roar::Representer::JSON
|
9
|
+
|
10
|
+
property :name
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
SongRepresentation.prepare(OpenStruct.new(:name => "Not The Same")).to_hash.must_equal({"name"=>"Not The Same"})
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "Hypermedia modules" do
|
18
|
+
representer_for do
|
19
|
+
link(:self) { "http://self" } # TODO: test with Collection::JSON, too.
|
20
|
+
end
|
14
21
|
|
15
|
-
|
22
|
+
let (:model) { Object.new }
|
23
|
+
|
24
|
+
describe "JSON" do
|
25
|
+
let (:decorator) { rpr_mod = rpr
|
26
|
+
Class.new(Roar::Decorator) do
|
27
|
+
include rpr_mod
|
28
|
+
end }
|
29
|
+
|
30
|
+
it "rendering links works" do
|
31
|
+
decorator.new(model).to_hash.must_equal({"links"=>[{:rel=>:self, :href=>"http://self"}]})
|
32
|
+
end
|
33
|
+
|
34
|
+
it "sets links on decorator" do
|
35
|
+
decorator.new(model).from_hash("links"=>[{:rel=>:self, :href=>"http://self"}]).links.must_equal("self"=>link(:rel=>:self, :href=>"http://self"))
|
36
|
+
end
|
37
|
+
|
38
|
+
let (:model_with_links) { model.singleton_class.instance_eval { attr_accessor :links }; model }
|
39
|
+
it "does not set links on represented" do
|
40
|
+
decorator.new(model_with_links).from_hash("links"=>[{:rel=>:self, :href=>"http://self"}])
|
41
|
+
model_with_links.links.must_equal nil
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "Decorator::HypermediaClient" do
|
45
|
+
let (:decorator) { rpr_mod = rpr
|
46
|
+
Class.new(Roar::Decorator) do
|
47
|
+
include rpr_mod
|
48
|
+
include Roar::Decorator::HypermediaConsumer
|
49
|
+
end }
|
50
|
+
|
51
|
+
it "propagates links to represented" do
|
52
|
+
decorator.new(model_with_links).from_hash("links"=>[{:rel=>:self, :href=>"http://self"}])
|
53
|
+
model_with_links.links[:self].must_equal(link(:rel=>:self, :href=>"http://self"))
|
16
54
|
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "XML" do
|
59
|
+
representer_for([Roar::Representer::XML, Roar::Representer::Feature::Hypermedia]) do
|
60
|
+
link(:self) { "http://self" } # TODO: test with HAL, too.
|
61
|
+
#self.representation_wrap = :song # FIXME: why isn't this working?
|
62
|
+
end
|
63
|
+
let (:decorator) { rpr_mod = rpr
|
64
|
+
Class.new(Roar::Decorator) do
|
65
|
+
include rpr_mod
|
66
|
+
self.representation_wrap = :song
|
67
|
+
end }
|
68
|
+
|
69
|
+
it "rendering links works" do
|
70
|
+
decorator.new(model).to_xml.must_equal_xml "<song><link rel=\"self\" href=\"http://self\"/></song>"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "sets links on decorator" do
|
74
|
+
decorator.new(model).from_xml(%{<song><link rel="self" href="http://self"/></song>}).links.must_equal("self"=>link(:rel=>:self, :href=>"http://self"))
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
describe "JSON::HAL" do
|
80
|
+
representer_for([Roar::Representer::JSON::HAL]) do
|
81
|
+
link(:self) { "http://self" }
|
82
|
+
end
|
83
|
+
let (:decorator) { rpr_mod = rpr
|
84
|
+
Class.new(Roar::Decorator) do
|
85
|
+
include rpr_mod
|
86
|
+
end }
|
17
87
|
|
18
|
-
|
88
|
+
it "rendering links works" do
|
89
|
+
decorator.new(model).to_hash.must_equal({"_links"=>{"self"=>{:href=>"http://self"}}})
|
19
90
|
end
|
20
91
|
end
|
21
92
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.11.
|
4
|
+
version: 0.11.16
|
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-05-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: representable
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 1.2
|
21
|
+
version: 1.4.2
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 1.2
|
29
|
+
version: 1.4.2
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rake
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -91,7 +91,6 @@ files:
|
|
91
91
|
- README.textile
|
92
92
|
- Rakefile
|
93
93
|
- TODO.markdown
|
94
|
-
- gemfiles/Gemfile.representable-1.3
|
95
94
|
- gemfiles/Gemfile.representable-1.4
|
96
95
|
- lib/roar.rb
|
97
96
|
- lib/roar/decorator.rb
|