u-struct 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/Gemfile.lock +2 -2
- data/README.md +17 -4
- data/lib/micro/struct/version.rb +1 -1
- data/lib/micro/struct.rb +50 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 393ba5215b21bf91ef0229de920ca0980c44168fcc0e65d539a0f6b4f6a7caa2
|
4
|
+
data.tar.gz: e1ad5cfbd6215982aa8f6e5708d4a6f8cea0a046da9ad82568d0ed8136d58629
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d86bb3a75737927e5f74d56e16c7760b8c7b9c3dce7642ebdef058122212dd21c38aec04573722842ea081995b5e03c6a094a1548bf63f1ed709762c6e5ad2c
|
7
|
+
data.tar.gz: 6dee9c1ab9cd6ba5675cf7c1e6cedc128e70fe45aabf52a2b1beb77fb63e9b01c1d70b0775b4393167132371923b5d56a2efed099048e0e3e594b5c199d95ab3
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.5.0] - 2021-12-03
|
4
|
+
|
5
|
+
- To-do
|
6
|
+
|
7
|
+
## [0.4.0] - 2021-12-03
|
8
|
+
|
9
|
+
- To-do
|
10
|
+
|
11
|
+
## [0.3.1] - 2021-12-02
|
12
|
+
|
13
|
+
- To-do
|
14
|
+
|
15
|
+
## [0.3.0] - 2021-12-02
|
16
|
+
|
17
|
+
- To-do
|
18
|
+
|
19
|
+
## [0.2.0] - 2021-12-02
|
20
|
+
|
21
|
+
- To-do
|
22
|
+
|
3
23
|
## [0.1.0] - 2021-12-02
|
4
24
|
|
5
25
|
- Initial release
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Micro::Struct
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Create powered Ruby structs.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,22 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
```ruby
|
24
|
+
# Like in a regular Struct, you can define one or many attributes.
|
25
|
+
Micro::Struct.new(:first_name, :last_name, ...)
|
26
|
+
|
27
|
+
# You can also pass a block to define custom methods.
|
28
|
+
Micro::Struct.new(:name) {}
|
29
|
+
|
30
|
+
# Available features (use one, many or all):
|
31
|
+
# .with(:to_ary, :to_hash, :to_proc, :readonly, :instance_copy)
|
32
|
+
|
33
|
+
Micro::Struct.with(:to_ary).new(:name)
|
34
|
+
Micro::Struct.with(:to_ary, :to_hash).new(:name)
|
35
|
+
Micro::Struct.with(:to_ary, :to_hash, :to_proc).new(:name)
|
36
|
+
|
37
|
+
Micro::Struct.with(...).new(...) {}
|
38
|
+
```
|
26
39
|
|
27
40
|
## Development
|
28
41
|
|
data/lib/micro/struct/version.rb
CHANGED
data/lib/micro/struct.rb
CHANGED
@@ -3,29 +3,37 @@
|
|
3
3
|
require_relative 'struct/version'
|
4
4
|
|
5
5
|
module Micro
|
6
|
-
#
|
6
|
+
# Like in a regular Struct, you can define one or many attributes.
|
7
|
+
# Micro::Struct.new(:first_name, :last_name, ...)
|
7
8
|
#
|
8
|
-
#
|
9
|
+
# You can also pass a block to define custom methods.
|
10
|
+
# Micro::Struct.new(:name) {}
|
9
11
|
#
|
10
|
-
#
|
12
|
+
# Available features (use one, many or all):
|
13
|
+
# .with(:to_ary, :to_hash, :to_proc, :readonly, :instance_copy)
|
11
14
|
#
|
12
|
-
#
|
15
|
+
# Micro::Struct.with(:to_ary).new(:name)
|
16
|
+
# Micro::Struct.with(:to_ary, :to_hash).new(:name)
|
17
|
+
# Micro::Struct.with(:to_ary, :to_hash, :to_proc).new(:name)
|
13
18
|
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# Micro::Struct.with(...).new(:name) {}
|
19
|
+
# Micro::Struct.with(...).new(...) {}
|
17
20
|
module Struct
|
18
21
|
class Creator
|
19
22
|
module Features
|
20
23
|
DISABLED =
|
21
24
|
{ to_ary: false,
|
22
25
|
to_hash: false,
|
23
|
-
to_proc: false
|
26
|
+
to_proc: false,
|
27
|
+
readonly: false,
|
28
|
+
instance_copy: false
|
29
|
+
}.freeze
|
24
30
|
|
25
|
-
Expose = ->(to_ary:, to_hash:, to_proc:) do
|
31
|
+
Expose = ->(to_ary:, to_hash:, to_proc:, readonly:, instance_copy:) do
|
26
32
|
{ to_ary: to_ary,
|
27
33
|
to_hash: to_hash,
|
28
|
-
to_proc: to_proc
|
34
|
+
to_proc: to_proc,
|
35
|
+
readonly: readonly,
|
36
|
+
instance_copy: instance_copy }
|
29
37
|
end
|
30
38
|
|
31
39
|
def self.check(names)
|
@@ -41,35 +49,40 @@ module Micro
|
|
41
49
|
end
|
42
50
|
|
43
51
|
def new(*members, &block)
|
44
|
-
|
45
|
-
def_struct(
|
46
|
-
def_initialize(
|
52
|
+
def_container do |container|
|
53
|
+
def_struct(container, members, block) do |struct|
|
54
|
+
def_initialize(container, struct)
|
55
|
+
|
47
56
|
def_to_ary(struct)
|
48
57
|
def_to_hash(struct)
|
49
|
-
|
58
|
+
def_readonly(struct)
|
59
|
+
def_instance_copy(struct)
|
50
60
|
end
|
61
|
+
|
62
|
+
def_to_proc(container)
|
51
63
|
end
|
52
64
|
end
|
53
65
|
|
54
66
|
private
|
55
67
|
|
56
|
-
def
|
68
|
+
def def_container(&block)
|
57
69
|
Module.new.tap(&block)
|
58
70
|
end
|
59
71
|
|
60
|
-
def def_struct(
|
72
|
+
def def_struct(container, members, block)
|
61
73
|
struct = ::Struct.new(*members, &block)
|
74
|
+
struct.const_set(:Container, container)
|
62
75
|
struct.send(:private_class_method, :new)
|
63
76
|
|
64
|
-
|
77
|
+
container.const_set(:Struct, struct)
|
65
78
|
|
66
79
|
yield struct
|
67
80
|
end
|
68
81
|
|
69
|
-
def def_initialize(
|
82
|
+
def def_initialize(container, struct)
|
70
83
|
# The .new() method will require all of the Struct's keyword arguments.
|
71
84
|
# We are doing this because Struct's keyword_init option doesn't do that.
|
72
|
-
|
85
|
+
container.module_eval(<<~RUBY, __FILE__, __LINE__ + 1) #
|
73
86
|
def self.new(#{struct.members.join(':, ')}:) # def self.new(a:, b:) do
|
74
87
|
Struct.send(:new, #{struct.members.join(', ')}) # Struct.send(:new, a, b)
|
75
88
|
end # end
|
@@ -88,10 +101,26 @@ module Micro
|
|
88
101
|
struct.send(:alias_method, :to_hash, :to_h) if @features[:to_hash]
|
89
102
|
end
|
90
103
|
|
91
|
-
def
|
104
|
+
def def_readonly(struct)
|
105
|
+
return unless @features[:readonly]
|
106
|
+
|
107
|
+
struct.send(:private, *struct.members.map { |member| "#{member}=" })
|
108
|
+
end
|
109
|
+
|
110
|
+
def def_instance_copy(struct)
|
111
|
+
return unless (@features[:readonly] || @features[:instance_copy])
|
112
|
+
|
113
|
+
struct.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
114
|
+
def with(**members)
|
115
|
+
self.class.const_get(:Container, false).new(**to_h.merge(members))
|
116
|
+
end
|
117
|
+
RUBY
|
118
|
+
end
|
119
|
+
|
120
|
+
def def_to_proc(container)
|
92
121
|
return unless @features[:to_proc]
|
93
122
|
|
94
|
-
|
123
|
+
container.module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
95
124
|
def self.to_proc
|
96
125
|
->(hash) { new(**hash) }
|
97
126
|
end
|