u-struct 0.6.0 → 0.7.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: 52450d53b7accc5aa4c83682dfc6557d3f5fa4d7879648c189701198548e0ace
4
- data.tar.gz: 4a25f913523f7aa9e79e96e49cafd33cff2ec748dac79d0bca611cd67aa304e3
3
+ metadata.gz: f4b40b69da026b0f45f5514a8d340effc8f7d81489c4f1217237c16641186357
4
+ data.tar.gz: 6a5cd7b12d2d1b16830a805b9904e5c212c685d5a32108439e8f9f2b8235dfb8
5
5
  SHA512:
6
- metadata.gz: 1bd5940255d259aeebbe5207a0a397bfade550bbc0e4bf719233ef18c2ba8f3fb483b96fb932fd775f7b98c35af184292b5f7392b32ebf0456888379b13c857c
7
- data.tar.gz: 0ca2437cf6a48c090cc176324477b579c394b11c8c451a14a11c62a9509e5b3f6f722f1318d777e4db610cf4ab004b7f820dba169544fca0c31fb6033d03cf76
6
+ metadata.gz: 159075d6944696849893dd4f4860613b104b12f3e872c0cac65df62055af61309d9f9256a949693a01df7fb5b514e916755bf0f6f1295874d93b9bc1abb2a2b3
7
+ data.tar.gz: 12db20998fcd5b1688e4786766630cee699daffacfca72d18e3e769ac387931dd310a46ff511862c7d9d9f2a458ae8667dd3e7623dbfca55802caa41fd3cfbb8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.7.0] - 2021-12-04
4
+
5
+ - To-do
6
+
3
7
  ## [0.6.0] - 2021-12-03
4
8
 
5
9
  - To-do
data/Gemfile CHANGED
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source "https://rubygems.org"
3
+ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in micro-struct.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
8
+ gem 'rake', '~> 13.0'
9
9
 
10
- gem "minitest", "~> 5.0"
10
+ gem 'minitest', '~> 5.0'
11
+ gem 'simplecov', '~> 0.21.2'
data/Gemfile.lock CHANGED
@@ -1,13 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-struct (0.6.0)
4
+ u-struct (0.7.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ docile (1.4.0)
9
10
  minitest (5.14.4)
10
11
  rake (13.0.6)
12
+ simplecov (0.21.2)
13
+ docile (~> 1.1)
14
+ simplecov-html (~> 0.11)
15
+ simplecov_json_formatter (~> 0.1)
16
+ simplecov-html (0.12.3)
17
+ simplecov_json_formatter (0.1.3)
11
18
 
12
19
  PLATFORMS
13
20
  x86_64-darwin-19
@@ -16,6 +23,7 @@ DEPENDENCIES
16
23
  bundler
17
24
  minitest (~> 5.0)
18
25
  rake (~> 13.0)
26
+ simplecov (~> 0.21.2)
19
27
  u-struct!
20
28
 
21
29
  BUNDLED WITH
data/README.md CHANGED
@@ -26,13 +26,20 @@ Or install it yourself as:
26
26
 
27
27
  Micro::Struct.new(:first_name, :last_name, ...)
28
28
 
29
- # Use the `_optional:` arg if you want some optional attributes.
29
+ # Use the `optional:` arg if you want some optional attributes.
30
30
 
31
- Micro::Struct.new(:first_name, :last_name, _optional: :gender)
31
+ Micro::Struct.new(:first_name, :last_name, optional: :gender)
32
32
 
33
- # Using `_optional:` to define all attributes are optional.
33
+ # Using `optional:` to define all attributes are optional.
34
34
 
35
- Micro::Struct.new(_optional: [:first_name, :last_name])
35
+ Micro::Struct.new(optional: [:first_name, :last_name])
36
+
37
+ # Use the `required:` arg to define required attributes.
38
+
39
+ Micro::Struct.new(
40
+ required: [:first_name, :last_name],
41
+ optional: [:gender, :age]
42
+ )
36
43
 
37
44
  # You can also pass a block to define custom methods.
38
45
 
@@ -52,11 +59,17 @@ Micro::Struct.with(:to_ary, :to_hash, :to_proc, :readonly, :instance_copy).new(:
52
59
  Micro::Struct.new(*required)
53
60
  Micro::Struct.new(*required) {}
54
61
 
55
- Micro::Struct.new(_optional: *)
56
- Micro::Struct.new(_optional: *) {}
62
+ Micro::Struct.new(optional: *)
63
+ Micro::Struct.new(optional: *) {}
64
+
65
+ Micro::Struct.new(required: *)
66
+ Micro::Struct.new(required: *) {}
67
+
68
+ Micro::Struct.new(*required, optional: *)
69
+ Micro::Struct.new(*required, optional: *) {}
57
70
 
58
- Micro::Struct.new(*required, _optional: *)
59
- Micro::Struct.new(*required, _optional: *) {}
71
+ Micro::Struct.new(required: *, optional: *)
72
+ Micro::Struct.new(required: *, optional: *) {}
60
73
 
61
74
  # Any options above can be used by the `.new()` method of the struct creator returned by the `.with()` method.
62
75
 
@@ -23,7 +23,7 @@ class Micro::Struct::Creator
23
23
  method_arguments = [required, optional].reject(&:empty?).join(', ')
24
24
  struct_arguments = (required_members + optional_members).join(', ')
25
25
 
26
- container.module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
26
+ container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
27
27
  # The .new() method will require all required keyword arguments.
28
28
  # We are doing this because the Struct constructor keyword init option treats everything as optional.
29
29
  #
@@ -38,7 +38,7 @@ class Micro::Struct::Creator
38
38
  end
39
39
 
40
40
  def def_to_proc(container)
41
- container.module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
41
+ container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
42
42
  def self.to_proc
43
43
  ->(hash) { new(**hash) }
44
44
  end
@@ -31,7 +31,7 @@ class Micro::Struct::Creator
31
31
  end
32
32
 
33
33
  def def_instance_copy(struct)
34
- struct.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
34
+ struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
35
35
  def with(**members)
36
36
  self.class.const_get(:Container, false).new(**to_h.merge(members))
37
37
  end
@@ -9,13 +9,13 @@ module Micro::Struct
9
9
  @features = Features.require(features)
10
10
  end
11
11
 
12
- ValidateMemberNames = ->(values) do
13
- Validate::Names.(values, label: 'member')
12
+ NormalizeMemberNames = ->(values) do
13
+ NormalizeNames::AsSymbols.(values, context: 'member')
14
14
  end
15
15
 
16
- def new(*members, _optional: nil, &block)
17
- required_members = ValidateMemberNames[members]
18
- optional_members = ValidateMemberNames[_optional]
16
+ def new(*members, required: nil, optional: nil, &block)
17
+ optional_members = NormalizeMemberNames[optional]
18
+ required_members = NormalizeMemberNames[members] + NormalizeMemberNames[required]
19
19
 
20
20
  container = CreateModule.with(required_members, optional_members, @features)
21
21
  struct = CreateStruct.with(required_members, optional_members, @features, &block)
@@ -18,15 +18,15 @@ module Micro::Struct
18
18
  instance_copy: instance_copy }
19
19
  end
20
20
 
21
- ValidateFeatureNames = ->(values) do
22
- Validate::Names.(values, label: 'feature')
21
+ NormalizeFeatureNames = ->(values) do
22
+ NormalizeNames::AsSymbols.(values, context: 'feature')
23
23
  end
24
24
 
25
25
  def self.require(names)
26
- features_to_enable =
27
- ValidateFeatureNames[names].each_with_object({}) { |name, memo| memo[name] = true }
26
+ to_enable =
27
+ NormalizeFeatureNames[names].each_with_object({}) { |name, memo| memo[name] = true }
28
28
 
29
- Check.(**DISABLED.merge(features_to_enable))
29
+ Check.(**DISABLED.merge(to_enable))
30
30
  end
31
31
  end
32
32
 
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Micro::Struct
4
+ module NormalizeNames
5
+ module AsSymbols
6
+ REGEXP = /\A[_A-Za-z]\w*\z/.freeze
7
+ Invalid = ->(context, val) { raise NameError.new("invalid #{context} name: #{val}") }
8
+ AsSymbol = ->(context, val) { REGEXP =~ val ? val.to_sym : Invalid[context, val] }.curry
9
+
10
+ def self.call(values, context:)
11
+ Array(values).map(&AsSymbol[context])
12
+ end
13
+ end
14
+ end
15
+
16
+ private_constant :NormalizeNames
17
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Struct
5
- VERSION = '0.6.0'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  end
data/lib/micro/struct.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require_relative 'struct/version'
4
4
  require_relative 'struct/features'
5
5
  require_relative 'struct/creator'
6
- require_relative 'struct/validate'
6
+ require_relative 'struct/normalize_names'
7
7
 
8
8
  module Micro
9
9
  # Like in a regular Struct, you can define one or many attributes.
@@ -11,13 +11,20 @@ module Micro
11
11
  #
12
12
  # Micro::Struct.new(:first_name, :last_name, ...)
13
13
  #
14
- # Use the `_optional:` arg if you want some optional attributes.
14
+ # Use the `optional:` arg if you want some optional attributes.
15
15
  #
16
- # Micro::Struct.new(:first_name, :last_name, _optional: :gender)
16
+ # Micro::Struct.new(:first_name, :last_name, optional: :gender)
17
17
  #
18
- # Using `_optional:` to define all attributes are optional.
18
+ # Using `optional:` to define all attributes are optional.
19
19
  #
20
- # Micro::Struct.new(_optional: [:first_name, :last_name])
20
+ # Micro::Struct.new(optional: [:first_name, :last_name])
21
+ #
22
+ # Use the `required:` arg to define required attributes.
23
+ #
24
+ # Micro::Struct.new(
25
+ # required: [:first_name, :last_name],
26
+ # optional: [:gender, :age]
27
+ # )
21
28
  #
22
29
  # You can also pass a block to define custom methods.
23
30
  #
@@ -34,21 +41,24 @@ module Micro
34
41
  #
35
42
  # All of the possible combinations to create a Ruby Struct. ;)
36
43
  #
37
- # Micro::Struct.new(*required)
38
- # Micro::Struct.new(*required) {}
44
+ # Micro::Struct.new(optional: *)
45
+ # Micro::Struct.new(optional: *) {}
46
+ #
47
+ # Micro::Struct.new(required: *)
48
+ # Micro::Struct.new(required: *) {}
39
49
  #
40
- # Micro::Struct.new(_optional: *)
41
- # Micro::Struct.new(_optional: *) {}
50
+ # Micro::Struct.new(*required, optional: *)
51
+ # Micro::Struct.new(*required, optional: *) {}
42
52
  #
43
- # Micro::Struct.new(*required, _optional: *)
44
- # Micro::Struct.new(*required, _optional: *) {}
53
+ # Micro::Struct.new(required: *, optional: *)
54
+ # Micro::Struct.new(required: *, optional: *) {}
45
55
  #
46
56
  # Any options above can be used by the `.new()` method of the struct creator returned by the `.with()` method.
47
57
  #
48
58
  # Micro::Struct.with(*features).new(...) {}
49
59
  module Struct
50
- def self.new(*members, _optional: nil, &block)
51
- with.new(*members, _optional: _optional, &block)
60
+ def self.new(*members, required: nil, optional: nil, &block)
61
+ with.new(*members, required: required, optional: optional, &block)
52
62
  end
53
63
 
54
64
  def self.with(*features)
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
@@ -60,7 +60,7 @@ files:
60
60
  - lib/micro/struct/creator/create_module.rb
61
61
  - lib/micro/struct/creator/create_struct.rb
62
62
  - lib/micro/struct/features.rb
63
- - lib/micro/struct/validate.rb
63
+ - lib/micro/struct/normalize_names.rb
64
64
  - lib/micro/struct/version.rb
65
65
  - lib/u-struct.rb
66
66
  - u-struct.gemspec
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Micro::Struct
4
- module Validate
5
- module Names
6
- REGEXP = /\A[_A-Za-z]\w*\z/.freeze
7
- Invalid = ->(label, val) { raise NameError.new("invalid #{label} name: #{val}") }
8
- AsSymbol = ->(label, val) { REGEXP =~ val ? val.to_sym : Invalid[label, val] }.curry
9
-
10
- def self.call(values, label:)
11
- Array(values).map(&Names::AsSymbol[label])
12
- end
13
- end
14
- end
15
-
16
- private_constant :Validate
17
- end