jsonapi-serializable 0.1.3 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsonapi/serializable/error.rb +6 -0
- data/lib/jsonapi/serializable/relationship.rb +2 -1
- data/lib/jsonapi/serializable/relationship/dsl.rb +4 -24
- data/lib/jsonapi/serializable/renderer.rb +62 -53
- data/lib/jsonapi/serializable/resource.rb +71 -16
- data/lib/jsonapi/serializable/resource/conditional_fields.rb +82 -42
- data/lib/jsonapi/serializable/resource/dsl.rb +173 -0
- data/lib/jsonapi/serializable/resource/key_format.rb +47 -29
- data/lib/jsonapi/serializable/resource/relationship.rb +62 -0
- data/lib/jsonapi/serializable/resource/relationship/dsl.rb +103 -0
- data/lib/jsonapi/serializable/resource_builder.rb +23 -43
- metadata +12 -12
- data/lib/jsonapi/serializable/fieldset.rb +0 -7
- data/lib/jsonapi/serializable/resource/attributes.rb +0 -73
- data/lib/jsonapi/serializable/resource/id.rb +0 -48
- data/lib/jsonapi/serializable/resource/links.rb +0 -67
- data/lib/jsonapi/serializable/resource/meta.rb +0 -62
- data/lib/jsonapi/serializable/resource/relationships.rb +0 -95
- data/lib/jsonapi/serializable/resource/temp_id.rb +0 -13
- data/lib/jsonapi/serializable/resource/temp_id.rb~ +0 -0
- data/lib/jsonapi/serializable/resource/type.rb +0 -62
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-serializable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Hosseini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jsonapi-renderer
|
@@ -17,6 +17,9 @@ dependencies:
|
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.1.3
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -24,6 +27,9 @@ dependencies:
|
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '0.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.1.3
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: rake
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,22 +82,16 @@ files:
|
|
76
82
|
- lib/jsonapi/serializable.rb
|
77
83
|
- lib/jsonapi/serializable/error.rb
|
78
84
|
- lib/jsonapi/serializable/error_dsl.rb
|
79
|
-
- lib/jsonapi/serializable/fieldset.rb
|
80
85
|
- lib/jsonapi/serializable/link.rb
|
81
86
|
- lib/jsonapi/serializable/relationship.rb
|
82
87
|
- lib/jsonapi/serializable/relationship/dsl.rb
|
83
88
|
- lib/jsonapi/serializable/renderer.rb
|
84
89
|
- lib/jsonapi/serializable/resource.rb
|
85
|
-
- lib/jsonapi/serializable/resource/attributes.rb
|
86
90
|
- lib/jsonapi/serializable/resource/conditional_fields.rb
|
87
|
-
- lib/jsonapi/serializable/resource/
|
91
|
+
- lib/jsonapi/serializable/resource/dsl.rb
|
88
92
|
- lib/jsonapi/serializable/resource/key_format.rb
|
89
|
-
- lib/jsonapi/serializable/resource/
|
90
|
-
- lib/jsonapi/serializable/resource/
|
91
|
-
- lib/jsonapi/serializable/resource/relationships.rb
|
92
|
-
- lib/jsonapi/serializable/resource/temp_id.rb
|
93
|
-
- lib/jsonapi/serializable/resource/temp_id.rb~
|
94
|
-
- lib/jsonapi/serializable/resource/type.rb
|
93
|
+
- lib/jsonapi/serializable/resource/relationship.rb
|
94
|
+
- lib/jsonapi/serializable/resource/relationship/dsl.rb
|
95
95
|
- lib/jsonapi/serializable/resource_builder.rb
|
96
96
|
homepage: https://github.com/jsonapi-rb/jsonapi-serializable
|
97
97
|
licenses:
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
version: '0'
|
114
114
|
requirements: []
|
115
115
|
rubyforge_project:
|
116
|
-
rubygems_version: 2.6.
|
116
|
+
rubygems_version: 2.6.12
|
117
117
|
signing_key:
|
118
118
|
specification_version: 4
|
119
119
|
summary: Conveniently serialize JSON API resources.
|
@@ -1,73 +0,0 @@
|
|
1
|
-
module JSONAPI
|
2
|
-
module Serializable
|
3
|
-
class Resource
|
4
|
-
# Mixin to handle resource attributes.
|
5
|
-
module Attributes
|
6
|
-
def self.prepended(klass)
|
7
|
-
super
|
8
|
-
klass.class_eval do
|
9
|
-
extend DSL
|
10
|
-
class << self
|
11
|
-
attr_accessor :attribute_blocks
|
12
|
-
end
|
13
|
-
self.attribute_blocks = {}
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def initialize(*)
|
18
|
-
super
|
19
|
-
@_attributes = {}
|
20
|
-
end
|
21
|
-
|
22
|
-
# @see JSONAPI::Serializable::Resource#as_jsonapi
|
23
|
-
def as_jsonapi(fields: nil, include: [])
|
24
|
-
super.tap do |hash|
|
25
|
-
attrs =
|
26
|
-
requested_attributes(fields).each_with_object({}) do |(k, v), h|
|
27
|
-
h[k] = instance_eval(&v)
|
28
|
-
end
|
29
|
-
hash[:attributes] = attrs if attrs.any?
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# @api private
|
34
|
-
def requested_attributes(fields)
|
35
|
-
self.class.attribute_blocks
|
36
|
-
.select { |k, _| fields.nil? || fields.include?(k) }
|
37
|
-
end
|
38
|
-
|
39
|
-
# DSL methods for declaring attributes.
|
40
|
-
module DSL
|
41
|
-
def inherited(klass)
|
42
|
-
super
|
43
|
-
klass.attribute_blocks = attribute_blocks.dup
|
44
|
-
end
|
45
|
-
|
46
|
-
# Declare an attribute for this resource.
|
47
|
-
#
|
48
|
-
# @param [Symbol] name The key of the attribute.
|
49
|
-
# @yieldreturn [Hash, String, nil] The block to compute the value.
|
50
|
-
#
|
51
|
-
# @example
|
52
|
-
# attribute(:name) { @object.name }
|
53
|
-
def attribute(name, _options = {}, &block)
|
54
|
-
block ||= proc { @object.public_send(name) }
|
55
|
-
attribute_blocks[name.to_sym] = block
|
56
|
-
end
|
57
|
-
|
58
|
-
# Declare a list of attributes for this resource.
|
59
|
-
#
|
60
|
-
# @param [Array] *args The attributes keys.
|
61
|
-
#
|
62
|
-
# @example
|
63
|
-
# attributes :title, :body, :date
|
64
|
-
def attributes(*args)
|
65
|
-
args.each do |attr|
|
66
|
-
attribute(attr)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
module JSONAPI
|
2
|
-
module Serializable
|
3
|
-
class Resource
|
4
|
-
# Mixin to handle resource id.
|
5
|
-
module Id
|
6
|
-
def self.prepended(klass)
|
7
|
-
super
|
8
|
-
klass.class_eval do
|
9
|
-
extend DSL
|
10
|
-
class << self
|
11
|
-
attr_accessor :id_block
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def initialize(*)
|
17
|
-
super
|
18
|
-
@_id = instance_eval(&self.class.id_block).to_s
|
19
|
-
end
|
20
|
-
|
21
|
-
# @see JSONAPI::Serializable::Resource#as_jsonapi
|
22
|
-
def as_jsonapi(*)
|
23
|
-
super.tap do |hash|
|
24
|
-
hash[:id] = @_id
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# DSL methods for declaring the resource id.
|
29
|
-
module DSL
|
30
|
-
def inherited(klass)
|
31
|
-
super
|
32
|
-
klass.id_block = id_block
|
33
|
-
end
|
34
|
-
|
35
|
-
# Declare the JSON API id of this resource.
|
36
|
-
#
|
37
|
-
# @yieldreturn [String] The id of the resource.
|
38
|
-
#
|
39
|
-
# @example
|
40
|
-
# id { @user.id.to_s }
|
41
|
-
def id(&block)
|
42
|
-
self.id_block = block
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require 'jsonapi/serializable/link'
|
2
|
-
|
3
|
-
module JSONAPI
|
4
|
-
module Serializable
|
5
|
-
class Resource
|
6
|
-
# Mixin for handling resource links.
|
7
|
-
module Links
|
8
|
-
def self.prepended(klass)
|
9
|
-
super
|
10
|
-
klass.class_eval do
|
11
|
-
extend DSL
|
12
|
-
class << self
|
13
|
-
attr_accessor :link_blocks
|
14
|
-
end
|
15
|
-
self.link_blocks = {}
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def initialize(*)
|
20
|
-
super
|
21
|
-
@_links = self.class.link_blocks.each_with_object({}) do |(k, v), h|
|
22
|
-
h[k] = Link.as_jsonapi(@_exposures, &v)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# @see JSONAPI::Serializable::Resource#as_jsonapi
|
27
|
-
def as_jsonapi(*)
|
28
|
-
super.tap do |hash|
|
29
|
-
hash[:links] = @_links if @_links.any?
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# DSL methods for declaring resource links.
|
34
|
-
module DSL
|
35
|
-
def inherited(klass)
|
36
|
-
super
|
37
|
-
klass.link_blocks = link_blocks.dup
|
38
|
-
end
|
39
|
-
|
40
|
-
# Declare a link for this resource. The properties of the link are set
|
41
|
-
# by providing a block in which the DSL methods of
|
42
|
-
# +JSONAPI::Serializable::Link+ are called, or the value of the link
|
43
|
-
# is returned directly.
|
44
|
-
# @see JSONAPI::Serialiable::Link
|
45
|
-
#
|
46
|
-
# @param [Symbol] name The key of the link.
|
47
|
-
# @yieldreturn [Hash, String, nil] The block to compute the value, if
|
48
|
-
# any.
|
49
|
-
#
|
50
|
-
# @example
|
51
|
-
# link(:self) do
|
52
|
-
# "http://api.example.com/users/#{@user.id}"
|
53
|
-
# end
|
54
|
-
#
|
55
|
-
# @example
|
56
|
-
# link(:self) do
|
57
|
-
# href "http://api.example.com/users/#{@user.id}"
|
58
|
-
# meta is_self: true
|
59
|
-
# end
|
60
|
-
def link(name, &block)
|
61
|
-
link_blocks[name] = block
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
module JSONAPI
|
2
|
-
module Serializable
|
3
|
-
class Resource
|
4
|
-
# Mixin for handling resource meta.
|
5
|
-
module Meta
|
6
|
-
def self.prepended(klass)
|
7
|
-
super
|
8
|
-
klass.class_eval do
|
9
|
-
extend DSL
|
10
|
-
class << self
|
11
|
-
attr_accessor :meta_val, :meta_block
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def initialize(*)
|
17
|
-
super
|
18
|
-
@_meta = if self.class.meta_block
|
19
|
-
instance_eval(&self.class.meta_block)
|
20
|
-
else
|
21
|
-
self.class.meta_val
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
# @see JSONAPI::Serializable::Resource#as_jsonapi
|
26
|
-
def as_jsonapi(*)
|
27
|
-
super.tap do |hash|
|
28
|
-
hash[:meta] = @_meta unless @_meta.nil?
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# DSL methods for declaring resource meta.
|
33
|
-
module DSL
|
34
|
-
def inherited(klass)
|
35
|
-
super
|
36
|
-
klass.meta_val = meta_val
|
37
|
-
klass.meta_block = meta_block
|
38
|
-
end
|
39
|
-
|
40
|
-
# @overload meta(value)
|
41
|
-
# Declare the meta information for this resource.
|
42
|
-
# @param [Hash] value The meta information hash.
|
43
|
-
#
|
44
|
-
# @example
|
45
|
-
# meta key: value
|
46
|
-
#
|
47
|
-
# @overload meta(&block)
|
48
|
-
# Declare the meta information for this resource.
|
49
|
-
# @yieldreturn [String] The meta information hash.
|
50
|
-
# @example
|
51
|
-
# meta do
|
52
|
-
# { key: value }
|
53
|
-
# end
|
54
|
-
def meta(value = nil, &block)
|
55
|
-
self.meta_val = value
|
56
|
-
self.meta_block = block
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
require 'jsonapi/serializable/relationship'
|
2
|
-
|
3
|
-
module JSONAPI
|
4
|
-
module Serializable
|
5
|
-
class Resource
|
6
|
-
# Mixin to handle resource relationships.
|
7
|
-
module Relationships
|
8
|
-
def self.prepended(klass)
|
9
|
-
super
|
10
|
-
klass.class_eval do
|
11
|
-
extend DSL
|
12
|
-
class << self
|
13
|
-
attr_accessor :relationship_blocks
|
14
|
-
end
|
15
|
-
self.relationship_blocks = {}
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def initialize(*)
|
20
|
-
super
|
21
|
-
@_relationships = self.class.relationship_blocks
|
22
|
-
.each_with_object({}) do |(k, v), h|
|
23
|
-
h[k] = Relationship.new(@_exposures, &v)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# @see JSONAPI::Serializable::Resource#as_jsonapi
|
28
|
-
def as_jsonapi(fields: nil, include: [])
|
29
|
-
super.tap do |hash|
|
30
|
-
rels = requested_relationships(fields)
|
31
|
-
.each_with_object({}) do |(k, v), h|
|
32
|
-
h[k] = v.as_jsonapi(include.include?(k))
|
33
|
-
end
|
34
|
-
hash[:relationships] = rels if rels.any?
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# @api private
|
39
|
-
def requested_relationships(fields)
|
40
|
-
@_relationships
|
41
|
-
.select { |k, _| fields.nil? || fields.include?(k) }
|
42
|
-
end
|
43
|
-
|
44
|
-
# DSL methods for declaring relationships.
|
45
|
-
module DSL
|
46
|
-
def inherited(klass)
|
47
|
-
super
|
48
|
-
klass.relationship_blocks = relationship_blocks.dup
|
49
|
-
end
|
50
|
-
|
51
|
-
# Declare a relationship for this resource. The properties of the
|
52
|
-
# relationship are set by providing a block in which the DSL methods
|
53
|
-
# of +JSONAPI::Serializable::Relationship+ are called.
|
54
|
-
# @see JSONAPI::Serializable::Relationship
|
55
|
-
#
|
56
|
-
# @param [Symbol] name The key of the relationship.
|
57
|
-
#
|
58
|
-
# @example
|
59
|
-
# relationship :posts do
|
60
|
-
# resources { @user.posts.map { |p| PostResource.new(post: p) } }
|
61
|
-
# end
|
62
|
-
#
|
63
|
-
# @example
|
64
|
-
# relationship :author do
|
65
|
-
# resources do
|
66
|
-
# @post.author && UserResource.new(user: @post.author)
|
67
|
-
# end
|
68
|
-
# data do
|
69
|
-
# { type: 'users', id: @post.author_id }
|
70
|
-
# end
|
71
|
-
# link(:self) do
|
72
|
-
# "http://api.example.com/posts/#{@post.id}/relationships/author"
|
73
|
-
# end
|
74
|
-
# link(:related) do
|
75
|
-
# "http://api.example.com/posts/#{@post.id}/author"
|
76
|
-
# end
|
77
|
-
# meta do
|
78
|
-
# { author_online: @post.author.online? }
|
79
|
-
# end
|
80
|
-
# end
|
81
|
-
def relationship(name, options = {}, &block)
|
82
|
-
rel_block = proc do
|
83
|
-
data(options[:class]) { @object.public_send(name) }
|
84
|
-
instance_eval(&block) unless block.nil?
|
85
|
-
end
|
86
|
-
relationship_blocks[name.to_sym] = rel_block
|
87
|
-
end
|
88
|
-
alias has_many relationship
|
89
|
-
alias has_one relationship
|
90
|
-
alias belongs_to relationship
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
File without changes
|