u-struct 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ebbd60464f2e2d2efd428b0091d68820ef2b5d1a4bc73bed1a474d22f2f0fac
4
- data.tar.gz: 1965a2b7bd8da50c6c5c31acff5c950590146c060ced14e732d3a5b646dafd8f
3
+ metadata.gz: 393ba5215b21bf91ef0229de920ca0980c44168fcc0e65d539a0f6b4f6a7caa2
4
+ data.tar.gz: e1ad5cfbd6215982aa8f6e5708d4a6f8cea0a046da9ad82568d0ed8136d58629
5
5
  SHA512:
6
- metadata.gz: b7274b8401d134cc21408447b87a54472f5f5cdea5263c3ca24e1a4feab6c0104deea8ff1985edb49a3e58d6e91e27f52b908d6fa2974b26ed6472916ebc4cb9
7
- data.tar.gz: 62238f702e13aaa61d13bcb6213819d79b67e373b3f768cf0675526baa7bf73aa7d1e6c8130d84072f8a6a124923de28318b34b2a44ae17b0de573513c557acf
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-struct (0.4.0)
4
+ u-struct (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -19,4 +19,4 @@ DEPENDENCIES
19
19
  u-struct!
20
20
 
21
21
  BUNDLED WITH
22
- 2.2.26
22
+ 2.2.32
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Micro::Struct
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/micro/struct`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Struct
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
data/lib/micro/struct.rb CHANGED
@@ -3,29 +3,37 @@
3
3
  require_relative 'struct/version'
4
4
 
5
5
  module Micro
6
- # Micro::Struct.new(:first_name, :last_name, ...)
6
+ # Like in a regular Struct, you can define one or many attributes.
7
+ # Micro::Struct.new(:first_name, :last_name, ...)
7
8
  #
8
- # Micro::Struct.with(:to_ary).new(:name) # or .with(:to_hash), .with(:to_proc)
9
+ # You can also pass a block to define custom methods.
10
+ # Micro::Struct.new(:name) {}
9
11
  #
10
- # Micro::Struct.with(:to_ary, :to_hash).new(:name)
12
+ # Available features (use one, many or all):
13
+ # .with(:to_ary, :to_hash, :to_proc, :readonly, :instance_copy)
11
14
  #
12
- # Micro::Struct.with(:to_ary, :to_hash, :to_proc).new(:name)
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
- # Micro::Struct.new(:name) {}
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 }.freeze
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
- def_module do |mod|
45
- def_struct(mod, members, block) do |struct|
46
- def_initialize(mod, struct)
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
- def_to_proc(mod)
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 def_module(&block)
68
+ def def_container(&block)
57
69
  Module.new.tap(&block)
58
70
  end
59
71
 
60
- def def_struct(mod, members, block)
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
- mod.const_set(:Struct, struct)
77
+ container.const_set(:Struct, struct)
65
78
 
66
79
  yield struct
67
80
  end
68
81
 
69
- def def_initialize(mod, struct)
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
- mod.module_eval(<<~RUBY, __FILE__, __LINE__ + 1) #
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 def_to_proc(mod)
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
- mod.module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
123
+ container.module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
95
124
  def self.to_proc
96
125
  ->(hash) { new(**hash) }
97
126
  end
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.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura