representable 1.6.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,10 +38,10 @@ class RepresentableTest < MiniTest::Spec
38
38
  assert_equal "name", BandRepresentation.representable_attrs.first.name
39
39
  end
40
40
 
41
- it "inherits to including modules" do
41
+ it "inherits to including modules xxx " do
42
42
  assert_equal 2, PunkBandRepresentation.representable_attrs.size
43
- assert_equal "name", PunkBandRepresentation.representable_attrs.first.name
44
- assert_equal "street_cred", PunkBandRepresentation.representable_attrs.last.name
43
+ assert_equal "name", PunkBandRepresentation.representable_attrs[:name].name
44
+ assert_equal "street_cred", PunkBandRepresentation.representable_attrs[:street_cred].name
45
45
  end
46
46
 
47
47
  it "inherits to including class" do
@@ -51,8 +51,8 @@ class RepresentableTest < MiniTest::Spec
51
51
  end
52
52
 
53
53
  assert_equal 2, band.representable_attrs.size
54
- assert_equal "name", band.representable_attrs.first.name
55
- assert_equal "street_cred", band.representable_attrs.last.name
54
+ assert_equal "name", band.representable_attrs[:name].name
55
+ assert_equal "street_cred", band.representable_attrs[:street_cred].name
56
56
  end
57
57
 
58
58
  it "allows including the concrete representer module later" do
@@ -96,26 +96,26 @@ class RepresentableTest < MiniTest::Spec
96
96
  end
97
97
 
98
98
 
99
- describe "Representable" do
100
- describe "inheritance" do
101
- class CoverSong < OpenStruct
102
- end
103
- module SongRepresenter
104
- include Representable::Hash
105
- property :name
106
- end
107
- module CoverSongRepresenter
108
- include Representable::Hash
109
- include SongRepresenter
110
- property :by
111
- end
99
+ describe "inheritance" do
100
+ class CoverSong < OpenStruct
101
+ end
112
102
 
113
- it "merges properties from all ancestors" do
114
- props = {"name"=>"The Brews", "by"=>"Nofx"}
115
- assert_equal(props, CoverSong.new(props).extend(CoverSongRepresenter).to_hash)
103
+ module SongRepresenter
104
+ include Representable::Hash
105
+ property :name
106
+ end
116
107
 
117
- end
108
+ module CoverSongRepresenter
109
+ include Representable::Hash
110
+ include SongRepresenter
111
+ property :by
112
+ end
113
+
114
+ it "merges properties from all ancestors" do
115
+ props = {"name"=>"The Brews", "by"=>"Nofx"}
116
+ assert_equal(props, CoverSong.new(props).extend(CoverSongRepresenter).to_hash)
118
117
  end
118
+
119
119
  it "allows mixing in multiple representers" do
120
120
  require 'representable/json'
121
121
  require 'representable/xml'
@@ -154,21 +154,50 @@ class RepresentableTest < MiniTest::Spec
154
154
 
155
155
 
156
156
  describe "#property" do
157
+ describe "overriding" do
158
+ representer! do
159
+ property :title, :as => :name
160
+ end
161
+
162
+ it { representer.representable_attrs.size.must_equal 1 }
163
+ it { representer.representable_attrs[:title].options.must_equal({:as => :name}) }
164
+
165
+ it "overrides property when called again" do
166
+ representer.class_eval do
167
+ property :title, :representable => true
168
+ end
169
+
170
+ representer.representable_attrs.size.must_equal 1
171
+ representer.representable_attrs[:title].options.must_equal({:representable => true})
172
+ end
173
+
174
+ it "overrides when inheriting same property" do
175
+ overriding = representer! { property :title, :representable => true }
176
+
177
+ representer.class_eval do
178
+ include overriding
179
+ end
180
+
181
+ representer.representable_attrs.size.must_equal 1
182
+ representer.representable_attrs[:title].options.must_equal({:representable => true})
183
+ end
184
+ end
185
+
157
186
  describe ":from" do
158
187
  # TODO: do this with all options.
159
188
  it "can be set explicitly" do
160
189
  band = Class.new(Band) { property :friends, :from => :friend }
161
- assert_equal "friend", band.representable_attrs.last.from
190
+ assert_equal "friend", band.representable_attrs[:friends].from
162
191
  end
163
192
 
164
193
  it "can be set explicitly with as" do
165
194
  band = Class.new(Band) { property :friends, :as => :friend }
166
- assert_equal "friend", band.representable_attrs.last.from
195
+ assert_equal "friend", band.representable_attrs[:friends].from
167
196
  end
168
197
 
169
198
  it "is infered from the name implicitly" do
170
199
  band = Class.new(Band) { property :friends }
171
- assert_equal "friends", band.representable_attrs.last.from
200
+ assert_equal "friends", band.representable_attrs[:friends].from
172
201
  end
173
202
  end
174
203
 
@@ -185,8 +214,8 @@ class RepresentableTest < MiniTest::Spec
185
214
  end
186
215
 
187
216
  it "creates correct Definition" do
188
- assert_equal "albums", RockBand.representable_attrs.last.name
189
- assert RockBand.representable_attrs.last.array?
217
+ assert_equal "albums", RockBand.representable_attrs[:albums].name
218
+ assert RockBand.representable_attrs[:albums].array?
190
219
  end
191
220
  end
192
221
 
@@ -858,89 +887,4 @@ class RepresentableTest < MiniTest::Spec
858
887
  end
859
888
  end
860
889
  end
861
-
862
- describe "Config" do
863
- subject { Representable::Config.new }
864
- PunkRock = Class.new
865
-
866
- describe "wrapping" do
867
- it "returns false per default" do
868
- assert_equal nil, subject.wrap_for("Punk")
869
- end
870
-
871
- it "infers a printable class name if set to true" do
872
- subject.wrap = true
873
- assert_equal "punk_rock", subject.wrap_for(PunkRock)
874
- end
875
-
876
- it "can be set explicitely" do
877
- subject.wrap = "Descendents"
878
- assert_equal "Descendents", subject.wrap_for(PunkRock)
879
- end
880
- end
881
-
882
- describe "#clone" do
883
- it "clones all definitions" do
884
- subject << Object.new
885
- assert subject.first != subject.clone.first
886
- end
887
- end
888
-
889
- describe "Config inheritance" do
890
- # TODO: this section will soon be moved to uber.
891
- describe "inheritance when including" do
892
- # TODO: test all the below issues AND if cloning works.
893
- module TestMethods
894
- def representer_for(modules=[Representable], &block)
895
- Module.new do
896
- extend TestMethods
897
- include *modules
898
- module_exec(&block)
899
- end
900
- end
901
- end
902
- include TestMethods
903
-
904
- it "inherits to uninitialized child" do
905
- representer_for do # child
906
- include(representer_for do # parent
907
- representable_attrs.inheritable_array(:links) << "bar"
908
- end)
909
- end.representable_attrs.inheritable_array(:links).must_equal(["bar"])
910
- end
911
-
912
- it "works with uninitialized parent" do
913
- representer_for do # child
914
- representable_attrs.inheritable_array(:links) << "bar"
915
-
916
- include(representer_for do # parent
917
- end)
918
- end.representable_attrs.inheritable_array(:links).must_equal(["bar"])
919
- end
920
-
921
- it "inherits when both are initialized" do
922
- representer_for do # child
923
- representable_attrs.inheritable_array(:links) << "bar"
924
-
925
- include(representer_for do # parent
926
- representable_attrs.inheritable_array(:links) << "stadium"
927
- end)
928
- end.representable_attrs.inheritable_array(:links).must_equal(["bar", "stadium"])
929
- end
930
-
931
- it "clones parent inheritables" do # FIXME: actually we don't clone here!
932
- representer_for do # child
933
- representable_attrs.inheritable_array(:links) << "bar"
934
-
935
- include(parent = representer_for do # parent
936
- representable_attrs.inheritable_array(:links) << "stadium"
937
- end)
938
-
939
- parent.representable_attrs.inheritable_array(:links) << "park" # modify parent array.
940
-
941
- end.representable_attrs.inheritable_array(:links).must_equal(["bar", "stadium"])
942
- end
943
- end
944
- end
945
- end
946
890
  end
@@ -50,11 +50,36 @@ MiniTest::Spec.class_eval do
50
50
  include XmlHelper
51
51
 
52
52
  def self.representer!(format=Representable::Hash, name=:representer, &block)
53
+ fmt = format # we need that so the 2nd call to ::let (within a ::describe) remembers the right format.
54
+
55
+ if fmt.is_a?(Hash)
56
+ name = fmt[:name] || :representer
57
+ format = fmt[:module] || Representable::Hash
58
+ end
59
+
53
60
  let(name) do
54
- Module.new do
61
+ mod = Module.new
62
+
63
+ if fmt.is_a?(Hash)
64
+ inject_representer(mod, fmt)
65
+ end
66
+
67
+ mod.module_eval do
55
68
  include format
56
69
  instance_exec(&block)
57
70
  end
71
+
72
+ mod
73
+ end
74
+
75
+ def inject_representer(mod, options)
76
+ return unless options[:inject]
77
+
78
+ injected_name = options[:inject]
79
+ injected = send(injected_name) # song_representer
80
+ mod.singleton_class.instance_eval do
81
+ define_method(injected_name) { injected }
82
+ end
58
83
  end
59
84
  end
60
85
 
@@ -66,6 +91,8 @@ MiniTest::Spec.class_eval do
66
91
  module_exec(&block)
67
92
  end
68
93
  end
94
+
95
+ alias_method :representer!, :representer_for
69
96
  end
70
97
  include TestMethods
71
98
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-12 00:00:00.000000000 Z
11
+ date: 2013-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -167,6 +167,7 @@ files:
167
167
  - Rakefile
168
168
  - TODO
169
169
  - lib/representable.rb
170
+ - lib/representable/TODO.getting_serious
170
171
  - lib/representable/binding.rb
171
172
  - lib/representable/bindings/hash_bindings.rb
172
173
  - lib/representable/bindings/xml_bindings.rb
@@ -177,6 +178,7 @@ files:
177
178
  - lib/representable/decorator/coercion.rb
178
179
  - lib/representable/definition.rb
179
180
  - lib/representable/deprecations.rb
181
+ - lib/representable/deserializer.rb
180
182
  - lib/representable/feature/readable_writeable.rb
181
183
  - lib/representable/hash.rb
182
184
  - lib/representable/hash/collection.rb
@@ -185,6 +187,7 @@ files:
185
187
  - lib/representable/json/collection.rb
186
188
  - lib/representable/json/hash.rb
187
189
  - lib/representable/mapper.rb
190
+ - lib/representable/serializer.rb
188
191
  - lib/representable/version.rb
189
192
  - lib/representable/xml.rb
190
193
  - lib/representable/xml/collection.rb
@@ -192,6 +195,7 @@ files:
192
195
  - lib/representable/yaml.rb
193
196
  - representable.gemspec
194
197
  - test/coercion_test.rb
198
+ - test/config_test.rb
195
199
  - test/decorator_test.rb
196
200
  - test/definition_test.rb
197
201
  - test/example.rb