alba 0.1.0 → 0.2.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/.rubocop.yml +6 -0
- data/Gemfile +2 -1
- data/Gemfile.lock +40 -15
- data/README.md +4 -0
- data/lib/alba/attribute.rb +2 -2
- data/lib/alba/many.rb +26 -0
- data/lib/alba/one.rb +26 -0
- data/lib/alba/resource.rb +58 -24
- data/lib/alba/version.rb +1 -1
- data/sider.yml +60 -0
- metadata +9 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9dab0921e07fb607cfceb802a42fdb5f8cc20eb745522c46dc7b451b8a0cd14e
|
4
|
+
data.tar.gz: 6c6fcc0035ef543ee6d60bffa18ca83379d090e800950411d7316c21031784ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c97545976f850ba0191dcabf7f34b8daeebf54d2b82c9a37305f569cc7cadaa1b9f0f92dc42f81193e9fd56eae2e862d650b023c4bcd105739c18c0baaf35e13
|
7
|
+
data.tar.gz: 7423763628b13e5643193347379eefbcf2cc2d78266cafd147d8c86d3f755feca93dd3f2c9bca92e8ec008b1ec7d00c9ed7bf1590a2f26e83c99fa529a987de1
|
data/.rubocop.yml
CHANGED
@@ -10,6 +10,7 @@ AllCops:
|
|
10
10
|
Exclude:
|
11
11
|
- 'Rakefile'
|
12
12
|
- 'alba.gemspec'
|
13
|
+
NewCops: enable
|
13
14
|
|
14
15
|
Layout/SpaceInsideHashLiteralBraces:
|
15
16
|
EnforcedStyle: no_space
|
@@ -17,5 +18,10 @@ Layout/SpaceInsideHashLiteralBraces:
|
|
17
18
|
Metrics/MethodLength:
|
18
19
|
Max: 20
|
19
20
|
|
21
|
+
Naming/PredicateName:
|
22
|
+
AllowedMethods:
|
23
|
+
- 'has_one'
|
24
|
+
- 'has_many'
|
25
|
+
|
20
26
|
Style/FrozenStringLiteralComment:
|
21
27
|
Enabled: false
|
data/Gemfile
CHANGED
@@ -3,8 +3,9 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in alba.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
+
gem 'coveralls', require: false
|
6
7
|
gem 'minitest', '~> 5.0'
|
7
8
|
gem 'rake', '~> 13.0'
|
8
9
|
gem 'rubocop', '>= 0.79.0', require: false
|
9
|
-
gem 'rubocop-performance', '~> 1.
|
10
|
+
gem 'rubocop-performance', '~> 1.7.1', require: false
|
10
11
|
gem 'rubocop-sensible', '~> 0.3.0', require: false
|
data/Gemfile.lock
CHANGED
@@ -1,42 +1,67 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
alba (0.
|
4
|
+
alba (0.2.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
ast (2.4.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
ast (2.4.1)
|
10
|
+
coveralls (0.8.23)
|
11
|
+
json (>= 1.8, < 3)
|
12
|
+
simplecov (~> 0.16.1)
|
13
|
+
term-ansicolor (~> 1.3)
|
14
|
+
thor (>= 0.19.4, < 2.0)
|
15
|
+
tins (~> 1.6)
|
16
|
+
docile (1.3.2)
|
17
|
+
json (2.3.1)
|
18
|
+
minitest (5.14.1)
|
19
|
+
parallel (1.19.2)
|
20
|
+
parser (2.7.1.4)
|
21
|
+
ast (~> 2.4.1)
|
15
22
|
rainbow (3.0.0)
|
16
23
|
rake (13.0.1)
|
17
|
-
|
18
|
-
|
24
|
+
regexp_parser (1.7.1)
|
25
|
+
rexml (3.2.4)
|
26
|
+
rubocop (0.88.0)
|
19
27
|
parallel (~> 1.10)
|
20
|
-
parser (>= 2.7.
|
28
|
+
parser (>= 2.7.1.1)
|
21
29
|
rainbow (>= 2.2.2, < 4.0)
|
30
|
+
regexp_parser (>= 1.7)
|
31
|
+
rexml
|
32
|
+
rubocop-ast (>= 0.1.0, < 1.0)
|
22
33
|
ruby-progressbar (~> 1.7)
|
23
|
-
unicode-display_width (>= 1.4.0, <
|
24
|
-
rubocop-
|
25
|
-
|
34
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
35
|
+
rubocop-ast (0.2.0)
|
36
|
+
parser (>= 2.7.0.1)
|
37
|
+
rubocop-performance (1.7.1)
|
38
|
+
rubocop (>= 0.82.0)
|
26
39
|
rubocop-sensible (0.3.0)
|
27
40
|
rubocop (>= 0.60.0)
|
28
41
|
ruby-progressbar (1.10.1)
|
29
|
-
|
42
|
+
simplecov (0.16.1)
|
43
|
+
docile (~> 1.1)
|
44
|
+
json (>= 1.8, < 3)
|
45
|
+
simplecov-html (~> 0.10.0)
|
46
|
+
simplecov-html (0.10.2)
|
47
|
+
sync (0.5.0)
|
48
|
+
term-ansicolor (1.7.1)
|
49
|
+
tins (~> 1.0)
|
50
|
+
thor (1.0.1)
|
51
|
+
tins (1.25.0)
|
52
|
+
sync
|
53
|
+
unicode-display_width (1.7.0)
|
30
54
|
|
31
55
|
PLATFORMS
|
32
56
|
ruby
|
33
57
|
|
34
58
|
DEPENDENCIES
|
35
59
|
alba!
|
60
|
+
coveralls
|
36
61
|
minitest (~> 5.0)
|
37
62
|
rake (~> 13.0)
|
38
63
|
rubocop (>= 0.79.0)
|
39
|
-
rubocop-performance (~> 1.
|
64
|
+
rubocop-performance (~> 1.7.1)
|
40
65
|
rubocop-sensible (~> 0.3.0)
|
41
66
|
|
42
67
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[](https://travis-ci.com/okuramasafumi/alba)
|
2
|
+
[](https://coveralls.io/github/okuramasafumi/alba?branch=master)
|
3
|
+
[](https://codeclimate.com/github/okuramasafumi/alba/maintainability)
|
4
|
+
|
1
5
|
# Alba
|
2
6
|
|
3
7
|
`Alba` is a fast and flexible JSON serializer.
|
data/lib/alba/attribute.rb
CHANGED
@@ -7,12 +7,12 @@ module Alba
|
|
7
7
|
@method = method
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def to_hash(target)
|
11
11
|
case @method
|
12
12
|
when Symbol, String
|
13
13
|
target.public_send(@method)
|
14
14
|
when Proc
|
15
|
-
@method.call(target)
|
15
|
+
@method.arity.zero? ? target.instance_exec(&@method) : @method.call(target)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
data/lib/alba/many.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Alba
|
2
|
+
# Representing many association
|
3
|
+
class Many
|
4
|
+
def initialize(name:, resource: nil, &block)
|
5
|
+
@name = name
|
6
|
+
@resource = resource
|
7
|
+
@block = block
|
8
|
+
raise ArgumentError, 'resource or block is required' if @resource.nil? && @block.nil?
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_hash(target)
|
12
|
+
objects = target.__send__(@name)
|
13
|
+
@resource ||= resource_class
|
14
|
+
objects.map { |o| @resource.new(o).to_hash }
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def resource_class
|
20
|
+
klass = Class.new
|
21
|
+
klass.include(::Alba::Resource)
|
22
|
+
klass.class_exec(&@block)
|
23
|
+
klass
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/alba/one.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Alba
|
2
|
+
# Representing one association
|
3
|
+
class One
|
4
|
+
def initialize(name:, resource: nil, &block)
|
5
|
+
@name = name
|
6
|
+
@resource = resource
|
7
|
+
@block = block
|
8
|
+
raise ArgumentError, 'resource or block is required' if @resource.nil? && @block.nil?
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_hash(target)
|
12
|
+
object = target.__send__(@name)
|
13
|
+
@resource ||= resource_class
|
14
|
+
@resource.new(object).to_hash
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def resource_class
|
20
|
+
klass = Class.new
|
21
|
+
klass.include(::Alba::Resource)
|
22
|
+
klass.class_exec(&@block)
|
23
|
+
klass
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/alba/resource.rb
CHANGED
@@ -1,11 +1,21 @@
|
|
1
1
|
require 'alba/serializer'
|
2
2
|
require 'alba/attribute'
|
3
|
+
require 'alba/one'
|
4
|
+
require 'alba/many'
|
3
5
|
require 'alba/serializers/default_serializer'
|
4
6
|
|
5
7
|
module Alba
|
6
8
|
# This module represents what should be serialized
|
7
9
|
module Resource
|
10
|
+
DSLS = [:_attributes, :_one, :_many, :_serializer].freeze
|
8
11
|
def self.included(base)
|
12
|
+
base.class_eval do
|
13
|
+
# Initialize
|
14
|
+
DSLS.each do |name|
|
15
|
+
initial = name == :_serializer ? nil : {}
|
16
|
+
instance_variable_set("@#{name}", initial) unless instance_variable_defined?("@#{name}")
|
17
|
+
end
|
18
|
+
end
|
9
19
|
base.include InstanceMethods
|
10
20
|
base.extend ClassMethods
|
11
21
|
end
|
@@ -14,42 +24,58 @@ module Alba
|
|
14
24
|
module InstanceMethods
|
15
25
|
def initialize(resource)
|
16
26
|
@_resource = resource
|
17
|
-
|
18
|
-
@_serializer_class = self.class._serializer_class
|
27
|
+
DSLS.each { |name| instance_variable_set("@#{name}", self.class.public_send(name)) }
|
19
28
|
end
|
20
29
|
|
21
30
|
def serialize(with: nil)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
serializer = case with
|
32
|
+
when ->(obj) { obj.is_a?(Class) && obj <= Alba::Serializer }
|
33
|
+
with
|
34
|
+
when Symbol
|
35
|
+
const_get(with.to_s.capitalize)
|
36
|
+
when String
|
37
|
+
const_get(with)
|
38
|
+
when nil
|
39
|
+
@_serializer || Alba::Serializers::DefaultSerializer
|
40
|
+
end
|
41
|
+
serializer.new(serializable_hash).serialize
|
42
|
+
end
|
43
|
+
|
44
|
+
def serializable_hash
|
45
|
+
attrs.merge(ones).merge(manies)
|
46
|
+
end
|
47
|
+
alias to_hash serializable_hash
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def attrs
|
52
|
+
@_attributes.transform_values do |attribute|
|
53
|
+
attribute.to_hash(@_resource)
|
54
|
+
end || {}
|
55
|
+
end
|
56
|
+
|
57
|
+
def ones
|
58
|
+
@_one.transform_values do |one|
|
59
|
+
one.to_hash(@_resource)
|
60
|
+
end || {}
|
61
|
+
end
|
62
|
+
|
63
|
+
def manies
|
64
|
+
@_many.transform_values do |many|
|
65
|
+
many.to_hash(@_resource)
|
66
|
+
end || {}
|
37
67
|
end
|
38
68
|
end
|
39
69
|
|
40
70
|
# Class methods
|
41
71
|
module ClassMethods
|
42
|
-
attr_accessor
|
72
|
+
attr_accessor(*DSLS)
|
43
73
|
|
44
74
|
def inherited(subclass)
|
45
|
-
|
46
|
-
@_serializer_class = nil unless defined?(@_serializer_class)
|
47
|
-
subclass._attributes = @_attributes
|
48
|
-
subclass._serializer_class = @_serializer_class
|
75
|
+
DSLS.each { |name| subclass.public_send("#{name}=", instance_variable_get("@#{name}")) }
|
49
76
|
end
|
50
77
|
|
51
78
|
def attributes(*attrs)
|
52
|
-
@_attributes = {} unless defined? @_attributes
|
53
79
|
attrs.each { |attr_name| @_attributes[attr_name] = Attribute.new(name: attr_name, method: attr_name) }
|
54
80
|
end
|
55
81
|
|
@@ -59,8 +85,16 @@ module Alba
|
|
59
85
|
@_attributes[name] = Attribute.new(name: name, method: block)
|
60
86
|
end
|
61
87
|
|
88
|
+
def one(name, resource: nil, &block)
|
89
|
+
@_one[name.to_sym] = One.new(name: name, resource: resource, &block)
|
90
|
+
end
|
91
|
+
|
92
|
+
def many(name, resource: nil, &block)
|
93
|
+
@_many[name.to_sym] = Many.new(name: name, resource: resource, &block)
|
94
|
+
end
|
95
|
+
|
62
96
|
def serializer(name)
|
63
|
-
@
|
97
|
+
@_serializer = name <= Alba::Serializer ? name : nil
|
64
98
|
end
|
65
99
|
end
|
66
100
|
end
|
data/lib/alba/version.rb
CHANGED
data/sider.yml
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
linter:
|
2
|
+
# # https://help.sider.review/getting-started/custom-configuration
|
3
|
+
|
4
|
+
# # https://help.sider.review/tools/ruby/rubocop
|
5
|
+
rubocop:
|
6
|
+
gems:
|
7
|
+
- "rubocop-performance"
|
8
|
+
- "rubocop-sensible"
|
9
|
+
safe: false
|
10
|
+
|
11
|
+
# # https://help.sider.review/tools/ruby/reek
|
12
|
+
# reek:
|
13
|
+
# gems:
|
14
|
+
# - name: "reek"
|
15
|
+
# version: "5.2.0"
|
16
|
+
# target:
|
17
|
+
# - lib/
|
18
|
+
# - test/
|
19
|
+
# config: config/.reek.yml
|
20
|
+
|
21
|
+
# # https://help.sider.review/tools/ruby/querly
|
22
|
+
# querly:
|
23
|
+
# gems:
|
24
|
+
# - "slim"
|
25
|
+
|
26
|
+
# # https://help.sider.review/tools/others/misspell
|
27
|
+
# misspell:
|
28
|
+
# exclude:
|
29
|
+
# - vendor
|
30
|
+
# - "**/*.min.js"
|
31
|
+
# - exclude_file.rb
|
32
|
+
# targets:
|
33
|
+
# - target_directory
|
34
|
+
# - another_target_directory/foo.rb
|
35
|
+
# - bar.rb
|
36
|
+
# locale: UK
|
37
|
+
# ignore: center,behavior
|
38
|
+
|
39
|
+
# # https://help.sider.review/tools/shellscript/shellcheck
|
40
|
+
# shellcheck:
|
41
|
+
# target: "src/**/*.{sh,bash}"
|
42
|
+
# include: "SC2104,SC2105"
|
43
|
+
# exclude: "SC1000,SC1118"
|
44
|
+
# enable: "all"
|
45
|
+
# shell: "bash"
|
46
|
+
# severity: "error"
|
47
|
+
# norc: true
|
48
|
+
|
49
|
+
# # https://help.sider.review/getting-started/custom-configuration#ignore
|
50
|
+
# ignore:
|
51
|
+
# - "*.pdf"
|
52
|
+
# - "*.mp4"
|
53
|
+
# - "images/**"
|
54
|
+
|
55
|
+
# # https://help.sider.review/getting-started/custom-configuration#branchesexclude
|
56
|
+
# branches:
|
57
|
+
# exclude:
|
58
|
+
# - master
|
59
|
+
# - development
|
60
|
+
# - /^release-.*$/
|
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.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OKURA Masafumi
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Fast and flexible JSON serializer
|
14
14
|
email:
|
@@ -31,10 +31,13 @@ files:
|
|
31
31
|
- bin/setup
|
32
32
|
- lib/alba.rb
|
33
33
|
- lib/alba/attribute.rb
|
34
|
+
- lib/alba/many.rb
|
35
|
+
- lib/alba/one.rb
|
34
36
|
- lib/alba/resource.rb
|
35
37
|
- lib/alba/serializer.rb
|
36
38
|
- lib/alba/serializers/default_serializer.rb
|
37
39
|
- lib/alba/version.rb
|
40
|
+
- sider.yml
|
38
41
|
homepage: https://github.com/okuramasafumi/alba
|
39
42
|
licenses:
|
40
43
|
- MIT
|
@@ -42,7 +45,7 @@ metadata:
|
|
42
45
|
homepage_uri: https://github.com/okuramasafumi/alba
|
43
46
|
source_code_uri: https://github.com/okuramasafumi/alba
|
44
47
|
changelog_uri: https://github.com/okuramasafumi/alba/CHANGELOG.md
|
45
|
-
post_install_message:
|
48
|
+
post_install_message:
|
46
49
|
rdoc_options: []
|
47
50
|
require_paths:
|
48
51
|
- lib
|
@@ -57,8 +60,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
60
|
- !ruby/object:Gem::Version
|
58
61
|
version: '0'
|
59
62
|
requirements: []
|
60
|
-
rubygems_version: 3.1.
|
61
|
-
signing_key:
|
63
|
+
rubygems_version: 3.1.4
|
64
|
+
signing_key:
|
62
65
|
specification_version: 4
|
63
66
|
summary: Fast and flexible JSON serializer
|
64
67
|
test_files: []
|