alba 2.0.0 → 2.0.1

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
  SHA256:
3
- metadata.gz: e445a59555f43f7c601699a3a2ceee967d33a37ea77f2b15a0e806dec0dfc5b8
4
- data.tar.gz: 3506f93a8fd9c00c5d8d85566f41032f3bc23356c753d202ce48e9ba984f1acc
3
+ metadata.gz: 9018588ddacac0cf7db8dac1d23c8165665c7a93976b581d25ab51357ead7834
4
+ data.tar.gz: 6bde71c65d8fb6965d46cea2769cd9a97d4f76407136e47de4392cdfa1a58333
5
5
  SHA512:
6
- metadata.gz: 46251245a639ad19d4c75d9b3afee2f9afdd19380ae70c1a1113ccc96966402bfff6434ac2fef9cdef4785873438d6ba828ee64712a8757dc497798fd890337a
7
- data.tar.gz: ce096713947ae541071331d67212dc1fb35957dd7849b8d87b98ce6a8eda05b867406ec4ffad2d64eb76fcfe2a5d8be16486f2f4cd2c88e82c259b0e58b37887
6
+ metadata.gz: a7d907446dad8ff8a19814cbf27ba74bfc84a0f00fae8366f561efd5de03e1ac16d66f3b31641e8ec83b5081e6e5fc61074c5a5e47509343fade989ab65e41da
7
+ data.tar.gz: d81e0055f6899698d70ab1522e71adc8ce9e22e3a8855ad2a3db8b54e551d2ea459a77bc92242bc5c7f8121d150c322f3b99f6d286fd7118f5df2f94d0dc3fdc
data/CHANGELOG.md CHANGED
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.0.1] 2022-11-02
10
+
11
+ ### Fix
12
+
13
+ - the bug including key not in `within` [#262](https://github.com/okuramasafumi/alba/pull/262)
14
+ - key transformation now cascades multiple levels [#263](https://github.com/okuramasafumi/alba/pull/263)
15
+
9
16
  ## [2.0.0] 2022-10-21
10
17
 
11
18
  ### Breaking changes
@@ -13,16 +20,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
13
20
  - All Hash-related methods now return String key instead of Symbol key
14
21
  This affects all users, but you can use `deep_symbolize_keys` in Rails environment if you prefer Symbol keys
15
22
  Some DSLs that take key argument such as `on_nil` and `on_error`, are also affected
23
+ - Remove deprecated methods: `Resource#to_hash`, `Resource.ignoring`, `Alba.on_nil`, `Alba.on_error`, `Alba.enable_root_key_transformation!` and `Alba.disable_root_key_transformation!`
24
+ - If using `transform_keys`, the default inflector is no longer set by default [d02245c8](https://github.com/okuramasafumi/alba/commit/d02245c87e9df303cb20e354a81e5457ea460bdd#diff-ecd8c835d2390b8cb89e7ff75e599f0c15cdbe18c30981d6090f4a515566686f)
25
+ To retain this functionality in Rails, add an initializer with the following:
26
+ `Alba.enable_inference!(with: :active_support)`
16
27
 
17
28
  ### New features
18
29
 
19
- - Passing an initial object to proc function in associations
20
- - Allow association resource to be Proc
21
- - `collection_key` to serialize collection into a Hash
22
- - params is now overridable
23
- - Key transformation now cascades
24
- - nested attribute
25
- - Implement `as_json`
30
+ - Passing an initial object to proc function in associations [#209](https://github.com/okuramasafumi/alba/pull/209)
31
+ - Allow association resource to be Proc [#213](https://github.com/okuramasafumi/alba/pull/213)
32
+ - `collection_key` to serialize collection into a Hash [#119](https://github.com/okuramasafumi/alba/pull/119)
33
+ - params is now overridable [#227](https://github.com/okuramasafumi/alba/pull/227)
34
+ - Key transformation now cascades [#232](https://github.com/okuramasafumi/alba/pull/232)
35
+ - nested attribute [#237](https://github.com/okuramasafumi/alba/pull/237)
36
+ - Implement `as_json` [#249](https://github.com/okuramasafumi/alba/pull/249)
26
37
 
27
38
  ### Bugfix
28
39
 
data/Gemfile CHANGED
@@ -10,7 +10,7 @@ gem 'inch', require: false # For inline documents
10
10
  gem 'minitest', '~> 5.14' # For test
11
11
  gem 'rake', '~> 13.0' # For test and automation
12
12
  gem 'rubocop', '>= 0.79.0', require: false # For lint
13
- gem 'rubocop-minitest', '~> 0.22.0', require: false # For lint
13
+ gem 'rubocop-minitest', '~> 0.23.0', require: false # For lint
14
14
  gem 'rubocop-performance', '~> 1.15.0', require: false # For lint
15
15
  gem 'rubocop-rake', '>= 0.5.1', require: false # For lint
16
16
  gem 'rubocop-sensible', '~> 0.3.0', require: false # For lint
@@ -20,10 +20,9 @@ module Alba
20
20
  @condition = condition
21
21
  @resource = resource
22
22
  @params = params
23
- @key_transformation = key_transformation
24
23
  return if @resource
25
24
 
26
- assign_resource(nesting, block)
25
+ assign_resource(nesting, key_transformation, block)
27
26
  end
28
27
 
29
28
  # Recursively converts an object into a Hash
@@ -58,9 +57,12 @@ module Alba
58
57
  end
59
58
  end
60
59
 
61
- def assign_resource(nesting, block)
60
+ def assign_resource(nesting, key_transformation, block)
62
61
  @resource = if block
63
- Alba.resource_class(&block)
62
+ klass = Alba.resource_class
63
+ klass.transform_keys(key_transformation)
64
+ klass.class_eval(&block)
65
+ klass
64
66
  elsif Alba.inferring
65
67
  Alba.infer_resource_class(@name, nesting: nesting)
66
68
  else
@@ -76,7 +78,6 @@ module Alba
76
78
 
77
79
  def to_h_with_constantize_resource(within, params)
78
80
  @resource = constantize(@resource)
79
- @resource.transform_keys(@key_transformation)
80
81
  @resource.new(object, params: params, within: within).to_h
81
82
  end
82
83
  end
@@ -1,9 +1,9 @@
1
+ require_relative 'association'
2
+ require_relative 'constants'
3
+
1
4
  module Alba
2
5
  # Represents attribute with `if` option
3
6
  class ConditionalAttribute
4
- CONDITION_UNMET = Object.new.freeze
5
- public_constant :CONDITION_UNMET # It's public for use in `Alba::Resource`
6
-
7
7
  # @param body [Symbol, Proc, Alba::Association, Alba::TypedAttribute] real attribute wrapped with condition
8
8
  # @param condition [Symbol, Proc] condition to check
9
9
  def initialize(body:, condition:)
@@ -15,14 +15,14 @@ module Alba
15
15
  #
16
16
  # @param resource [Alba::Resource]
17
17
  # @param object [Object] needed for collection, each object from collection
18
- # @return [ConditionalAttribute::CONDITION_UNMET, Object] CONDITION_UNMET if condition is unmet, fetched attribute otherwise
18
+ # @return [Alba::REMOVE_KEY, Object] REMOVE_KEY if condition is unmet, fetched attribute otherwise
19
19
  def with_passing_condition(resource:, object: nil)
20
- return CONDITION_UNMET unless condition_passes?(resource, object)
20
+ return Alba::REMOVE_KEY unless condition_passes?(resource, object)
21
21
 
22
22
  fetched_attribute = yield(@body)
23
23
  return fetched_attribute if fetched_attribute.nil? || !with_two_arity_proc_condition
24
24
 
25
- return CONDITION_UNMET unless resource.instance_exec(object, attribute_from_association_body_or(fetched_attribute), &@condition)
25
+ return Alba::REMOVE_KEY unless resource.instance_exec(object, attribute_from_association_body_or(fetched_attribute), &@condition)
26
26
 
27
27
  fetched_attribute
28
28
  end
@@ -0,0 +1,5 @@
1
+ # This file includes public constants to prevent circular dependencies.
2
+ module Alba
3
+ REMOVE_KEY = Object.new.freeze # A constant to remove key from serialized JSON
4
+ public_constant :REMOVE_KEY
5
+ end
@@ -10,8 +10,9 @@ module Alba
10
10
 
11
11
  # @return [Hash]
12
12
  def value(object)
13
- resource_class = Alba.resource_class(&@block)
13
+ resource_class = Alba.resource_class
14
14
  resource_class.transform_keys(@key_transformation)
15
+ resource_class.class_eval(&@block)
15
16
  resource_class.new(object).serializable_hash
16
17
  end
17
18
  end
data/lib/alba/resource.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative 'association'
2
2
  require_relative 'conditional_attribute'
3
+ require_relative 'constants'
3
4
  require_relative 'typed_attribute'
4
5
  require_relative 'nested_attribute'
5
6
  require_relative 'deprecation'
@@ -215,7 +216,7 @@ module Alba
215
216
  def set_key_and_attribute_body_from(object, key, attribute, hash)
216
217
  key = transform_key(key)
217
218
  value = fetch_attribute(object, key, attribute)
218
- hash[key] = value unless value == ConditionalAttribute::CONDITION_UNMET
219
+ hash[key] = value unless value == Alba::REMOVE_KEY
219
220
  end
220
221
 
221
222
  def handle_error(error, object, key, attribute, hash)
@@ -271,7 +272,7 @@ module Alba
271
272
 
272
273
  def yield_if_within(association_name)
273
274
  within = check_within(association_name)
274
- yield(within) if within
275
+ within ? yield(within) : Alba::REMOVE_KEY
275
276
  end
276
277
 
277
278
  def check_within(association_name)
data/lib/alba/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Alba
2
- VERSION = '2.0.0'.freeze
2
+ VERSION = '2.0.1'.freeze
3
3
  end
data/lib/alba.rb CHANGED
@@ -69,7 +69,7 @@ module Alba
69
69
  def resource_class(&block)
70
70
  klass = Class.new
71
71
  klass.include(Alba::Resource)
72
- klass.class_eval(&block)
72
+ klass.class_eval(&block) if block
73
73
  klass
74
74
  end
75
75
 
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: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - OKURA Masafumi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-21 00:00:00.000000000 Z
11
+ date: 2022-11-02 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.
@@ -52,6 +52,7 @@ files:
52
52
  - lib/alba.rb
53
53
  - lib/alba/association.rb
54
54
  - lib/alba/conditional_attribute.rb
55
+ - lib/alba/constants.rb
55
56
  - lib/alba/default_inflector.rb
56
57
  - lib/alba/deprecation.rb
57
58
  - lib/alba/errors.rb