alba 0.5.0 → 0.6.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 +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +3 -1
- data/lib/alba.rb +2 -0
- data/lib/alba/resource.rb +19 -5
- data/lib/alba/serializer.rb +10 -6
- data/lib/alba/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efa21967c65ad6880f19fbb2ec2fd795ed1b71f477084056068b23984df60834
|
4
|
+
data.tar.gz: 556f95a3ad12a7fdc591f8b7e336a7bc2d6c4b99710198f9bdc4e36b1389e0d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f2bc512373e64a1ecda15c5797a5e9a1944f9fdc2a81f34f4af063d54c288fa53710bba9645fdbdec922b2642491bf0983830b7b806c3e6706099d48ed31c29
|
7
|
+
data.tar.gz: 99c355f1ad8d2c26786b79d3f7889ac00b08444f72352a14f300eb25049efbe81792242d48c0c9bf760e5bce3e93d61f408a99ac38a93a252ca55954c0880162
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
alba (0.
|
4
|
+
alba (0.6.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -16,6 +16,7 @@ GEM
|
|
16
16
|
docile (1.3.2)
|
17
17
|
json (2.3.1)
|
18
18
|
minitest (5.14.1)
|
19
|
+
oj (3.10.8)
|
19
20
|
parallel (1.19.2)
|
20
21
|
parser (2.7.1.4)
|
21
22
|
ast (~> 2.4.1)
|
@@ -59,6 +60,7 @@ DEPENDENCIES
|
|
59
60
|
alba!
|
60
61
|
coveralls
|
61
62
|
minitest (~> 5.0)
|
63
|
+
oj (~> 3.10)
|
62
64
|
rake (~> 13.0)
|
63
65
|
rubocop (>= 0.79.0)
|
64
66
|
rubocop-performance (~> 1.7.1)
|
data/lib/alba.rb
CHANGED
@@ -10,6 +10,7 @@ module Alba
|
|
10
10
|
|
11
11
|
class << self
|
12
12
|
attr_reader :backend
|
13
|
+
attr_accessor :default_serializer
|
13
14
|
|
14
15
|
def backend=(backend)
|
15
16
|
@backend = backend&.to_sym
|
@@ -20,6 +21,7 @@ module Alba
|
|
20
21
|
|
21
22
|
resource_class.class_eval(&block)
|
22
23
|
resource = resource_class.new(object)
|
24
|
+
with ||= @default_serializer
|
23
25
|
resource.serialize(with: with)
|
24
26
|
end
|
25
27
|
|
data/lib/alba/resource.rb
CHANGED
@@ -7,12 +7,17 @@ require 'alba/serializers/default_serializer'
|
|
7
7
|
module Alba
|
8
8
|
# This module represents what should be serialized
|
9
9
|
module Resource
|
10
|
-
DSLS = [:_attributes, :_serializer].freeze
|
10
|
+
DSLS = [:_attributes, :_serializer, :_key].freeze
|
11
11
|
def self.included(base)
|
12
12
|
base.class_eval do
|
13
13
|
# Initialize
|
14
14
|
DSLS.each do |name|
|
15
|
-
initial = name
|
15
|
+
initial = case name
|
16
|
+
when :_attributes
|
17
|
+
{}
|
18
|
+
when :_serializer, :_name
|
19
|
+
nil
|
20
|
+
end
|
16
21
|
instance_variable_set("@#{name}", initial) unless instance_variable_defined?("@#{name}")
|
17
22
|
end
|
18
23
|
end
|
@@ -38,16 +43,21 @@ module Alba
|
|
38
43
|
else
|
39
44
|
raise ArgumentError, 'Unexpected type for with, possible types are Class or Proc'
|
40
45
|
end
|
41
|
-
serializer.new(
|
46
|
+
serializer.new(self).serialize
|
42
47
|
end
|
43
48
|
|
44
|
-
def serializable_hash
|
45
|
-
@_attributes.transform_values do |attribute|
|
49
|
+
def serializable_hash(with_key: true)
|
50
|
+
serializable_hash = @_attributes.transform_values do |attribute|
|
46
51
|
attribute.to_hash(@_resource)
|
47
52
|
end
|
53
|
+
with_key && @_key ? {@_key => serializable_hash} : serializable_hash
|
48
54
|
end
|
49
55
|
alias to_hash serializable_hash
|
50
56
|
|
57
|
+
def key
|
58
|
+
@_key || self.class.name.delete_suffix('Resource').downcase.gsub(/:{2}/, '_').to_sym
|
59
|
+
end
|
60
|
+
|
51
61
|
private
|
52
62
|
|
53
63
|
def inline_extended_serializer(with)
|
@@ -86,6 +96,10 @@ module Alba
|
|
86
96
|
def serializer(name)
|
87
97
|
@_serializer = name <= Alba::Serializer ? name : nil
|
88
98
|
end
|
99
|
+
|
100
|
+
def key(key)
|
101
|
+
@_key = key.to_sym
|
102
|
+
end
|
89
103
|
end
|
90
104
|
end
|
91
105
|
end
|
data/lib/alba/serializer.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module Alba
|
2
2
|
# This module represents how a resource should be serialized.
|
3
|
-
#
|
4
3
|
module Serializer
|
5
4
|
def self.included(base)
|
6
5
|
base.include InstanceMethods
|
@@ -10,22 +9,27 @@ module Alba
|
|
10
9
|
# Instance methods
|
11
10
|
module InstanceMethods
|
12
11
|
def initialize(resource)
|
13
|
-
@_resource = resource
|
14
12
|
@_opts = self.class._opts || {}
|
15
|
-
key = @_opts[:key]
|
16
|
-
|
13
|
+
key = case @_opts[:key]
|
14
|
+
when true
|
15
|
+
resource.key
|
16
|
+
else
|
17
|
+
@_opts[:key]
|
18
|
+
end
|
19
|
+
@hash = resource.serializable_hash(with_key: false)
|
20
|
+
@hash = {key.to_sym => @hash} if key
|
17
21
|
end
|
18
22
|
|
19
23
|
def serialize
|
20
24
|
fallback = lambda do
|
21
25
|
require 'json'
|
22
|
-
JSON.dump(@
|
26
|
+
JSON.dump(@hash)
|
23
27
|
end
|
24
28
|
case Alba.backend
|
25
29
|
when :oj
|
26
30
|
begin
|
27
31
|
require 'oj'
|
28
|
-
-> { Oj.dump(@
|
32
|
+
-> { Oj.dump(@hash, mode: :strict) }
|
29
33
|
rescue LoadError
|
30
34
|
fallback
|
31
35
|
end
|
data/lib/alba/version.rb
CHANGED
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: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OKURA Masafumi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Fast and flexible JSON serializer
|
14
14
|
email:
|