jsonapi-serializable 0.1.1.beta4 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
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.1.beta4
4
+ version: 0.1.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: 2016-11-18 00:00:00.000000000 Z
11
+ date: 2016-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-renderer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.1.beta3
19
+ version: 0.1.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.1.beta3
26
+ version: 0.1.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
55
69
  description: Powerful DSL for building resource classes - efficient and flexible rendering.
56
70
  email: lucas.hosseini@gmail.com
57
71
  executables: []
@@ -60,18 +74,24 @@ extra_rdoc_files: []
60
74
  files:
61
75
  - README.md
62
76
  - lib/jsonapi/serializable.rb
63
- - lib/jsonapi/serializable/abstract_resource.rb
64
- - lib/jsonapi/serializable/abstract_resource_dsl.rb
65
77
  - lib/jsonapi/serializable/error.rb
66
78
  - lib/jsonapi/serializable/error_dsl.rb
79
+ - lib/jsonapi/serializable/fieldset.rb
67
80
  - lib/jsonapi/serializable/link.rb
68
81
  - lib/jsonapi/serializable/relationship.rb
69
- - lib/jsonapi/serializable/relationship_dsl.rb
82
+ - lib/jsonapi/serializable/relationship/dsl.rb
70
83
  - lib/jsonapi/serializable/renderer.rb
71
84
  - lib/jsonapi/serializable/resource.rb
85
+ - lib/jsonapi/serializable/resource/attributes.rb
86
+ - lib/jsonapi/serializable/resource/conditional_fields.rb
87
+ - lib/jsonapi/serializable/resource/id.rb
88
+ - lib/jsonapi/serializable/resource/key_transform.rb
89
+ - lib/jsonapi/serializable/resource/links.rb
90
+ - lib/jsonapi/serializable/resource/meta.rb
91
+ - lib/jsonapi/serializable/resource/relationships.rb
92
+ - lib/jsonapi/serializable/resource/type.rb
72
93
  - lib/jsonapi/serializable/resource_builder.rb
73
- - lib/jsonapi/serializable/resource_dsl.rb
74
- homepage: https://github.com/jsonapi-rb/serializable
94
+ homepage: https://github.com/jsonapi-rb/jsonapi-serializable
75
95
  licenses:
76
96
  - MIT
77
97
  metadata: {}
@@ -86,12 +106,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
106
  version: '0'
87
107
  required_rubygems_version: !ruby/object:Gem::Requirement
88
108
  requirements:
89
- - - ">"
109
+ - - ">="
90
110
  - !ruby/object:Gem::Version
91
- version: 1.3.1
111
+ version: '0'
92
112
  requirements: []
93
113
  rubyforge_project:
94
- rubygems_version: 2.5.1
114
+ rubygems_version: 2.6.8
95
115
  signing_key:
96
116
  specification_version: 4
97
117
  summary: Conveniently serialize JSON API resources.
@@ -1,122 +0,0 @@
1
- require 'jsonapi/serializable/link'
2
- require 'jsonapi/serializable/relationship'
3
- require 'jsonapi/serializable/abstract_resource_dsl'
4
-
5
- module JSONAPI
6
- module Serializable
7
- class AbstractResource
8
- include AbstractResourceDSL
9
-
10
- class << self
11
- attr_accessor :id_block,
12
- :type_val, :type_block,
13
- :meta_val, :meta_block,
14
- :attribute_blocks,
15
- :relationship_blocks,
16
- :link_blocks
17
- end
18
-
19
- self.attribute_blocks = {}
20
- self.relationship_blocks = {}
21
- self.link_blocks = {}
22
-
23
- def self.inherited(klass)
24
- super
25
- klass.type_val = type_val
26
- klass.type_block = type_block
27
- klass.id_block = id_block
28
- klass.meta_val = meta_val
29
- klass.meta_block = meta_block
30
- klass.attribute_blocks = attribute_blocks.dup
31
- klass.relationship_blocks = relationship_blocks.dup
32
- klass.link_blocks = link_blocks.dup
33
- end
34
-
35
- def initialize(exposures = {})
36
- exposures.each { |k, v| instance_variable_set("@#{k}", v) }
37
- @_exposures = exposures
38
- @_type = _type
39
- @_id = _id
40
- @_attributes = {}
41
- @_relationships = _relationships
42
- @_meta = _meta
43
- @_links = _links
44
- end
45
-
46
- def as_jsonapi(fields: nil, include: [])
47
- {}.tap do |hash|
48
- hash[:id] = @_id
49
- hash[:type] = @_type
50
- hash[:links] = @_links if @_links.any?
51
- hash[:meta] = @_meta unless @_meta.nil?
52
-
53
- attrs = requested_attributes(fields)
54
- hash[:attributes] = attrs if attrs.any?
55
-
56
- rels = requested_relationships(fields, include)
57
- hash[:relationships] = rels if rels.any?
58
- end
59
- end
60
-
61
- def jsonapi_type
62
- @_type
63
- end
64
-
65
- def jsonapi_id
66
- @_id
67
- end
68
-
69
- def jsonapi_related(include)
70
- @_relationships
71
- .select { |k, _| include.include?(k) }
72
- .each_with_object({}) { |(k, v), h| h[k] = v.related_resources }
73
- end
74
-
75
- private
76
-
77
- def _type
78
- self.class.type_val || instance_eval(&self.class.type_block)
79
- end
80
-
81
- def _id
82
- instance_eval(&self.class.id_block)
83
- end
84
-
85
- def _relationships
86
- self.class.relationship_blocks
87
- .each_with_object({}) do |(k, v), h|
88
- h[k] = Relationship.new(@_exposures, &v)
89
- end
90
- end
91
-
92
- def _meta
93
- if self.class.meta_block
94
- instance_eval(&self.class.meta_block)
95
- else
96
- self.class.meta_val
97
- end
98
- end
99
-
100
- def _links
101
- self.class.link_blocks
102
- .each_with_object({}) do |(k, v), h|
103
- h[k] = Link.as_jsonapi(@_exposures, &v)
104
- end
105
- end
106
-
107
- def requested_attributes(fields)
108
- self.class.attribute_blocks
109
- .select { |k, _| fields.nil? || fields.include?(k) }
110
- .each_with_object({}) { |(k, v), h| h[k] = instance_eval(&v) }
111
- end
112
-
113
- def requested_relationships(fields, include)
114
- @_relationships
115
- .select { |k, _| fields.nil? || fields.include?(k) }
116
- .each_with_object({}) do |(k, v), h|
117
- h[k] = v.as_jsonapi(include.include?(k))
118
- end
119
- end
120
- end
121
- end
122
- end
@@ -1,130 +0,0 @@
1
- module JSONAPI
2
- module Serializable
3
- module AbstractResourceDSL
4
- def self.included(base)
5
- base.extend(ClassMethods)
6
- end
7
-
8
- module ClassMethods
9
- # @overload type(value)
10
- # Declare the JSON API type of this resource.
11
- # @param [String] value The value of the type.
12
- #
13
- # @example
14
- # type 'users'
15
- #
16
- # @overload type(value)
17
- # Declare the JSON API type of this resource.
18
- # @yieldreturn [String] The value of the type.
19
- #
20
- # @example
21
- # type { @user.admin? ? "admin" : "users" }
22
- def type(value = nil, &block)
23
- self.type_val = value
24
- self.type_block = block
25
- end
26
-
27
- # Declare the JSON API id of this resource.
28
- #
29
- # @yieldreturn [String] The id of the resource.
30
- #
31
- # @example
32
- # id { @user.id.to_s }
33
- def id(&block)
34
- self.id_block = block
35
- end
36
-
37
- # @overload meta(value)
38
- # Declare the meta information for this resource.
39
- # @param [Hash] value The meta information hash.
40
- #
41
- # @example
42
- # meta key: value
43
- #
44
- # @overload meta(&block)
45
- # Declare the meta information for this resource.
46
- # @yieldreturn [String] The meta information hash.
47
- # @example
48
- # meta do
49
- # { key: value }
50
- # end
51
- def meta(value = nil, &block)
52
- self.meta_val = value
53
- self.meta_block = block
54
- end
55
-
56
- # Declare an attribute for this resource.
57
- #
58
- # @param [Symbol] name The key of the attribute.
59
- # @yieldreturn [Hash, String, nil] The block to compute the value.
60
- #
61
- # @example
62
- # attribute(:name) { @user.name }
63
- def attribute(name, &block)
64
- attribute_blocks[name] = block
65
- end
66
-
67
- # Declare a relationship for this resource. The properties of the
68
- # relationship are set by providing a block in which the DSL methods
69
- # of +JSONAPI::Serializable::Relationship+ are called.
70
- # @see JSONAPI::Serializable::Relationship
71
- #
72
- # @param [Symbol] name The key of the relationship.
73
- #
74
- # @example
75
- # relationship :posts do
76
- # data { @user.posts.map { |p| PostResource.new(post: p) } }
77
- # end
78
- #
79
- # @example
80
- # relationship :author do
81
- # data do
82
- # @post.author && UserResource.new(user: @post.author)
83
- # end
84
- # linkage_data do
85
- # { type: 'users', id: @post.author_id }
86
- # end
87
- # link(:self) do
88
- # "http://api.example.com/posts/#{@post.id}/relationships/author"
89
- # end
90
- # link(:related) do
91
- # "http://api.example.com/posts/#{@post.id}/author"
92
- # end
93
- # meta do
94
- # { author_online: @post.author.online? }
95
- # end
96
- # end
97
- def relationship(name, &block)
98
- relationship_blocks[name] = block
99
- end
100
- alias has_many relationship
101
- alias has_one relationship
102
- alias belongs_to relationship
103
-
104
- # Declare a link for this resource. The properties of the link are set
105
- # by providing a block in which the DSL methods of
106
- # +JSONAPI::Serializable::Link+ are called, or the value of the link
107
- # is returned directly.
108
- # @see JSONAPI::Serialiable::Link
109
- #
110
- # @param [Symbol] name The key of the link.
111
- # @yieldreturn [Hash, String, nil] The block to compute the value, if
112
- # any.
113
- #
114
- # @example
115
- # link(:self) do
116
- # "http://api.example.com/users/#{@user.id}"
117
- # end
118
- #
119
- # @example
120
- # link(:self) do
121
- # href "http://api.example.com/users/#{@user.id}"
122
- # meta is_self: true
123
- # end
124
- def link(name, &block)
125
- link_blocks[name] = block
126
- end
127
- end
128
- end
129
- end
130
- end
@@ -1,109 +0,0 @@
1
- require 'jsonapi/serializable/resource_builder'
2
-
3
- module JSONAPI
4
- module Serializable
5
- module RelationshipDSL
6
- # Declare the related resources for this relationship.
7
- # @param [String,Constant,Hash{Symbol=>String,Constant}] resource_class
8
- # @yieldreturn The related resources for this relationship.
9
- # If it is nil, an object implementing the Serializable::Resource
10
- # interface, an empty array, or an array of objects implementing the
11
- # Serializable::Resource interface, then it is used as is.
12
- # Otherwise an appropriate Serializable::Resource subclass is inferred
13
- # from the object(s)' namespace/class, the resource_class parameter if
14
- # provided, and the @_resource_inferer.
15
- #
16
- # @example
17
- # resources do
18
- # @user.posts.map { |p| PostResource.new(post: p) }
19
- # end
20
- #
21
- # @example
22
- # resources do
23
- # @post.author && UserResource.new(user: @user.author)
24
- # end
25
- #
26
- # @example
27
- # resources do
28
- # @user.posts
29
- # end
30
- # end
31
- #
32
- # @example
33
- # resources SerializablePost do
34
- # @user.posts
35
- # end
36
- #
37
- # @example
38
- # resources "SerializableUser" do
39
- # @post.author
40
- # end
41
- def resources(resource_class = nil)
42
- # NOTE(beauby): Lazify computation since it is only needed when
43
- # the corresponding relationship is included.
44
- @_resources_block = proc do
45
- _resources_for(yield, resource_class)
46
- end
47
- end
48
-
49
- # Explicitly declare linkage data.
50
- # @yieldreturn The resource linkage.
51
- def data(&block)
52
- # NOTE(beauby): Lazify computation since it is only executed when
53
- # the corresponding relationship is included (or no links and
54
- # no meta was specified).
55
- @_linkage_block = block
56
- end
57
-
58
- # @overload meta(value)
59
- # Declare the meta information for this relationship.
60
- # @param [Hash] value The meta information hash.
61
- #
62
- # @example
63
- # meta paginated: true
64
- #
65
- # @overload meta(&block)
66
- # Declare the meta information for this relationship.
67
- # @yieldreturn [Hash] The meta information hash.
68
- #
69
- # @example
70
- # meta do
71
- # { paginated: true }
72
- # end
73
- def meta(value = nil)
74
- @_meta = value || yield
75
- end
76
-
77
- # Declare a link for this relationship. The properties of the link are set
78
- # by providing a block in which the DSL methods of
79
- # +JSONAPI::Serializable::Link+ are called.
80
- # @see JSONAPI::Serialiable::Link
81
- #
82
- # @param [Symbol] name The key of the link.
83
- # @yieldreturn [Hash, String, nil] The block to compute the value, if any.
84
- #
85
- # @example
86
- # link(:self) do
87
- # "http://api.example.com/users/#{@user.id}/relationships/posts"
88
- # end
89
- #
90
- # @example
91
- # link(:related) do
92
- # href "http://api.example.com/users/#{@user.id}/posts"
93
- # meta authorization_needed: true
94
- # end
95
- def link(name, &block)
96
- @_links[name] = Link.as_jsonapi(@_exposures, &block)
97
- end
98
-
99
- private
100
-
101
- # @api private
102
- def _resources_for(objects, resource_class)
103
- resource_class ||= @_resource_inferer
104
-
105
- ResourceBuilder.build(objects, @_exposures, resource_class)
106
- end
107
- end
108
- end
109
- end
@@ -1,33 +0,0 @@
1
- module JSONAPI
2
- module Serializable
3
- module ResourceDSL
4
- def self.included(base)
5
- base.extend(ClassMethods)
6
- end
7
-
8
- module ClassMethods
9
- def attribute(attr, &block)
10
- block ||= proc { @object.public_send(attr) }
11
- super(attr, &block)
12
- end
13
-
14
- def attributes(*args)
15
- args.each do |attr|
16
- attribute(attr)
17
- end
18
- end
19
-
20
- def relationship(rel, resource_class = nil, &block)
21
- rel_block = proc do
22
- resources(resource_class) { @object.public_send(rel) }
23
- instance_eval(&block) unless block.nil?
24
- end
25
- super(rel, &rel_block)
26
- end
27
- alias has_many relationship
28
- alias has_one relationship
29
- alias belongs_to relationship
30
- end
31
- end
32
- end
33
- end