collection_json_serializer 0.3.6 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0663e720fc3843ee8c028d9d3d6dc8e060792217
4
- data.tar.gz: 8a789413bcff39e3d7df648b027472a5b568a2a5
3
+ metadata.gz: a3261f2fef5892978bcfe9c28b592538995524b2
4
+ data.tar.gz: 006a703ad51bc4914e2bbc3c6d6199c9c5d0789a
5
5
  SHA512:
6
- metadata.gz: 059b0bd27cd307e3347e9699829daeafbb16a96d6819216cdfe899c8c42ff2e9897f3bb069bc190608dcf46ba7831a74468294716adbb9047307e841bbe48c1f
7
- data.tar.gz: e2bc1b7708f4f7f69bfb25329e0394e0bdc5ffff6b067b3e0857110749cae802a30b07dc1ea535814bd727fcc42030a10771089e096cdf4ca0d20910c9f6b0fc
6
+ metadata.gz: c72dd15101792c1773f136d6ec5f954b2b42b28d9aaec2b7e269885dd8a2c4bd53635dc5af1193552f7e910da4c0dd748d2257a0d90c70c943803ee7b0143710
7
+ data.tar.gz: dc76bbc18ec5c33fd613624553cb9927d300a0ff8285ea5d872d769821c71296fbfad6873538da0b7e94c70661b0bb669cec9bd636dc0f6c63543160cfa5b043
data/README.md CHANGED
@@ -4,11 +4,16 @@
4
4
 
5
5
  | :warning: This is _not finished_ yet, so use it at your own risk. |
6
6
  --------------------------------------------------------------------
7
+ | :warning: Until version 1.X breaking changes might happen |
8
+ -------------------------------------------------------------
7
9
 
8
10
  CollectionJson::Serializer serializes Ruby objects to Collection+JSON, the hypermedia type by Mike Amudsen.
9
11
 
10
12
  Please note that CollectionJson::Serializer _only serializes data_. You still need to set the proper Headers or media-types in your app.
11
13
 
14
+ If you're working on a Rails app, you might want to use [Collection+JSON
15
+ Rails](https://github.com/carlesjove/collection_json_rails) instead.
16
+
12
17
  ## Installation
13
18
 
14
19
  Add this line to your application's Gemfile:
@@ -32,16 +37,14 @@ class UserSerializer < CollectionJson::Serializer
32
37
 
33
38
  template :name
34
39
  template email: { prompt: "My email" }
35
- # This could be written in a single line, too, wrapping the hash:
36
- # template :name, { email: { ... } }
37
40
 
38
- # Please note that links can only be passed as hashes
39
- links dashboard: { href: "http://example.com/my-dashboard" }
41
+ link dashboard: { href: "http://example.com/my-dashboard" }
40
42
 
41
- queries search: {
43
+ query search: {
42
44
  href: "http://example.com/search",
43
45
  name: false # Don't automatically include the name attribute
44
- }, pagination: {
46
+ }
47
+ query pagination: {
45
48
  rel: "page",
46
49
  href: "http://example.com/page",
47
50
  prompt: "Select a page number",
@@ -50,9 +53,13 @@ class UserSerializer < CollectionJson::Serializer
50
53
  ]
51
54
  }
52
55
 
53
- item do
54
- attributes :id, name: { prompt: "Your full name" }, :email
56
+ items do
57
+ attribute :id
58
+ attribute name: { prompt: "Your full name" }}
59
+ attribute :email
60
+
55
61
  href "http://example.com/users/{id}"
62
+
56
63
  link avatar: { href: "http://assets.example.com/avatar.jpg", render: "image" }
57
64
  end
58
65
  end
@@ -67,7 +74,7 @@ Then, you pass your objects to the serializer:
67
74
  user_serializer = UserSerializer.new(@user)
68
75
 
69
76
  # You can also pass an array of objects
70
- # user_serializer = UserSerializer.new([@user1, @user2, etc])
77
+ # user_serializer = UserSerializer.new([@user1, @user2])
71
78
 
72
79
  # Pass the serializer to the builder
73
80
  collection = CollectionJson::Serializer::Builder.new(user_serializer)
@@ -139,10 +146,10 @@ All placeholders will be called, so you can use more than one if necessary, but
139
146
  ```ruby
140
147
  class UserSerializer < CollectionJson::Serializer
141
148
  items do
142
- # This is ok
149
+ # This will work
143
150
  href "http://example.com/users/{id}/{username}"
144
151
 
145
- # This is wrong
152
+ # This won't work
146
153
  href "http://example.com/users/{id}-{username}"
147
154
  end
148
155
  end
@@ -163,19 +170,16 @@ class UserSerializer < CollectionJson::Serializer
163
170
 
164
171
  # Now you can use your crazy properties everywhere
165
172
  items do
166
- attributes :id, name: { css_class: "people" }
173
+ attribute name: { css_class: "people" }
167
174
  end
168
175
 
169
176
  template name: { regex: "/\A[a-zA-Z0-9_]*\z/" }
170
177
 
171
- links profile: { on_click: "reboot_universe" }
178
+ link profile: { on_click: "reboot_universe" }
172
179
  end
173
180
  ```
174
181
 
175
182
  ## Contributing
176
183
 
177
- 1. Fork it ( https://github.com/[my-github-username]/collection_json_serializer/fork )
178
- 2. Create your feature branch (`git checkout -b my-new-feature`)
179
- 3. Commit your changes (`git commit -am 'Add some feature'`)
180
- 4. Push to the branch (`git push origin my-new-feature`)
181
- 5. Create a new Pull Request
184
+ Please, all Pull Request should point to the `dev` branch. `master` is for the
185
+ latest release only.
@@ -18,6 +18,11 @@ module CollectionJson
18
18
  @attributes ||= args
19
19
  end
20
20
 
21
+ def attribute(args)
22
+ @attributes = Array.new unless @attributes.is_a?(Array)
23
+ @attributes << args
24
+ end
25
+
21
26
  def link(args)
22
27
  @links = Array.new unless @links.is_a?(Array)
23
28
  @links << args
@@ -4,9 +4,9 @@ module CollectionJson
4
4
  class Query
5
5
  def initialize(serializer, item: 0)
6
6
  @serializer = serializer
7
- @index = item >= 0 ? item : 0
8
- @key = @serializer.queries.first.keys[@index]
9
- @resource = @serializer.queries.first[@key]
7
+ index = item >= 0 ? item : 0
8
+ @key = @serializer.queries[index].keys.first
9
+ @resource = @serializer.queries[index].fetch(@key)
10
10
  @query = Hash.new
11
11
  end
12
12
 
@@ -1,40 +1,53 @@
1
1
  module CollectionJson
2
2
  class Serializer
3
3
  class << self
4
- attr_accessor :extensions
5
- attr_accessor :href
6
- attr_accessor :template
7
- attr_accessor :links
8
- attr_accessor :queries
4
+ attr_accessor :_extensions
5
+ attr_accessor :_href
6
+ attr_accessor :_template
7
+ attr_accessor :_links
8
+ attr_accessor :_queries
9
9
  attr_accessor :_items
10
10
  end
11
11
 
12
12
  def self.inherited(base)
13
- base.extensions = []
14
- base.href = []
15
- base.template = []
16
- base.links = []
17
- base.queries = []
13
+ base._extensions = []
14
+ base._href = []
15
+ base._template = []
16
+ base._links = []
17
+ base._queries = []
18
18
  end
19
19
 
20
20
  def self.extensions(*attrs)
21
- @extensions.concat attrs
21
+ @_extensions.concat attrs
22
22
  end
23
23
 
24
24
  def self.href(*attrs)
25
- @href.concat attrs
25
+ @_href.concat attrs
26
26
  end
27
27
 
28
28
  def self.template(*attrs)
29
- @template.concat attrs
29
+ @_template.concat attrs
30
30
  end
31
31
 
32
+ def self.link(*attrs)
33
+ @_links.concat attrs
34
+ end
35
+
36
+ # DEPRECATED
32
37
  def self.links(*attrs)
33
- @links.concat attrs
38
+ warn "links has been deprecated and will be removed soon. " <<
39
+ "Please, use `link` instead"
40
+ @_links.concat attrs
41
+ end
42
+
43
+ def self.query(*attrs)
44
+ @_queries.concat attrs
34
45
  end
35
46
 
36
47
  def self.queries(*attrs)
37
- @queries.concat attrs
48
+ warn "`queries` has been deprecated and will be removed soon. " <<
49
+ "Please, use `query` instead"
50
+ @_queries.concat attrs
38
51
  end
39
52
 
40
53
  def self.items(&block)
@@ -53,35 +66,35 @@ module CollectionJson
53
66
  end
54
67
 
55
68
  def extensions
56
- self.class.extensions
69
+ self.class._extensions
57
70
  end
58
71
 
59
72
  def href
60
- self.class.href.first
73
+ self.class._href.first
61
74
  end
62
75
 
63
76
  def template
64
- self.class.template
77
+ self.class._template
65
78
  end
66
79
 
67
80
  def template?
68
- self.class.template.present?
81
+ self.class._template.present?
69
82
  end
70
83
 
71
84
  def links
72
- self.class.links
85
+ self.class._links
73
86
  end
74
87
 
75
88
  def links?
76
- self.class.links.present?
89
+ self.class._links.present?
77
90
  end
78
91
 
79
92
  def queries
80
- self.class.queries
93
+ self.class._queries
81
94
  end
82
95
 
83
96
  def queries?
84
- self.class.queries.present?
97
+ self.class._queries.present?
85
98
  end
86
99
 
87
100
  def items
@@ -1,5 +1,5 @@
1
1
  module CollectionJson
2
2
  class Serializer
3
- VERSION = "0.3.6"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -73,6 +73,15 @@ module CollectionJson
73
73
  {
74
74
  rel: "search",
75
75
  href: "http://example.com/search"
76
+ },
77
+ {
78
+ rel: "page",
79
+ href: "http://example.com/page",
80
+ name: "pagination",
81
+ prompt: "Select a page number",
82
+ data: [
83
+ { name: "page", value: "" }
84
+ ]
76
85
  }
77
86
  ]
78
87
  }
@@ -168,6 +177,15 @@ module CollectionJson
168
177
  {
169
178
  rel: "search",
170
179
  href: "http://example.com/search"
180
+ },
181
+ {
182
+ rel: "page",
183
+ href: "http://example.com/page",
184
+ name: "pagination",
185
+ prompt: "Select a page number",
186
+ data: [
187
+ { name: "page", value: "" }
188
+ ]
171
189
  }
172
190
  ]
173
191
  }
@@ -8,8 +8,8 @@ module CollectionJson
8
8
  def setup
9
9
  @user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
10
10
  @serializer = empty_serializer_for(@user)
11
- @serializer.class.extensions = [:open_attrs]
12
- @serializer.class.template = [
11
+ @serializer.class._extensions = [:open_attrs]
12
+ @serializer.class._template = [
13
13
  email: {
14
14
  prompt: "My email",
15
15
  anything: "at all"
@@ -27,13 +27,13 @@ module CollectionJson
27
27
  anything: "at all"
28
28
  }
29
29
  ]
30
- @serializer.class.links = [
30
+ @serializer.class._links = [
31
31
  dashboard: {
32
32
  href: "http://example.com/my-dashboard",
33
33
  anything: "at all"
34
34
  }
35
35
  ]
36
- @serializer.class.queries = [
36
+ @serializer.class._queries = [
37
37
  search: {
38
38
  href: "http://example.com/search",
39
39
  rel: "search",
@@ -1,4 +1,4 @@
1
1
  class InvalidSerializer < CollectionJson::Serializer
2
2
  href self: "/users/1", collection: "www.users.com"
3
- links dashboard: { href: "/my-dashboard" }
3
+ link dashboard: { href: "/my-dashboard" }
4
4
  end
@@ -5,12 +5,13 @@ class UserSerializer < CollectionJson::Serializer
5
5
  template email: { prompt: "My email" }
6
6
  template :password
7
7
 
8
- links dashboard: { href: "http://example.com/my-dashboard" }
8
+ link dashboard: { href: "http://example.com/my-dashboard" }
9
9
 
10
- queries search: {
10
+ query search: {
11
11
  href: "http://example.com/search",
12
12
  name: false
13
- }, pagination: {
13
+ }
14
+ query pagination: {
14
15
  rel: "page",
15
16
  href: "http://example.com/page",
16
17
  prompt: "Select a page number",
@@ -22,6 +23,7 @@ class UserSerializer < CollectionJson::Serializer
22
23
  items do
23
24
  href "http://example.com/users/{id}"
24
25
  attributes :name, :email
26
+ attribute date_created: { prompt: "Member since" }
25
27
  link avatar: { href: "http://assets.example.com/avatar.jpg" }
26
28
  link bio: { href: "http://example.com/bio" }
27
29
  end
@@ -1,5 +1,5 @@
1
1
  class ValidSerializer < CollectionJson::Serializer
2
2
  href self: "http://example.com/users/1",
3
3
  collection: "http://example.com/users"
4
- links dashboard: { href: "http://example.com/my-dashboard" }
4
+ link dashboard: { href: "http://example.com/my-dashboard" }
5
5
  end
@@ -10,11 +10,11 @@ Dir.glob(File.dirname(__FILE__) + "/fixtures/serializers/**/*.rb") { |file| requ
10
10
  module TestHelper
11
11
  def empty_serializer_for(object)
12
12
  serializer = CollectionJson::Serializer.new(object)
13
- serializer.class.extensions = []
14
- serializer.class.href = []
15
- serializer.class.links = []
16
- serializer.class.template = []
17
- serializer.class.queries = []
13
+ serializer.class._extensions = []
14
+ serializer.class._href = []
15
+ serializer.class._links = []
16
+ serializer.class._template = []
17
+ serializer.class._queries = []
18
18
  serializer.class.items {}
19
19
  serializer.items.attributes = []
20
20
  serializer
@@ -8,8 +8,10 @@ module CollectionJson
8
8
  include TestHelper
9
9
 
10
10
  def setup
11
- @user1 = User.new(name: "Carles Jove", email: "hola@carlus.cat")
12
- @user2 = User.new(name: "Aina Jove", email: "hola@example.com")
11
+ @user1 = User.new(name: "Carles Jove", email: "hola@carlus.cat",
12
+ date_created: "2015-02-01")
13
+ @user2 = User.new(name: "Aina Jove", email: "hola@example.com",
14
+ date_created: "2015-02-02")
13
15
  @user_serializer = UserSerializer.new(@user1)
14
16
  @item = Item.new(@user_serializer)
15
17
  end
@@ -19,7 +21,12 @@ module CollectionJson
19
21
  href: "http://example.com/users/#{@user1.id}",
20
22
  data: [
21
23
  { name: "name", value: "Carles Jove" },
22
- { name: "email", value: "hola@carlus.cat" }
24
+ { name: "email", value: "hola@carlus.cat" },
25
+ {
26
+ name: "date_created",
27
+ value: "2015-02-01",
28
+ prompt: "Member since"
29
+ }
23
30
  ],
24
31
  links: [
25
32
  {
@@ -46,7 +53,12 @@ module CollectionJson
46
53
  href: "http://example.com/users/#{@user2.id}",
47
54
  data: [
48
55
  { name: "name", value: "Aina Jove" },
49
- { name: "email", value: "hola@example.com" }
56
+ { name: "email", value: "hola@example.com" },
57
+ {
58
+ name: "date_created",
59
+ value: "2015-02-02",
60
+ prompt: "Member since"
61
+ }
50
62
  ],
51
63
  links: [
52
64
  {
@@ -4,7 +4,8 @@ module CollectionJson
4
4
  class Serializer
5
5
  class TestDSL < Minitest::Test
6
6
  def setup
7
- @user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
7
+ @user = User.new(name: "Carles Jove", email: "hola@carlus.cat",
8
+ date_created: "2015-02-01")
8
9
  @serializer = Serializer.new(@user)
9
10
  end
10
11
 
@@ -11,14 +11,14 @@ module CollectionJson
11
11
  end
12
12
 
13
13
  def test_href_object
14
- assert_equal ["http://example.com/users"], @user_serializer.class.href
14
+ assert_equal ["http://example.com/users"], @user_serializer.class._href
15
15
  end
16
16
 
17
17
  def test_that_only_one_href_value_is_passed_to_builder
18
18
  serializer = empty_serializer_for(@user)
19
- serializer.class.href = %w(/a /b /c)
19
+ serializer.class._href = %w(/a /b /c)
20
20
 
21
- assert_equal %w(/a /b /c), serializer.class.href
21
+ assert_equal %w(/a /b /c), serializer.class._href
22
22
  assert_equal "/a", serializer.href
23
23
  end
24
24
  end
@@ -4,12 +4,14 @@ module CollectionJson
4
4
  class Serializer
5
5
  class TestAttributes < Minitest::Test
6
6
  def setup
7
- @user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
7
+ @user = User.new(name: "Carles Jove", email: "hola@carlus.cat",
8
+ date_created: "2015-02-01")
8
9
  @user_serializer = UserSerializer.new(@user)
9
10
  end
10
11
 
11
12
  def test_attributes_properties
12
- assert_equal [:name, :email], @user_serializer.items.attributes
13
+ expected_attributes = [:name, :email, { date_created: { prompt: "Member since" } }]
14
+ assert_equal expected_attributes, @user_serializer.items.attributes
13
15
  end
14
16
  end
15
17
  end
@@ -12,7 +12,7 @@ module CollectionJson
12
12
 
13
13
  def test_item_links_attributes
14
14
  expected = [dashboard: { href: "http://example.com/my-dashboard" }]
15
- assert_equal expected, @user_serializer.class.links
15
+ assert_equal expected, @user_serializer.class._links
16
16
  end
17
17
  end
18
18
  end
@@ -10,21 +10,23 @@ module CollectionJson
10
10
 
11
11
  def test_template_attributes
12
12
  expected = [
13
- search: {
14
- href: "http://example.com/search",
15
- name: false
13
+ { search: {
14
+ href: "http://example.com/search",
15
+ name: false
16
+ }
16
17
  },
17
- pagination: {
18
- rel: "page",
19
- href: "http://example.com/page",
20
- prompt: "Select a page number",
21
- data: [
22
- { name: "page" }
23
- ]
18
+ { pagination: {
19
+ rel: "page",
20
+ href: "http://example.com/page",
21
+ prompt: "Select a page number",
22
+ data: [
23
+ { name: "page" }
24
+ ]
25
+ }
24
26
  }
25
27
  ]
26
28
 
27
- assert_equal expected, @user_serializer.class.queries
29
+ assert_equal expected, @user_serializer.class._queries
28
30
  end
29
31
  end
30
32
  end
@@ -10,16 +10,16 @@ module CollectionJson
10
10
  serializer.items.attributes = [
11
11
  name: { unknown: "this should not be valid" }
12
12
  ]
13
- serializer.items.links = serializer.class.links = [
13
+ serializer.items.links = serializer.class._links = [
14
14
  dashboard: {
15
15
  href: "http://example.com",
16
16
  unknown: "this should not be valid"
17
17
  }
18
18
  ]
19
- serializer.class.template = [
19
+ serializer.class._template = [
20
20
  name: { whatever: "this should not be valid" }
21
21
  ]
22
- serializer.class.queries = [
22
+ serializer.class._queries = [
23
23
  name: {
24
24
  href: "http://example.com",
25
25
  whatever: "this should be invalid",
@@ -23,7 +23,7 @@ module CollectionJson
23
23
 
24
24
  # Href
25
25
  def test_that_href_generates_errors
26
- @invalid.class.href = [self: "/users/1", collection: "www.users.com"]
26
+ @invalid.class._href = [self: "/users/1", collection: "www.users.com"]
27
27
 
28
28
  assert @invalid.invalid?
29
29
  assert @invalid.errors.include? :href
@@ -32,7 +32,7 @@ module CollectionJson
32
32
  assert @invalid.errors[:href][1].
33
33
  include? "href:collection is an invalid URL"
34
34
 
35
- @invalid.class.href = ["/users/1"]
35
+ @invalid.class._href = ["/users/1"]
36
36
 
37
37
  assert @invalid.invalid?
38
38
  assert @invalid.errors.include? :href
@@ -42,13 +42,13 @@ module CollectionJson
42
42
 
43
43
  # Links
44
44
  def test_that_links_generates_errors
45
- @invalid.class.links = [dashboard: { href: "/my-dashboard" }]
45
+ @invalid.class._links = [dashboard: { href: "/my-dashboard" }]
46
46
  assert @invalid.invalid?
47
47
  assert @invalid.errors.include? :links
48
48
  assert @invalid.errors[:links].first.
49
49
  include? "links:dashboard:href is an invalid URL"
50
50
 
51
- @invalid.class.links = [
51
+ @invalid.class._links = [
52
52
  dashboard: {
53
53
  href: "http://valid.url.com",
54
54
  prompt: /invalid/
@@ -61,7 +61,7 @@ module CollectionJson
61
61
  end
62
62
 
63
63
  def test_that_links_missing_href_generates_error
64
- @invalid.class.links = [dashboard: {}]
64
+ @invalid.class._links = [dashboard: {}]
65
65
  assert @invalid.invalid?
66
66
  assert @invalid.errors.include? :links
67
67
  assert @invalid.errors[:links].first.
@@ -103,7 +103,7 @@ module CollectionJson
103
103
  # Template
104
104
  def test_that_template_values_validate
105
105
  values_for_test(:invalid).each do |invalidate|
106
- @invalid.class.template = [
106
+ @invalid.class._template = [
107
107
  name: {
108
108
  prompt: invalidate,
109
109
  name: invalidate
@@ -123,7 +123,7 @@ module CollectionJson
123
123
 
124
124
  # Queries
125
125
  def test_that_queries_validate_href_format
126
- @invalid.class.queries = [
126
+ @invalid.class._queries = [
127
127
  search: {
128
128
  href: "not-valid"
129
129
  }
@@ -138,7 +138,7 @@ module CollectionJson
138
138
  end
139
139
 
140
140
  def test_that_queries_href_is_required
141
- @invalid.class.queries = [
141
+ @invalid.class._queries = [
142
142
  search: {
143
143
  name: "missing href"
144
144
  }
@@ -153,7 +153,7 @@ module CollectionJson
153
153
 
154
154
  def test_that_queries_values_are_validated
155
155
  values_for_test(:invalid).each do |invalidate|
156
- @invalid.class.queries = [
156
+ @invalid.class._queries = [
157
157
  search: {
158
158
  href: "http://example.com/",
159
159
  name: invalidate,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collection_json_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carles Jove i Buxeda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-09 00:00:00.000000000 Z
11
+ date: 2015-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport