restpack_serializer 0.2.8 → 0.2.9

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTBhNDdkYjA4OTdiYzE4NTJmMzg4NzExMWY5OGM1ODY5OWUwN2Y5Mw==
4
+ M2M0YjM0NjM5MGExZWY2Y2JiZmJiZGU3ZDBiN2U0NWRkZTBlYWIyNg==
5
5
  data.tar.gz: !binary |-
6
- YWI2ZGYwMTU1NTM0MDQxYWQ1MjBmMzI4MWI5ZDQ0ZTdlZWI4ZjVlMg==
6
+ ZDNiOTQxNTk0ZDM4NDZhZGExZDM3MGVjMTQ5OGUwOGJlYzg0YzQ3YQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NTVlYWRlNDgzYWE2NDBkZTNjYTA4ZTlhZjY1ZGM2MGFmZjNhZGZlNGQ0NDU5
10
- YzMyYjk5NzQ3YmEyZWI3Mzc3MGEyMWE0NDBjYjQ1MzgzYWNlYTg4ZGRkNTQ4
11
- NzU1MjA3NzdjZjgyZDVlMzQxM2JmNDA4YmIxOTkwNWE3ZmZkOWU=
9
+ OTQ4MTA0NmI5NDcxOTU3NTgxNWM0MTc3MzJjNzk4ZGYzYzBkZWYzY2I4NGFh
10
+ YzA4NmRhMDdiYjFhODM2YWYwMzRhNjU1ZDAxNTIxNDNhMzhiODY1ZDE3MzE4
11
+ M2Y5ZjkzMzU4Y2ZhNGQzOTg1OWEwMGZmYmE1ZjU4YWRiNmFkNzQ=
12
12
  data.tar.gz: !binary |-
13
- Yzc5OGZjYWIwZmFkMDhkMmUzYzExMGI2ZDc2MWM1ODEzOTBjNDUyMThhY2Vi
14
- YTI0ZmEwMWEwMThkZGM4ODdlMTgzNTg3OTc4YWU5ZmY3ZjYxYmM0N2VkZjFl
15
- YTA0OTU5ZmE0MmUyYTgxMmRmOTQxN2RmN2ZhNzI3NDcxN2Q1YTM=
13
+ ZWIyYjkzZTQzNjNhYzFhNmRjZTgyYjBlN2JhNzRjOWQ3N2YxOTg1MWI1ZmQ5
14
+ NzI5NTk2NTVlYjljM2U0OTMxZjRkYzZmMDM2YTAyNmEzMmYzOTAyNDJhNDM0
15
+ M2IyOTNhNDdhYmE2NzAyYTk2ZDBkZTNmNzZmM2FjODQ4ZGQzOGQ=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- restpack_serializer (0.2.8)
4
+ restpack_serializer (0.2.9)
5
5
  activerecord (>= 3.0)
6
6
  activesupport (>= 3.0)
7
7
  will_paginate (~> 3.0)
data/README.md CHANGED
@@ -31,10 +31,6 @@ restpack_serializer allows us to define a corresponding serializer:
31
31
  class AlbumSerializer
32
32
  include RestPack::Serializer
33
33
  attributes :id, :title, :year, :artist_id, :href
34
-
35
- def href
36
- "/albums/#{id}.json"
37
- end
38
34
  end
39
35
  ```
40
36
 
@@ -52,7 +48,7 @@ end
52
48
 
53
49
  ## Exposing an API
54
50
 
55
- The `AlbumSerializer` provides `page` and `resource` method which provide paged collection and singular resource GET endpoints.
51
+ The `AlbumSerializer` provides `page` and `resource` methods which provide paged collection and singular resource GET endpoints.
56
52
 
57
53
  ```ruby
58
54
  class AlbumsController < ApplicationController
@@ -71,7 +67,7 @@ These endpoint will live at URLs such as `/albums.json` and `/albums/142857.json
71
67
  * http://restpack-serializer-sample.herokuapp.com/albums.json
72
68
  * http://restpack-serializer-sample.herokuapp.com/albums/4.json
73
69
 
74
- Both `page` and `resource` methods can have an optional scope allowing us to enforce arbitrary constraints:
70
+ Both `page` and `resource` methods take an optional scope argument allowing us to enforce arbitrary constraints:
75
71
 
76
72
  ```ruby
77
73
  AlbumSerializer.page(params, Albums.where("year < 1950"))
@@ -102,7 +98,7 @@ http://restpack-serializer-sample.herokuapp.com/songs.json?page=2&page_size=3 yi
102
98
  },
103
99
  {
104
100
  "id": "5",
105
- "title": "Treedfingers",
101
+ "title": "Treefingers",
106
102
  "href": "/songs/5.json",
107
103
  "links": {
108
104
  "artist": "1",
@@ -127,7 +123,9 @@ http://restpack-serializer-sample.herokuapp.com/songs.json?page=2&page_size=3 yi
127
123
  "includes": [],
128
124
  "page_count": 14,
129
125
  "previous_page": 1,
130
- "next_page": 3
126
+ "next_page": 3,
127
+ "previous_href": "/api/v1/songs.json?page_size=3",
128
+ "next_href": "/api/v1/songs.json?page=3&page_size=3"
131
129
  }
132
130
  },
133
131
  "links": {
@@ -156,11 +154,8 @@ Side-loading allows related resources to be optionally included in a single API
156
154
  class AlbumSerializer
157
155
  include RestPack::Serializer
158
156
  attributes :id, :title, :year, :artist_id, :href
157
+
159
158
  can_include :songs, :artists
160
-
161
- def href
162
- "/albums/#{id}.json"
163
- end
164
159
  end
165
160
  ```
166
161
 
@@ -226,7 +221,9 @@ which yields:
226
221
  ],
227
222
  "page_count": 1,
228
223
  "previous_page": null,
229
- "next_page": null
224
+ "next_page": null,
225
+ "previous_href": null,
226
+ "next_href": null
230
227
  }
231
228
  },
232
229
  "links": {
@@ -0,0 +1,12 @@
1
+ module RestPack
2
+ module Serializer
3
+ class Configuration
4
+ attr_accessor :href_prefix, :page_size
5
+
6
+ def initialize
7
+ @href_prefix = ''
8
+ @page_size = 10
9
+ end
10
+ end
11
+ end
12
+ end
@@ -2,13 +2,11 @@ module RestPack::Serializer
2
2
  class Options
3
3
  attr_accessor :page, :page_size, :includes, :filters, :model_class, :scope, :include_links
4
4
 
5
- DEFAULT_PAGE_SIZE = 10
6
-
7
5
  def initialize(model_class, params = {}, scope = nil)
8
6
  params.symbolize_keys! if params.respond_to?(:symbolize_keys!)
9
7
 
10
8
  @page = 1
11
- @page_size = DEFAULT_PAGE_SIZE
9
+ @page_size = RestPack::Serializer.config.page_size
12
10
  @includes = []
13
11
  @filters = filters_from_params(params, model_class)
14
12
  @model_class = model_class
@@ -34,7 +32,7 @@ module RestPack::Serializer
34
32
  end
35
33
 
36
34
  def default_page_size?
37
- @page_size == DEFAULT_PAGE_SIZE
35
+ @page_size == RestPack::Serializer.config.page_size
38
36
  end
39
37
 
40
38
  def filters_as_url_params
@@ -2,7 +2,7 @@ module RestPack::Serializer::Attributes
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  def default_href
5
- "#{RestPack::Serializer.href_prefix}/#{@model.class.table_name}/#{@model.id}.json"
5
+ "#{RestPack::Serializer.config.href_prefix}/#{@model.class.table_name}/#{@model.id}.json"
6
6
  end
7
7
 
8
8
  module ClassMethods
@@ -57,7 +57,7 @@ module RestPack::Serializer::Paging
57
57
  def page_href(page, options)
58
58
  return nil unless page
59
59
 
60
- url = "#{RestPack::Serializer.href_prefix}/#{self.key}.json"
60
+ url = "#{RestPack::Serializer.config.href_prefix}/#{self.key}.json"
61
61
 
62
62
  params = []
63
63
  params << "page=#{page}" unless page == 1
@@ -45,7 +45,7 @@ module RestPack::Serializer::SideLoading
45
45
  end
46
46
 
47
47
  links[link_key] = {
48
- :href => RestPack::Serializer.href_prefix + href,
48
+ :href => RestPack::Serializer.config.href_prefix + href,
49
49
  :type => association.plural_name.to_sym
50
50
  }
51
51
  end
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.2.8'
3
+ VERSION = '0.2.9'
4
4
  end
5
5
  end
@@ -2,13 +2,17 @@ require 'will_paginate'
2
2
  require 'will_paginate/active_record'
3
3
 
4
4
  require 'restpack_serializer/version'
5
+ require 'restpack_serializer/configuration'
5
6
  require 'restpack_serializer/serializable'
6
7
  require 'restpack_serializer/factory'
7
8
 
8
9
  module RestPack
9
10
  module Serializer
10
- mattr_accessor :href_prefix
11
+ mattr_accessor :config
12
+ @@config = Configuration.new
11
13
 
12
- @@href_prefix = ''
14
+ def self.setup
15
+ yield @@config
16
+ end
13
17
  end
14
18
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestPack::Serializer do
4
+ before { @original_config = subject.config.clone }
5
+ after { subject.config = @original_config }
6
+
7
+ context "#setup" do
8
+ it "has defaults" do
9
+ subject.config.href_prefix.should == ''
10
+ subject.config.page_size.should == 10
11
+ end
12
+
13
+ it "can be configured" do
14
+ subject.setup do |config|
15
+ config.href_prefix = '/api/v1'
16
+ config.page_size = 50
17
+ end
18
+
19
+ subject.config.href_prefix.should == '/api/v1'
20
+ subject.config.page_size.should == 50
21
+ end
22
+ end
23
+ end
@@ -61,11 +61,11 @@ describe RestPack::Serializer::SideLoading do
61
61
  }
62
62
  }
63
63
 
64
- it "applies custom RestPack::Serializer.href_prefix" do
65
- original = RestPack::Serializer.href_prefix
66
- RestPack::Serializer.href_prefix = "/api/v1"
64
+ it "applies custom RestPack::Serializer.config.href_prefix" do
65
+ original = RestPack::Serializer.config.href_prefix
66
+ RestPack::Serializer.config.href_prefix = "/api/v1"
67
67
  AlbumSerializer.links["albums.artist"][:href].should == "/api/v1/artists/{albums.artist}.json"
68
- RestPack::Serializer.href_prefix = original
68
+ RestPack::Serializer.config.href_prefix = original
69
69
  end
70
70
  end
71
71
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restpack_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gavin Joyce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-21 00:00:00.000000000 Z
11
+ date: 2013-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -181,6 +181,7 @@ files:
181
181
  - README.md
182
182
  - Rakefile
183
183
  - lib/restpack_serializer.rb
184
+ - lib/restpack_serializer/configuration.rb
184
185
  - lib/restpack_serializer/factory.rb
185
186
  - lib/restpack_serializer/options.rb
186
187
  - lib/restpack_serializer/serializable.rb
@@ -193,6 +194,7 @@ files:
193
194
  - spec/factory/factory_spec.rb
194
195
  - spec/fixtures/db.rb
195
196
  - spec/fixtures/serializers.rb
197
+ - spec/restpack_serializer_spec.rb
196
198
  - spec/serializable/attributes_spec.rb
197
199
  - spec/serializable/options_spec.rb
198
200
  - spec/serializable/paging_spec.rb
@@ -230,6 +232,7 @@ test_files:
230
232
  - spec/factory/factory_spec.rb
231
233
  - spec/fixtures/db.rb
232
234
  - spec/fixtures/serializers.rb
235
+ - spec/restpack_serializer_spec.rb
233
236
  - spec/serializable/attributes_spec.rb
234
237
  - spec/serializable/options_spec.rb
235
238
  - spec/serializable/paging_spec.rb