u-struct 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39ecf73c68816127a9919b2ba6fcfc99c4dec9d10e37a15bbcc8ebee881b468c
4
- data.tar.gz: afe359b57dd0561e93309107c1c2bbad3521a70b9fada6dddef02b3d28820bb6
3
+ metadata.gz: 1ebbd60464f2e2d2efd428b0091d68820ef2b5d1a4bc73bed1a474d22f2f0fac
4
+ data.tar.gz: 1965a2b7bd8da50c6c5c31acff5c950590146c060ced14e732d3a5b646dafd8f
5
5
  SHA512:
6
- metadata.gz: cb2708d1a4eabab4da1e1832153babae1f756fbd869e9053de7d62a2f829bfe30ec0c50df66f1cf9139e58d1e65f749d1a12b01b19addf904748fe1f6d7997e3
7
- data.tar.gz: ad4276c480a8e2225c81ea1a9b02fa53fd9bce94d9a3279d94a7d20dc329cc87921c8869a5fd727dc28db2a0e9489273b6a9f37ebf8b304222a67d366a347fea
6
+ metadata.gz: b7274b8401d134cc21408447b87a54472f5f5cdea5263c3ca24e1a4feab6c0104deea8ff1985edb49a3e58d6e91e27f52b908d6fa2974b26ed6472916ebc4cb9
7
+ data.tar.gz: 62238f702e13aaa61d13bcb6213819d79b67e373b3f768cf0675526baa7bf73aa7d1e6c8130d84072f8a6a124923de28318b34b2a44ae17b0de573513c557acf
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-struct (0.3.1)
4
+ u-struct (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Struct
5
- VERSION = '0.3.1'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
data/lib/micro/struct.rb CHANGED
@@ -3,30 +3,110 @@
3
3
  require_relative 'struct/version'
4
4
 
5
5
  module Micro
6
+ # Micro::Struct.new(:first_name, :last_name, ...)
7
+ #
8
+ # Micro::Struct.with(:to_ary).new(:name) # or .with(:to_hash), .with(:to_proc)
9
+ #
10
+ # Micro::Struct.with(:to_ary, :to_hash).new(:name)
11
+ #
12
+ # Micro::Struct.with(:to_ary, :to_hash, :to_proc).new(:name)
13
+ #
14
+ # Micro::Struct.new(:name) {}
15
+ #
16
+ # Micro::Struct.with(...).new(:name) {}
6
17
  module Struct
7
- def self.new(*keys, &block)
8
- struct = ::Struct.new(*keys, &block)
18
+ class Creator
19
+ module Features
20
+ DISABLED =
21
+ { to_ary: false,
22
+ to_hash: false,
23
+ to_proc: false }.freeze
9
24
 
10
- struct.send(:private_class_method, :new)
11
- struct.send(:alias_method, :to_ary, :to_a)
12
- struct.send(:alias_method, :to_hash, :to_h)
25
+ Expose = ->(to_ary:, to_hash:, to_proc:) do
26
+ { to_ary: to_ary,
27
+ to_hash: to_hash,
28
+ to_proc: to_proc }
29
+ end
30
+
31
+ def self.check(names)
32
+ features_to_enable =
33
+ Array(names).each_with_object({}) { |name, memo| memo[name] = true }
13
34
 
14
- mod = Module.new
15
- mod.const_set(:Struct, struct)
35
+ Expose.(**DISABLED.merge(features_to_enable))
36
+ end
37
+ end
16
38
 
17
- # The .new() method will require all of the Struct's keyword arguments.
18
- # We are doing this because Struct's keyword_init option doesn't do that.
19
- mod.module_eval(<<~RUBY, __FILE__, __LINE__ + 1) #
20
- def self.new(#{struct.members.join(':, ')}:) # def self.new(a:, b:) do
21
- Struct.send(:new, #{struct.members.join(', ')}) # Struct.send(:new, a, b)
22
- end # end
39
+ def initialize(features)
40
+ @features = Features.check(features)
41
+ end
23
42
 
24
- def self.to_proc
25
- ->(hash) { new(**hash) }
43
+ def new(*members, &block)
44
+ def_module do |mod|
45
+ def_struct(mod, members, block) do |struct|
46
+ def_initialize(mod, struct)
47
+ def_to_ary(struct)
48
+ def_to_hash(struct)
49
+ def_to_proc(mod)
50
+ end
26
51
  end
27
- RUBY
52
+ end
53
+
54
+ private
55
+
56
+ def def_module(&block)
57
+ Module.new.tap(&block)
58
+ end
59
+
60
+ def def_struct(mod, members, block)
61
+ struct = ::Struct.new(*members, &block)
62
+ struct.send(:private_class_method, :new)
63
+
64
+ mod.const_set(:Struct, struct)
65
+
66
+ yield struct
67
+ end
68
+
69
+ def def_initialize(mod, struct)
70
+ # The .new() method will require all of the Struct's keyword arguments.
71
+ # We are doing this because Struct's keyword_init option doesn't do that.
72
+ mod.module_eval(<<~RUBY, __FILE__, __LINE__ + 1) #
73
+ def self.new(#{struct.members.join(':, ')}:) # def self.new(a:, b:) do
74
+ Struct.send(:new, #{struct.members.join(', ')}) # Struct.send(:new, a, b)
75
+ end # end
28
76
 
29
- mod
77
+ def self.members
78
+ Struct.members
79
+ end
80
+ RUBY
81
+ end
82
+
83
+ def def_to_ary(struct)
84
+ struct.send(:alias_method, :to_ary, :to_a) if @features[:to_ary]
85
+ end
86
+
87
+ def def_to_hash(struct)
88
+ struct.send(:alias_method, :to_hash, :to_h) if @features[:to_hash]
89
+ end
90
+
91
+ def def_to_proc(mod)
92
+ return unless @features[:to_proc]
93
+
94
+ mod.module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
95
+ def self.to_proc
96
+ ->(hash) { new(**hash) }
97
+ end
98
+ RUBY
99
+ end
30
100
  end
101
+
102
+ def self.new(*members, &block)
103
+ with.new(*members, &block)
104
+ end
105
+
106
+ def self.with(*features)
107
+ Creator.new(features)
108
+ end
109
+
110
+ private_constant :Creator
31
111
  end
32
112
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-02 00:00:00.000000000 Z
11
+ date: 2021-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  requirements: []
84
- rubygems_version: 3.2.17
84
+ rubygems_version: 3.2.21
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: Create powered Ruby structs.