u-struct 0.6.0 → 0.7.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/CHANGELOG.md +4 -0
- data/Gemfile +4 -3
- data/Gemfile.lock +9 -1
- data/README.md +21 -8
- data/lib/micro/struct/creator/create_module.rb +2 -2
- data/lib/micro/struct/creator/create_struct.rb +1 -1
- data/lib/micro/struct/creator.rb +5 -5
- data/lib/micro/struct/features.rb +5 -5
- data/lib/micro/struct/normalize_names.rb +17 -0
- data/lib/micro/struct/version.rb +1 -1
- data/lib/micro/struct.rb +23 -13
- metadata +2 -2
- data/lib/micro/struct/validate.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4b40b69da026b0f45f5514a8d340effc8f7d81489c4f1217237c16641186357
|
4
|
+
data.tar.gz: 6a5cd7b12d2d1b16830a805b9904e5c212c685d5a32108439e8f9f2b8235dfb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 159075d6944696849893dd4f4860613b104b12f3e872c0cac65df62055af61309d9f9256a949693a01df7fb5b514e916755bf0f6f1295874d93b9bc1abb2a2b3
|
7
|
+
data.tar.gz: 12db20998fcd5b1688e4786766630cee699daffacfca72d18e3e769ac387931dd310a46ff511862c7d9d9f2a458ae8667dd3e7623dbfca55802caa41fd3cfbb8
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
source
|
3
|
+
source 'https://rubygems.org'
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in micro-struct.gemspec
|
6
6
|
gemspec
|
7
7
|
|
8
|
-
gem
|
8
|
+
gem 'rake', '~> 13.0'
|
9
9
|
|
10
|
-
gem
|
10
|
+
gem 'minitest', '~> 5.0'
|
11
|
+
gem 'simplecov', '~> 0.21.2'
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,20 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
u-struct (0.
|
4
|
+
u-struct (0.7.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
docile (1.4.0)
|
9
10
|
minitest (5.14.4)
|
10
11
|
rake (13.0.6)
|
12
|
+
simplecov (0.21.2)
|
13
|
+
docile (~> 1.1)
|
14
|
+
simplecov-html (~> 0.11)
|
15
|
+
simplecov_json_formatter (~> 0.1)
|
16
|
+
simplecov-html (0.12.3)
|
17
|
+
simplecov_json_formatter (0.1.3)
|
11
18
|
|
12
19
|
PLATFORMS
|
13
20
|
x86_64-darwin-19
|
@@ -16,6 +23,7 @@ DEPENDENCIES
|
|
16
23
|
bundler
|
17
24
|
minitest (~> 5.0)
|
18
25
|
rake (~> 13.0)
|
26
|
+
simplecov (~> 0.21.2)
|
19
27
|
u-struct!
|
20
28
|
|
21
29
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -26,13 +26,20 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
Micro::Struct.new(:first_name, :last_name, ...)
|
28
28
|
|
29
|
-
# Use the `
|
29
|
+
# Use the `optional:` arg if you want some optional attributes.
|
30
30
|
|
31
|
-
Micro::Struct.new(:first_name, :last_name,
|
31
|
+
Micro::Struct.new(:first_name, :last_name, optional: :gender)
|
32
32
|
|
33
|
-
# Using `
|
33
|
+
# Using `optional:` to define all attributes are optional.
|
34
34
|
|
35
|
-
Micro::Struct.new(
|
35
|
+
Micro::Struct.new(optional: [:first_name, :last_name])
|
36
|
+
|
37
|
+
# Use the `required:` arg to define required attributes.
|
38
|
+
|
39
|
+
Micro::Struct.new(
|
40
|
+
required: [:first_name, :last_name],
|
41
|
+
optional: [:gender, :age]
|
42
|
+
)
|
36
43
|
|
37
44
|
# You can also pass a block to define custom methods.
|
38
45
|
|
@@ -52,11 +59,17 @@ Micro::Struct.with(:to_ary, :to_hash, :to_proc, :readonly, :instance_copy).new(:
|
|
52
59
|
Micro::Struct.new(*required)
|
53
60
|
Micro::Struct.new(*required) {}
|
54
61
|
|
55
|
-
Micro::Struct.new(
|
56
|
-
Micro::Struct.new(
|
62
|
+
Micro::Struct.new(optional: *)
|
63
|
+
Micro::Struct.new(optional: *) {}
|
64
|
+
|
65
|
+
Micro::Struct.new(required: *)
|
66
|
+
Micro::Struct.new(required: *) {}
|
67
|
+
|
68
|
+
Micro::Struct.new(*required, optional: *)
|
69
|
+
Micro::Struct.new(*required, optional: *) {}
|
57
70
|
|
58
|
-
Micro::Struct.new(
|
59
|
-
Micro::Struct.new(
|
71
|
+
Micro::Struct.new(required: *, optional: *)
|
72
|
+
Micro::Struct.new(required: *, optional: *) {}
|
60
73
|
|
61
74
|
# Any options above can be used by the `.new()` method of the struct creator returned by the `.with()` method.
|
62
75
|
|
@@ -23,7 +23,7 @@ class Micro::Struct::Creator
|
|
23
23
|
method_arguments = [required, optional].reject(&:empty?).join(', ')
|
24
24
|
struct_arguments = (required_members + optional_members).join(', ')
|
25
25
|
|
26
|
-
container.module_eval(
|
26
|
+
container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
27
27
|
# The .new() method will require all required keyword arguments.
|
28
28
|
# We are doing this because the Struct constructor keyword init option treats everything as optional.
|
29
29
|
#
|
@@ -38,7 +38,7 @@ class Micro::Struct::Creator
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def def_to_proc(container)
|
41
|
-
container.module_eval(
|
41
|
+
container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
42
42
|
def self.to_proc
|
43
43
|
->(hash) { new(**hash) }
|
44
44
|
end
|
@@ -31,7 +31,7 @@ class Micro::Struct::Creator
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def def_instance_copy(struct)
|
34
|
-
struct.class_eval(
|
34
|
+
struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
|
35
35
|
def with(**members)
|
36
36
|
self.class.const_get(:Container, false).new(**to_h.merge(members))
|
37
37
|
end
|
data/lib/micro/struct/creator.rb
CHANGED
@@ -9,13 +9,13 @@ module Micro::Struct
|
|
9
9
|
@features = Features.require(features)
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
NormalizeMemberNames = ->(values) do
|
13
|
+
NormalizeNames::AsSymbols.(values, context: 'member')
|
14
14
|
end
|
15
15
|
|
16
|
-
def new(*members,
|
17
|
-
|
18
|
-
|
16
|
+
def new(*members, required: nil, optional: nil, &block)
|
17
|
+
optional_members = NormalizeMemberNames[optional]
|
18
|
+
required_members = NormalizeMemberNames[members] + NormalizeMemberNames[required]
|
19
19
|
|
20
20
|
container = CreateModule.with(required_members, optional_members, @features)
|
21
21
|
struct = CreateStruct.with(required_members, optional_members, @features, &block)
|
@@ -18,15 +18,15 @@ module Micro::Struct
|
|
18
18
|
instance_copy: instance_copy }
|
19
19
|
end
|
20
20
|
|
21
|
-
|
22
|
-
|
21
|
+
NormalizeFeatureNames = ->(values) do
|
22
|
+
NormalizeNames::AsSymbols.(values, context: 'feature')
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.require(names)
|
26
|
-
|
27
|
-
|
26
|
+
to_enable =
|
27
|
+
NormalizeFeatureNames[names].each_with_object({}) { |name, memo| memo[name] = true }
|
28
28
|
|
29
|
-
Check.(**DISABLED.merge(
|
29
|
+
Check.(**DISABLED.merge(to_enable))
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Micro::Struct
|
4
|
+
module NormalizeNames
|
5
|
+
module AsSymbols
|
6
|
+
REGEXP = /\A[_A-Za-z]\w*\z/.freeze
|
7
|
+
Invalid = ->(context, val) { raise NameError.new("invalid #{context} name: #{val}") }
|
8
|
+
AsSymbol = ->(context, val) { REGEXP =~ val ? val.to_sym : Invalid[context, val] }.curry
|
9
|
+
|
10
|
+
def self.call(values, context:)
|
11
|
+
Array(values).map(&AsSymbol[context])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private_constant :NormalizeNames
|
17
|
+
end
|
data/lib/micro/struct/version.rb
CHANGED
data/lib/micro/struct.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
require_relative 'struct/version'
|
4
4
|
require_relative 'struct/features'
|
5
5
|
require_relative 'struct/creator'
|
6
|
-
require_relative 'struct/
|
6
|
+
require_relative 'struct/normalize_names'
|
7
7
|
|
8
8
|
module Micro
|
9
9
|
# Like in a regular Struct, you can define one or many attributes.
|
@@ -11,13 +11,20 @@ module Micro
|
|
11
11
|
#
|
12
12
|
# Micro::Struct.new(:first_name, :last_name, ...)
|
13
13
|
#
|
14
|
-
# Use the `
|
14
|
+
# Use the `optional:` arg if you want some optional attributes.
|
15
15
|
#
|
16
|
-
# Micro::Struct.new(:first_name, :last_name,
|
16
|
+
# Micro::Struct.new(:first_name, :last_name, optional: :gender)
|
17
17
|
#
|
18
|
-
# Using `
|
18
|
+
# Using `optional:` to define all attributes are optional.
|
19
19
|
#
|
20
|
-
# Micro::Struct.new(
|
20
|
+
# Micro::Struct.new(optional: [:first_name, :last_name])
|
21
|
+
#
|
22
|
+
# Use the `required:` arg to define required attributes.
|
23
|
+
#
|
24
|
+
# Micro::Struct.new(
|
25
|
+
# required: [:first_name, :last_name],
|
26
|
+
# optional: [:gender, :age]
|
27
|
+
# )
|
21
28
|
#
|
22
29
|
# You can also pass a block to define custom methods.
|
23
30
|
#
|
@@ -34,21 +41,24 @@ module Micro
|
|
34
41
|
#
|
35
42
|
# All of the possible combinations to create a Ruby Struct. ;)
|
36
43
|
#
|
37
|
-
# Micro::Struct.new(*
|
38
|
-
# Micro::Struct.new(*
|
44
|
+
# Micro::Struct.new(optional: *)
|
45
|
+
# Micro::Struct.new(optional: *) {}
|
46
|
+
#
|
47
|
+
# Micro::Struct.new(required: *)
|
48
|
+
# Micro::Struct.new(required: *) {}
|
39
49
|
#
|
40
|
-
# Micro::Struct.new(
|
41
|
-
# Micro::Struct.new(
|
50
|
+
# Micro::Struct.new(*required, optional: *)
|
51
|
+
# Micro::Struct.new(*required, optional: *) {}
|
42
52
|
#
|
43
|
-
# Micro::Struct.new(
|
44
|
-
# Micro::Struct.new(
|
53
|
+
# Micro::Struct.new(required: *, optional: *)
|
54
|
+
# Micro::Struct.new(required: *, optional: *) {}
|
45
55
|
#
|
46
56
|
# Any options above can be used by the `.new()` method of the struct creator returned by the `.with()` method.
|
47
57
|
#
|
48
58
|
# Micro::Struct.with(*features).new(...) {}
|
49
59
|
module Struct
|
50
|
-
def self.new(*members,
|
51
|
-
with.new(*members,
|
60
|
+
def self.new(*members, required: nil, optional: nil, &block)
|
61
|
+
with.new(*members, required: required, optional: optional, &block)
|
52
62
|
end
|
53
63
|
|
54
64
|
def self.with(*features)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
@@ -60,7 +60,7 @@ files:
|
|
60
60
|
- lib/micro/struct/creator/create_module.rb
|
61
61
|
- lib/micro/struct/creator/create_struct.rb
|
62
62
|
- lib/micro/struct/features.rb
|
63
|
-
- lib/micro/struct/
|
63
|
+
- lib/micro/struct/normalize_names.rb
|
64
64
|
- lib/micro/struct/version.rb
|
65
65
|
- lib/u-struct.rb
|
66
66
|
- u-struct.gemspec
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Micro::Struct
|
4
|
-
module Validate
|
5
|
-
module Names
|
6
|
-
REGEXP = /\A[_A-Za-z]\w*\z/.freeze
|
7
|
-
Invalid = ->(label, val) { raise NameError.new("invalid #{label} name: #{val}") }
|
8
|
-
AsSymbol = ->(label, val) { REGEXP =~ val ? val.to_sym : Invalid[label, val] }.curry
|
9
|
-
|
10
|
-
def self.call(values, label:)
|
11
|
-
Array(values).map(&Names::AsSymbol[label])
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
private_constant :Validate
|
17
|
-
end
|