ivy-serializers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +14 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +155 -0
  8. data/Rakefile +7 -0
  9. data/ivy-serializers.gemspec +24 -0
  10. data/lib/ivy-serializers.rb +1 -0
  11. data/lib/ivy/serializers.rb +5 -0
  12. data/lib/ivy/serializers/action_controller/serialization_support.rb +35 -0
  13. data/lib/ivy/serializers/attribute.rb +24 -0
  14. data/lib/ivy/serializers/documents.rb +17 -0
  15. data/lib/ivy/serializers/documents/document.rb +55 -0
  16. data/lib/ivy/serializers/documents/individual_resource.rb +18 -0
  17. data/lib/ivy/serializers/documents/resource_collection.rb +18 -0
  18. data/lib/ivy/serializers/formats.rb +3 -0
  19. data/lib/ivy/serializers/formats/active_model_serializers.rb +40 -0
  20. data/lib/ivy/serializers/formats/json.rb +96 -0
  21. data/lib/ivy/serializers/formats/json_api.rb +66 -0
  22. data/lib/ivy/serializers/mapping.rb +40 -0
  23. data/lib/ivy/serializers/railtie.rb +13 -0
  24. data/lib/ivy/serializers/registry.rb +33 -0
  25. data/lib/ivy/serializers/relationships.rb +2 -0
  26. data/lib/ivy/serializers/relationships/belongs_to.rb +13 -0
  27. data/lib/ivy/serializers/relationships/has_many.rb +13 -0
  28. data/lib/ivy/serializers/relationships/relationship.rb +23 -0
  29. data/lib/ivy/serializers/serializer.rb +35 -0
  30. data/lib/ivy/serializers/version.rb +5 -0
  31. data/spec/integration/formats/active_model_serializers_spec.rb +143 -0
  32. data/spec/integration/formats/json_api_spec.rb +254 -0
  33. data/spec/integration/serializer_spec.rb +47 -0
  34. data/spec/spec_helper.rb +93 -0
  35. metadata +165 -0
@@ -0,0 +1,254 @@
1
+ require 'ivy/serializers'
2
+
3
+ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
4
+ let(:format) { described_class.new(document) }
5
+
6
+ describe '#as_json' do
7
+ let(:registry) { Ivy::Serializers::Registry.new }
8
+ let(:document) { Ivy::Serializers::Documents.create(registry, :post, post) }
9
+
10
+ subject { format.as_json }
11
+
12
+ context 'with default mapping' do
13
+ let(:post_class) { double('Post', :name => 'Post') }
14
+ let(:post) { double('post', :class => post_class, :id => 1) }
15
+
16
+ it { should eq({
17
+ :data => {
18
+ :type => 'post',
19
+ :id => '1',
20
+ :links => {}
21
+ }
22
+ }) }
23
+ end
24
+
25
+ context 'with an attribute' do
26
+ let(:post_class) { double('Post', :name => 'Post') }
27
+ let(:post) { double('post', :class => post_class, :id => 1, :title => 'title') }
28
+
29
+ context 'with default options' do
30
+ before do
31
+ registry.map post_class do
32
+ attribute :title
33
+ end
34
+ end
35
+
36
+ it { should eq({
37
+ :data => {
38
+ :type => 'post',
39
+ :id => '1',
40
+ :title => 'title',
41
+ :links => {}
42
+ }
43
+ }) }
44
+ end
45
+
46
+ context 'with a block provided' do
47
+ before do
48
+ registry.map post_class do
49
+ attribute(:headline) { |post| post.title }
50
+ end
51
+ end
52
+
53
+ it { should eq({
54
+ :data => {
55
+ :type => 'post',
56
+ :id => '1',
57
+ :headline => 'title',
58
+ :links => {}
59
+ }
60
+ }) }
61
+ end
62
+ end
63
+
64
+ context 'with a belongs_to relationship' do
65
+ let(:author_class) { double('Author', :name => 'Author') }
66
+ let(:author) { double('author', :class => author_class, :id => 1) }
67
+ let(:post_class) { double('Post', :name => 'Post') }
68
+ let(:post) { double('post', :author => author, :class => post_class, :id => 1) }
69
+
70
+ context 'with default options' do
71
+ before do
72
+ registry.map post_class do
73
+ belongs_to :author
74
+ end
75
+ end
76
+
77
+ it { should eq({
78
+ :data => {
79
+ :type => 'post',
80
+ :id => '1',
81
+ :links => {
82
+ :author => {
83
+ :linkage => {:id => '1', :type => 'author'}
84
+ }
85
+ }
86
+ }
87
+ }) }
88
+ end
89
+
90
+ context 'with a block provided' do
91
+ before do
92
+ registry.map post_class do
93
+ belongs_to(:user) { |post| post.author }
94
+ end
95
+ end
96
+
97
+ it { should eq({
98
+ :data => {
99
+ :type => 'post',
100
+ :id => '1',
101
+ :links => {
102
+ :user => {
103
+ :linkage => {:id => '1', :type => 'author'}
104
+ }
105
+ }
106
+ }
107
+ }) }
108
+ end
109
+
110
+ context 'with the :embed_in_root option' do
111
+ before do
112
+ registry.map post_class do
113
+ belongs_to :author, :embed_in_root => true
114
+ end
115
+ end
116
+
117
+ it { should eq({
118
+ :data => {
119
+ :type => 'post',
120
+ :id => '1',
121
+ :links => {
122
+ :author => {
123
+ :linkage => {:id => '1', :type => 'author'}
124
+ }
125
+ }
126
+ },
127
+
128
+ :included => {
129
+ :authors => [{
130
+ :id => '1',
131
+ :links => {},
132
+ :type => 'author'
133
+ }]
134
+ }
135
+ }) }
136
+ end
137
+
138
+ context 'with the :polymorphic option' do
139
+ before do
140
+ registry.map post_class do
141
+ belongs_to :author, :polymorphic => true
142
+ end
143
+ end
144
+
145
+ it { should eq({
146
+ :data => {
147
+ :type => 'post',
148
+ :id => '1',
149
+ :links => {
150
+ :author => {
151
+ :linkage => {:id => '1', :type => 'author'}
152
+ }
153
+ }
154
+ }
155
+ }) }
156
+ end
157
+ end
158
+
159
+ context 'with a has_many relationship' do
160
+ let(:comment_class) { double('Comment', :name => 'Comment') }
161
+ let(:comment) { double('comment', :class => comment_class, :id => 1) }
162
+ let(:post_class) { double('Post', :name => 'Post') }
163
+ let(:post) { double('post', :class => post_class, :comments => [comment], :id => 1) }
164
+
165
+ context 'with default options' do
166
+ before do
167
+ registry.map post_class do
168
+ has_many :comments
169
+ end
170
+ end
171
+
172
+ it { should eq({
173
+ :data => {
174
+ :type => 'post',
175
+ :id => '1',
176
+ :links => {
177
+ :comments => {
178
+ :linkage => [{:id => '1', :type => 'comment'}]
179
+ }
180
+ }
181
+ }
182
+ }) }
183
+ end
184
+
185
+ context 'with a block provided' do
186
+ before do
187
+ registry.map post_class do
188
+ has_many(:replies) { |post| post.comments }
189
+ end
190
+ end
191
+
192
+ it { should eq({
193
+ :data => {
194
+ :type => 'post',
195
+ :id => '1',
196
+ :links => {
197
+ :replies => {
198
+ :linkage => [{:id => '1', :type => 'comment'}]
199
+ }
200
+ }
201
+ }
202
+ }) }
203
+ end
204
+
205
+ context 'with the :embed_in_root option' do
206
+ before do
207
+ registry.map post_class do
208
+ has_many :comments, :embed_in_root => true
209
+ end
210
+ end
211
+
212
+ it { should eq({
213
+ :data => {
214
+ :type => 'post',
215
+ :id => '1',
216
+ :links => {
217
+ :comments => {
218
+ :linkage => [{:id => '1', :type => 'comment'}]
219
+ }
220
+ }
221
+ },
222
+
223
+ :included => {
224
+ :comments => [{
225
+ :type => 'comment',
226
+ :id => '1',
227
+ :links => {}
228
+ }]
229
+ }
230
+ }) }
231
+ end
232
+
233
+ context 'with the :polymorphic option' do
234
+ before do
235
+ registry.map post_class do
236
+ has_many :comments, :polymorphic => true
237
+ end
238
+ end
239
+
240
+ it { should eq({
241
+ :data => {
242
+ :type => 'post',
243
+ :id => '1',
244
+ :links => {
245
+ :comments => {
246
+ :linkage => [{:id => '1', :type => 'comment'}]
247
+ }
248
+ }
249
+ }
250
+ }) }
251
+ end
252
+ end
253
+ end
254
+ end
@@ -0,0 +1,47 @@
1
+ require 'ivy/serializers'
2
+
3
+ RSpec.describe Ivy::Serializers::Serializer do
4
+ describe '#resource' do
5
+ let(:serializer) { serializer_class.new }
6
+ let(:generator) { Ivy::Serializers::Formats::ActiveModelSerializers.new(document) }
7
+ let(:document) { Ivy::Serializers::Documents.create(serializer, :post, post) }
8
+ let(:post_class) { double('Post') }
9
+ let(:post) { double('post', :class => post_class, :id => 1, :title => 'title') }
10
+
11
+ context 'with an attribute' do
12
+ subject { generator.as_json }
13
+
14
+ context 'with default options' do
15
+ let(:serializer_class) {
16
+ post_klass = post_class
17
+
18
+ Class.new(described_class) do
19
+ map post_klass do
20
+ attribute :title
21
+ end
22
+ end
23
+ }
24
+
25
+ it { should eq(:post => {:id => 1, :title => 'title'}) }
26
+ end
27
+
28
+ context 'with a block provided' do
29
+ let(:serializer_class) {
30
+ post_klass = post_class
31
+
32
+ Class.new(described_class) do
33
+ map post_klass do
34
+ attribute(:title) { title }
35
+
36
+ def title
37
+ 'custom_title'
38
+ end
39
+ end
40
+ end
41
+ }
42
+
43
+ it { should eq(:post => {:id => 1, :title => 'custom_title'}) }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,93 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start
4
+
5
+ # This file was generated by the `rspec --init` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
8
+ # this file to always be loaded, without a need to explicitly require it in any
9
+ # files.
10
+ #
11
+ # Given that it is always loaded, you are encouraged to keep this file as
12
+ # light-weight as possible. Requiring heavyweight dependencies from this file
13
+ # will add to the boot time of your test suite on EVERY test run, even for an
14
+ # individual file that may not need all of that loaded. Instead, consider making
15
+ # a separate helper file that requires the additional dependencies and performs
16
+ # the additional setup, and require it from the spec files that actually need
17
+ # it.
18
+ #
19
+ # The `.rspec` file also contains a few flags that are not defaults but that
20
+ # users commonly want.
21
+ #
22
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23
+ RSpec.configure do |config|
24
+ # rspec-expectations config goes here. You can use an alternate
25
+ # assertion/expectation library such as wrong or the stdlib/minitest
26
+ # assertions if you prefer.
27
+ config.expect_with :rspec do |expectations|
28
+ # This option will default to `true` in RSpec 4. It makes the `description`
29
+ # and `failure_message` of custom matchers include text for helper methods
30
+ # defined using `chain`, e.g.:
31
+ # be_bigger_than(2).and_smaller_than(4).description
32
+ # # => "be bigger than 2 and smaller than 4"
33
+ # ...rather than:
34
+ # # => "be bigger than 2"
35
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36
+ end
37
+
38
+ # rspec-mocks config goes here. You can use an alternate test double
39
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
40
+ config.mock_with :rspec do |mocks|
41
+ # Prevents you from mocking or stubbing a method that does not exist on
42
+ # a real object. This is generally recommended, and will default to
43
+ # `true` in RSpec 4.
44
+ mocks.verify_partial_doubles = true
45
+ end
46
+
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is
55
+ # recommended. For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ # config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ end
92
+
93
+ Dir['./spec/support/**/*.rb'].each { |file| require(file) }
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ivy-serializers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dray Lacy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.2.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.2.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: hash_generator
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.1'
97
+ description:
98
+ email:
99
+ - dray@envylabs.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - ivy-serializers.gemspec
112
+ - lib/ivy-serializers.rb
113
+ - lib/ivy/serializers.rb
114
+ - lib/ivy/serializers/action_controller/serialization_support.rb
115
+ - lib/ivy/serializers/attribute.rb
116
+ - lib/ivy/serializers/documents.rb
117
+ - lib/ivy/serializers/documents/document.rb
118
+ - lib/ivy/serializers/documents/individual_resource.rb
119
+ - lib/ivy/serializers/documents/resource_collection.rb
120
+ - lib/ivy/serializers/formats.rb
121
+ - lib/ivy/serializers/formats/active_model_serializers.rb
122
+ - lib/ivy/serializers/formats/json.rb
123
+ - lib/ivy/serializers/formats/json_api.rb
124
+ - lib/ivy/serializers/mapping.rb
125
+ - lib/ivy/serializers/railtie.rb
126
+ - lib/ivy/serializers/registry.rb
127
+ - lib/ivy/serializers/relationships.rb
128
+ - lib/ivy/serializers/relationships/belongs_to.rb
129
+ - lib/ivy/serializers/relationships/has_many.rb
130
+ - lib/ivy/serializers/relationships/relationship.rb
131
+ - lib/ivy/serializers/serializer.rb
132
+ - lib/ivy/serializers/version.rb
133
+ - spec/integration/formats/active_model_serializers_spec.rb
134
+ - spec/integration/formats/json_api_spec.rb
135
+ - spec/integration/serializer_spec.rb
136
+ - spec/spec_helper.rb
137
+ homepage: ''
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.6
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: JSON serialization for client-side apps.
161
+ test_files:
162
+ - spec/integration/formats/active_model_serializers_spec.rb
163
+ - spec/integration/formats/json_api_spec.rb
164
+ - spec/integration/serializer_spec.rb
165
+ - spec/spec_helper.rb