u-struct 0.8.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Micro::Struct::Creator
4
- module CreateModule
5
- extend self
6
-
7
- def with(required_members, optional_members, features)
8
- container = Module.new
9
-
10
- def_initialize(container, required_members, optional_members)
11
- def_triple_eq(container)
12
- def_members(container)
13
- def_to_proc(container, features)
14
-
15
- container
16
- end
17
-
18
- private
19
-
20
- def def_initialize(container, required_members, optional_members)
21
- required = "#{required_members.join(':, ')}#{':' unless required_members.empty?}"
22
- optional = "#{optional_members.join(': nil, ')}#{': nil' unless optional_members.empty?}"
23
-
24
- method_arguments = [required, optional].reject(&:empty?).join(', ')
25
- struct_arguments = (required_members + optional_members).join(', ')
26
-
27
- # The .new() method will require all required keyword arguments.
28
- # We are doing this because the Struct constructor keyword init option treats everything as optional.
29
- #
30
- container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
31
- def self.new(#{method_arguments}) # def self.new(a:, b:) do
32
- Struct.send(:new, #{struct_arguments}) # Struct.send(:new, a, b)
33
- end # end
34
- RUBY
35
- end
36
-
37
- def def_triple_eq(container)
38
- container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
39
- def self.===(other)
40
- Struct === other
41
- end
42
- RUBY
43
- end
44
-
45
- def def_members(container)
46
- container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
47
- def self.members
48
- Struct.members
49
- end
50
- RUBY
51
- end
52
-
53
- def def_to_proc(container, features)
54
- return unless features[:to_proc]
55
-
56
- container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
57
- def self.to_proc
58
- ->(hash) { new(**hash) }
59
- end
60
- RUBY
61
- end
62
- end
63
-
64
- private_constant :CreateModule
65
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Micro::Struct::Creator
4
- module CreateStruct
5
- extend self
6
-
7
- def with(required_members, optional_members, features, &block)
8
- struct = ::Struct.new(*(required_members + optional_members), &block)
9
- struct.send(:private_class_method, :new)
10
-
11
- def_to_ary(struct) if features[:to_ary]
12
- def_to_hash(struct) if features[:to_hash]
13
- def_readonly(struct) if features[:readonly]
14
- def_instance_copy(struct) if features[:readonly] || features[:instance_copy]
15
-
16
- struct
17
- end
18
-
19
- private
20
-
21
- def def_to_ary(struct)
22
- struct.send(:alias_method, :to_ary, :to_a)
23
- end
24
-
25
- def def_to_hash(struct)
26
- struct.send(:alias_method, :to_hash, :to_h)
27
- end
28
-
29
- def def_readonly(struct)
30
- struct.send(:private, *struct.members.map { |member| "#{member}=" })
31
- end
32
-
33
- def def_instance_copy(struct)
34
- struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
35
- def with(**members)
36
- self.class.const_get(:Container, false).new(**to_h.merge(members))
37
- end
38
- RUBY
39
- end
40
- end
41
-
42
- private_constant :CreateStruct
43
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Micro::Struct
4
- class Creator
5
- require_relative 'creator/create_module'
6
- require_relative 'creator/create_struct'
7
-
8
- def initialize(features)
9
- @features = Features.require(features)
10
- end
11
-
12
- NormalizeMemberNames = ->(values) do
13
- NormalizeNames::AsSymbols.(values, context: 'member')
14
- end
15
-
16
- def new(*members, required: nil, optional: nil, &block)
17
- optional_members = NormalizeMemberNames[optional]
18
- required_members = NormalizeMemberNames[members] + NormalizeMemberNames[required]
19
-
20
- container = CreateModule.with(required_members, optional_members, @features)
21
- struct = CreateStruct.with(required_members, optional_members, @features, &block)
22
-
23
- container.const_set(:Struct, struct)
24
- struct.const_set(:Container, container)
25
- end
26
- end
27
-
28
- private_constant :Creator
29
- end