u-struct 0.9.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,67 +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) if features[:to_proc]
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
-
35
- class << self
36
- alias new __new__
37
- end
38
- RUBY
39
- end
40
-
41
- def def_triple_eq(container)
42
- container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
43
- def self.===(other)
44
- Struct === other
45
- end
46
- RUBY
47
- end
48
-
49
- def def_members(container)
50
- container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
51
- def self.members
52
- Struct.members
53
- end
54
- RUBY
55
- end
56
-
57
- def def_to_proc(container)
58
- container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
59
- def self.to_proc
60
- ->(hash) { new(**hash) }
61
- end
62
- RUBY
63
- end
64
- end
65
-
66
- private_constant :CreateModule
67
- end
@@ -1,51 +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[:instance_copy]
15
-
16
- struct
17
- end
18
-
19
- private
20
-
21
- def def_to_ary(struct)
22
- struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
23
- def to_ary
24
- to_a
25
- end
26
- RUBY
27
- end
28
-
29
- def def_to_hash(struct)
30
- struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
31
- def to_hash
32
- to_h
33
- end
34
- RUBY
35
- end
36
-
37
- def def_readonly(struct)
38
- struct.send(:private, *struct.members.map { |member| "#{member}=" })
39
- end
40
-
41
- def def_instance_copy(struct)
42
- struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
43
- def with(**members)
44
- self.class.const_get(:Container, false).new(**to_h.merge(members))
45
- end
46
- RUBY
47
- end
48
- end
49
-
50
- private_constant :CreateStruct
51
- 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