alba 3.0.0 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d76423c125b10f3a012066e6df3924c1dde50c1842e78be87179d99da8707a9e
4
- data.tar.gz: 23bba5622260ac6926a669b1d4d4aa44e6a4189838b2c77b23f6e846c56ea14f
3
+ metadata.gz: 0f7b454e9de7d518fe1287a46d1b14a0c8cb8d13e3b10fcce3eee8a9e3df05c9
4
+ data.tar.gz: ab670cee9399503a4480f74b4533a40eb4fd67048a0b9c93b9f706712ecc1a8f
5
5
  SHA512:
6
- metadata.gz: d57037b9ea77550953a6e8dc4f6896e314ce1e63e36925ec960ebca1e2cce043f61a289091f09c74ef308de50d541e2b2624ce977dcf613165175328c546b32c
7
- data.tar.gz: 21ab608e1844f3a575985927f81ed892f1cb50c8bf9008302c4572d1acc9c9bebbc66c1afce173af29a22b03f67a6e0b117197d037ae64061401e5a19d1779e6
6
+ metadata.gz: 758481e888e55ae7a1ac87d61060cdbe76f3effea46e59b0906a125c4d804ef54f337aee014cba3df35fbd819601ad66c84eaafb820fd1a1144842a48969125a
7
+ data.tar.gz: 5960c6958060c3169152c4634aaecd34af1a0920b7cb6461c9652e690e6fed661275efea1a6988513be0e7142eec6e13fe3a6f61658af68a70fc25ad04eaf430
@@ -1,4 +1,4 @@
1
- name: Ruby
1
+ name: CI
2
2
 
3
3
  on: [push,pull_request]
4
4
 
@@ -8,7 +8,7 @@ jobs:
8
8
  fail-fast: false
9
9
  matrix:
10
10
  os: [ubuntu-latest, windows-latest, macos-latest]
11
- ruby: ['3.0', 3.1, 3.2, head, jruby, truffleruby]
11
+ ruby: ['3.0', 3.1, 3.2, head, truffleruby] # TODO: Add jruby after the error is resolved
12
12
  gemfile: [all, without_active_support, without_oj]
13
13
  exclude:
14
14
  - os: windows-latest
data/CHANGELOG.md CHANGED
@@ -6,8 +6,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.0.2] 2023-12-05
10
+
11
+ ### Fixed
12
+
13
+ - Fixed warning when `location` option is given to `render json:` in Rails [#348](https://github.com/okuramasafumi/alba/pull/348)
14
+
15
+ ## [3.0.1] 2023-10-13
16
+
17
+ ### Fixed
18
+
19
+ - Fixed a bug where methods such as `test` or `params` cannot be used as attribute name [#344](https://github.com/okuramasafumi/alba/pull/344)
20
+ - Remove redundant code
21
+
9
22
  ## [3.0.0] 2023-10-11
10
23
 
24
+ ### IMPORTANT
25
+
26
+ **This release contains an important bug fix that can cause data corruption.**
27
+ **If you're using Ruby 3, it's highly recommended to upgrade to [v3.0.0](https://rubygems.org/gems/alba/versions/3.0.0)**
28
+ **If you're using Ruby 2, please upgrade to [v2.4.2](https://rubygems.org/gems/alba/versions/2.4.2) that contains bug fix only as soon as possible.**
29
+
11
30
  ### Added
12
31
 
13
32
  - Custom type [#333](https://github.com/okuramasafumi/alba/pull/333)
data/CODE_OF_CONDUCT.md CHANGED
@@ -1,74 +1,132 @@
1
+
1
2
  # Contributor Covenant Code of Conduct
2
3
 
3
4
  ## Our Pledge
4
5
 
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
6
+ We as members, contributors, and leaders pledge to make participation in our
7
+ community a harassment-free experience for everyone, regardless of age, body
8
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
9
+ identity and expression, level of experience, education, socio-economic status,
10
+ nationality, personal appearance, race, caste, color, religion, or sexual
11
+ identity and orientation.
12
+
13
+ We pledge to act and interact in ways that contribute to an open, welcoming,
14
+ diverse, inclusive, and healthy community.
11
15
 
12
16
  ## Our Standards
13
17
 
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
18
+ Examples of behavior that contributes to a positive environment for our
19
+ community include:
16
20
 
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
21
+ * Demonstrating empathy and kindness toward other people
22
+ * Being respectful of differing opinions, viewpoints, and experiences
23
+ * Giving and gracefully accepting constructive feedback
24
+ * Accepting responsibility and apologizing to those affected by our mistakes,
25
+ and learning from the experience
26
+ * Focusing on what is best not just for us as individuals, but for the overall
27
+ community
22
28
 
23
- Examples of unacceptable behavior by participants include:
29
+ Examples of unacceptable behavior include:
24
30
 
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
31
+ * The use of sexualized language or imagery, and sexual attention or advances of
32
+ any kind
33
+ * Trolling, insulting or derogatory comments, and personal or political attacks
28
34
  * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
35
+ * Publishing others' private information, such as a physical or email address,
36
+ without their explicit permission
31
37
  * Other conduct which could reasonably be considered inappropriate in a
32
38
  professional setting
33
39
 
34
- ## Our Responsibilities
40
+ ## Enforcement Responsibilities
35
41
 
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
42
+ Community leaders are responsible for clarifying and enforcing our standards of
43
+ acceptable behavior and will take appropriate and fair corrective action in
44
+ response to any behavior that they deem inappropriate, threatening, offensive,
45
+ or harmful.
39
46
 
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
47
+ Community leaders have the right and responsibility to remove, edit, or reject
48
+ comments, commits, code, wiki edits, issues, and other contributions that are
49
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
50
+ decisions when appropriate.
45
51
 
46
52
  ## Scope
47
53
 
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
+ This Code of Conduct applies within all community spaces, and also applies when
55
+ an individual is officially representing the community in public spaces.
56
+ Examples of representing our community include using an official e-mail address,
57
+ posting via an official social media account, or acting as an appointed
58
+ representative at an online or offline event.
54
59
 
55
60
  ## Enforcement
56
61
 
57
62
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at masafumi.o1988@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
+ reported to the community leaders responsible for enforcement at masafumi.o1988@gmail.com .
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
63
84
 
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
67
114
 
68
115
  ## Attribution
69
116
 
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [https://contributor-covenant.org/version/1/4][version]
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
72
127
 
73
- [homepage]: https://contributor-covenant.org
74
- [version]: https://contributor-covenant.org/version/1/4/
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/README.md CHANGED
@@ -6,11 +6,22 @@
6
6
  [![Maintainability](https://api.codeclimate.com/v1/badges/fdab4cc0de0b9addcfe8/maintainability)](https://codeclimate.com/github/okuramasafumi/alba/maintainability)
7
7
  ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/okuramasafumi/alba)
8
8
  ![GitHub](https://img.shields.io/github/license/okuramasafumi/alba)
9
+ [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](code_of_conduct.md)
9
10
 
10
11
  # Alba
11
12
 
12
13
  Alba is a JSON serializer for Ruby, JRuby, and TruffleRuby.
13
14
 
15
+ ## IMPORTANT NOTICE
16
+
17
+ Both version `3.0.0` and `2.4.2` contain important bug fix.
18
+ ~~However, version `3.0.0` has some bugs (see https://github.com/okuramasafumi/alba/issues/342).
19
+ Until they get fixed, it's highly recommended to upgrade to version `2.4.2`.
20
+ Dependabot and similar tools might create an automated Pull Request to upgrade to `3.0.0`, so it might be required to upgrade to `2.4.2` manually.~~
21
+ Version `3.0.1` has been released so Ruby 3 users should upgrade to `3.0.1`.
22
+ For Ruby 2 users, it's highly recommended to upgrade to `2.4.2`.
23
+ Sorry for the inconvenience.
24
+
14
25
  ## TL;DR
15
26
 
16
27
  Alba allows you to do something like below.
@@ -281,72 +292,77 @@ class UserResource
281
292
  end
282
293
  ```
283
294
 
284
- #### Prefer methods on resource
295
+ #### Methods conflict
285
296
 
286
- By default, Alba prefers methods on the object to methods on the resource. This means if you have a following situation:
297
+ Consider following code:
287
298
 
288
299
  ```ruby
289
- class User
290
- attr_accessor :id, :name, :email
291
-
292
- def initialize(id, name, email)
293
- @id = id
294
- @name = name
295
- @email = email
296
- end
297
-
298
- def name_with_email
299
- 'dummy!'
300
+ class Foo
301
+ def bar
302
+ 'This is Foo'
300
303
  end
301
304
  end
302
305
 
303
- class UserResource
306
+ class FooResource
304
307
  include Alba::Resource
305
308
 
306
- root_key :user, :users # Later is for plural
307
-
308
- attributes :id, :name, :name_with_email
309
+ attributes :bar
309
310
 
310
- # Same method exists in `User` class!
311
- # This is not called
312
- def name_with_email(user)
313
- "#{user.name}: #{user.email}"
311
+ def bar
312
+ 'This is FooResource'
314
313
  end
315
314
  end
316
315
 
317
- user = User.new(1, 'Masafumi OKURA', 'masafumi@example.com')
318
- UserResource.new(user).serialize
319
- # => '{"user":{"id":1,"name":"Masafumi OKURA","name_with_email":"dummy!"}}'
316
+ FooResource.new(Foo.new).serialize
320
317
  ```
321
318
 
322
- You can see that `name_with_email` is now `dummy!` from `User#name_with_email`. You cna change this behavior by using `prefer_resource_method!` DSL in a resource class:
319
+ By default, Alba create the JSON as `'{"bar":"This is FooResource"}'`. This means Alba calls a method on a Resource class and doesn't call a method on a target object. This rule is applied to methods that are explicitly defined on Resource class, so methods that Resource class inherits from `Object` class such as `format` are ignored.
323
320
 
324
321
  ```ruby
325
- # With the same `User` class
322
+ class Foo
323
+ def format
324
+ 'This is Foo'
325
+ end
326
+ end
326
327
 
327
- class UserResource
328
+ class FooResource
328
329
  include Alba::Resource
329
330
 
330
- prefer_resource_method! # This line is important
331
+ attributes :bar
331
332
 
332
- root_key :user, :users # Later is for plural
333
+ # Here, `format` method is available
334
+ end
333
335
 
334
- attributes :id, :name, :name_with_email
336
+ FooResource.new(Foo.new).serialize
337
+ # => '{"bar":"This is Foo"}'
338
+ ```
335
339
 
336
- # Same method exists in `User` class!
337
- # But now this is called!
338
- def name_with_email(user)
339
- "#{user.name}: #{user.email}"
340
+ If you'd like Alba to call methods on a target object, use `prefer_object_method!` like below.
341
+
342
+ ```ruby
343
+ class Foo
344
+ def bar
345
+ 'This is Foo'
340
346
  end
341
347
  end
342
348
 
343
- user = User.new(1, 'Masafumi OKURA', 'masafumi@example.com')
344
- UserResource.new(user).serialize
345
- # => '{"user":{"id":1,"name":"Masafumi OKURA","name_with_email":"Masafumi OKURA: masafumi@example.com"}}'
346
- ```
349
+ class FooResource
350
+ include Alba::Resource
351
+
352
+ prefer_object_method! # <- important
347
353
 
348
- The next major version of Alba will change this default behavior to prefer resource methods. In case you want to preserve current behavior, there's `prefer_object_method!` DSL, which does that.
354
+ attributes :bar
355
+
356
+ # This is not called
357
+ def bar
358
+ 'This is FooResource'
359
+ end
360
+ end
349
361
 
362
+ FooResource.new(Foo.new).serialize
363
+ # => '{"bar":"This is Foo"}'
364
+ ```
365
+
350
366
  #### Params
351
367
 
352
368
  You can pass a Hash to the resource for internal use. It can be used as "flags" to control attribute content.
@@ -13,7 +13,7 @@ This guide is aimed at helping Jbuilder users transition to Alba, and it consist
13
13
 
14
14
  ## Example class
15
15
 
16
- This example will also be replaced by ActiveReord.
16
+ This example will also be replaced by ActiveRecord.
17
17
 
18
18
  ```rb
19
19
  class User
@@ -57,7 +57,6 @@ module Alba
57
57
  when Class
58
58
  resource
59
59
  when Symbol, String
60
- Object.const_get(resource)
61
60
  self.class.const_cache.fetch(resource) do
62
61
  self.class.const_cache[resource] = Object.const_get(resource)
63
62
  end
@@ -51,6 +51,8 @@ module Alba
51
51
 
52
52
  # OpenStruct is used as a simple solution for converting Hash or Array of Hash into an object
53
53
  # Using OpenStruct is not good in general, but in this case there's no other solution
54
+ # rubocop:disable Style/OpenStructUse
55
+ # rubocop:disable Performance/OpenStruct
54
56
  def objectize(fetched_attribute)
55
57
  return fetched_attribute unless @body.is_a?(Alba::Association)
56
58
 
@@ -62,5 +64,7 @@ module Alba
62
64
  OpenStruct.new(fetched_attribute)
63
65
  end
64
66
  end
67
+ # rubocop:enable Style/OpenStructUse
68
+ # rubocop:enable Performance/OpenStruct
65
69
  end
66
70
  end
data/lib/alba/resource.rb CHANGED
@@ -12,7 +12,7 @@ module Alba
12
12
  module Resource
13
13
  # @!parse include InstanceMethods
14
14
  # @!parse extend ClassMethods
15
- INTERNAL_VARIABLES = {_attributes: {}, _key: nil, _key_for_collection: nil, _meta: nil, _transform_type: :none, _transforming_root_key: false, _key_transformation_cascade: true, _on_error: nil, _on_nil: nil, _layout: nil, _collection_key: nil, _helper: nil}.freeze # rubocop:disable Layout/LineLength
15
+ INTERNAL_VARIABLES = {_attributes: {}, _key: nil, _key_for_collection: nil, _meta: nil, _transform_type: :none, _transforming_root_key: false, _key_transformation_cascade: true, _on_error: nil, _on_nil: nil, _layout: nil, _collection_key: nil, _helper: nil, _resource_methods: []}.freeze # rubocop:disable Layout/LineLength
16
16
  private_constant :INTERNAL_VARIABLES
17
17
 
18
18
  WITHIN_DEFAULT = Object.new.freeze
@@ -96,12 +96,11 @@ module Alba
96
96
  private
97
97
 
98
98
  def _to_json(root_key, meta, options)
99
- options.reject! { |k, _| %i[layout prefixes template status].include?(k) } # Rails specific guard
100
- names = options.filter_map { |k, v| k unless v.nil? }
101
- unless names.empty?
102
- names.sort!
103
- names.map! { |s| "\"#{s}\"" }
104
- message = "You passed #{names.join(', ')} options but ignored. Please refer to the document: https://github.com/okuramasafumi/alba/blob/main/docs/rails.md"
99
+ confusing_options = options.keys.select { |k| k.to_sym == :only || k.to_sym == :except }
100
+ unless confusing_options.empty?
101
+ confusing_options.sort!
102
+ confusing_options.map! { |s| "\"#{s}\"" }
103
+ message = "You passed #{confusing_options.join(' and ')} options but ignored. Please refer to the document: https://github.com/okuramasafumi/alba/blob/main/docs/rails.md"
105
104
  Kernel.warn(message)
106
105
  end
107
106
  serialize(root_key: root_key, meta: meta)
@@ -265,9 +264,15 @@ module Alba
265
264
  end
266
265
 
267
266
  def _fetch_attribute_from_resource_first(obj, attribute)
268
- __send__(attribute, obj)
269
- rescue NoMethodError
270
- obj.__send__(attribute)
267
+ if @_resource_methods.include?(attribute)
268
+ begin
269
+ __send__(attribute, obj)
270
+ rescue NoMethodError
271
+ obj.__send__(attribute)
272
+ end
273
+ else
274
+ obj.__send__(attribute)
275
+ end
271
276
  end
272
277
 
273
278
  def nil_handler
@@ -302,6 +307,12 @@ module Alba
302
307
  module ClassMethods
303
308
  attr_reader(*INTERNAL_VARIABLES.keys)
304
309
 
310
+ # This `method_added` is used for defining "resource methods"
311
+ def method_added(method_name)
312
+ _resource_methods << method_name.to_sym unless method_name.to_sym == :_setup
313
+ super
314
+ end
315
+
305
316
  # @private
306
317
  def inherited(subclass)
307
318
  super
data/lib/alba/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Alba
2
- VERSION = '3.0.0'.freeze
2
+ VERSION = '3.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alba
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - OKURA Masafumi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-11 00:00:00.000000000 Z
11
+ date: 2023-12-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Alba is the fastest JSON serializer for Ruby. It focuses on performance,
14
14
  flexibility and usability.
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  - !ruby/object:Gem::Version
95
95
  version: '0'
96
96
  requirements: []
97
- rubygems_version: 3.4.14
97
+ rubygems_version: 3.4.22
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Alba is the fastest JSON serializer for Ruby.