u-struct 0.9.0 → 0.10.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: '0765309623bfa9eb720102c66cd30f5ba785bdada60af711f19a03a51a7027bd'
4
- data.tar.gz: 216dd4920427d682c9e797e5ec0a8631486b5d8c0613391440bd0f94f2778824
3
+ metadata.gz: ed0c19c23a4cbdd7282390be2590b7b9ae8b025badd32ffbaae9adf128d79667
4
+ data.tar.gz: b18729b71798828d9ba517f635e3b2b8e6baff640724b0837bac66be14e72ed8
5
5
  SHA512:
6
- metadata.gz: ed911103da03769ac8014a618d713b313e20a19a65074ac1a737f2d4cd1717dbdf521d0f7693665d840bdbff145e518afe3e7ef875b1eef55d2555e198bf8d62
7
- data.tar.gz: 683f94804b10f3ff03ed3f6ecbf4ff40c889dacf8dfd964496e8807974f96cbb611399fa21d95d73881a9c20d4dd0296975528cafc59dfbb20128471149c398a
6
+ metadata.gz: '0519e4b525fb8e62021c8ec5e518f503c6f14d865f48942c9dd145864f08ca2c3cbfb5c65fae703c3f91c3730ffd04881890d950c335ecc14789cdb28f24a541'
7
+ data.tar.gz: c0068d5051527141d7d1bfd59de4cd5e7b026abbbb5abe2d0a9b45b0a74de674e51bda16d483400f1cdeb9284a7bdb6093ba5989d8eb940e4a431e70bed12ee7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.10.0] - 2021-12-15
4
+
5
+ - To-do
6
+
3
7
  ## [0.9.0] - 2021-12-14
4
8
 
5
9
  - To-do
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-struct (0.9.0)
4
+ u-struct (0.10.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -2,16 +2,6 @@
2
2
 
3
3
  module RGB
4
4
  Color = Micro::Struct.with(:readonly, :to_ary).new(:red, :green, :blue) do
5
- def to_a
6
- super.map(&:value)
7
- end
8
-
9
- def to_hex
10
- "##{red}#{green}#{blue}"
11
- end
12
- end
13
-
14
- module Color
15
5
  def self.new(r:, g:, b:)
16
6
  __new__(
17
7
  red: Number.new(r, label: 'r'),
@@ -19,5 +9,13 @@ module RGB
19
9
  blue: Number.new(b, label: 'b')
20
10
  )
21
11
  end
12
+
13
+ def to_a
14
+ super.map(&:value)
15
+ end
16
+
17
+ def to_hex
18
+ "##{red}#{green}#{blue}"
19
+ end
22
20
  end
23
21
  end
@@ -2,12 +2,6 @@
2
2
 
3
3
  module RGB
4
4
  Number = Micro::Struct.with(:readonly).new(:value) do
5
- def to_s
6
- value.to_s(16)
7
- end
8
- end
9
-
10
- module Number
11
5
  Input = Kind.object(name: 'Integer(>= 0 and <= 255)') do |value|
12
6
  value.is_a?(::Integer) && value >= 0 && value <= 255
13
7
  end
@@ -15,5 +9,9 @@ module RGB
15
9
  def self.new(value, label:)
16
10
  __new__(value: Input[value, label: label])
17
11
  end
12
+
13
+ def to_s
14
+ value.to_s(16)
15
+ end
18
16
  end
19
17
  end
data/examples/rgb_1.rb CHANGED
@@ -10,23 +10,21 @@ gemfile do
10
10
  end
11
11
 
12
12
  RGBColor = Micro::Struct.with(:readonly, :to_ary).new(:red, :green, :blue) do
13
- def to_hex
14
- "##{red.to_s(16)}#{green.to_s(16)}#{blue.to_s(16)}"
15
- end
16
- end
17
-
18
- module RGBColor
19
- ColorNumber = Kind.object(name: 'Integer(>= 0 and <= 255)') do |value|
13
+ Number = Kind.object(name: 'Integer(>= 0 and <= 255)') do |value|
20
14
  value.is_a?(::Integer) && value >= 0 && value <= 255
21
15
  end
22
16
 
23
17
  def self.new(r:, g:, b:)
24
18
  __new__(
25
- red: ColorNumber[r, label: 'r'],
26
- green: ColorNumber[g, label: 'g'],
27
- blue: ColorNumber[b, label: 'b']
19
+ red: Number[r, label: 'r'],
20
+ green: Number[g, label: 'g'],
21
+ blue: Number[b, label: 'b']
28
22
  )
29
23
  end
24
+
25
+ def to_hex
26
+ "##{red.to_s(16)}#{green.to_s(16)}#{blue.to_s(16)}"
27
+ end
30
28
  end
31
29
 
32
30
  rgb_color = RGBColor.new(r: 1, g: 1, b: 255)
data/examples/rgb_2.rb CHANGED
@@ -10,12 +10,6 @@ gemfile do
10
10
  end
11
11
 
12
12
  RGBNumber = Micro::Struct.with(:readonly).new(:value) do
13
- def to_s
14
- value.to_s(16)
15
- end
16
- end
17
-
18
- module RGBNumber
19
13
  Input = Kind.object(name: 'Integer(>= 0 and <= 255)') do |value|
20
14
  value.is_a?(::Integer) && value >= 0 && value <= 255
21
15
  end
@@ -23,19 +17,13 @@ module RGBNumber
23
17
  def self.new(value, label:)
24
18
  __new__(value: Input[value, label: label])
25
19
  end
26
- end
27
-
28
- RGBColor = Micro::Struct.with(:readonly, :to_ary).new(:red, :green, :blue) do
29
- def to_a
30
- [red.value, green.value, blue.value]
31
- end
32
20
 
33
- def to_hex
34
- "##{red}#{green}#{blue}"
21
+ def to_s
22
+ value.to_s(16)
35
23
  end
36
24
  end
37
25
 
38
- module RGBColor
26
+ RGBColor = Micro::Struct.with(:readonly, :to_ary).new(:red, :green, :blue) do
39
27
  def self.new(r:, g:, b:)
40
28
  __new__(
41
29
  red: RGBNumber.new(r, label: 'r'),
@@ -43,6 +31,14 @@ module RGBColor
43
31
  blue: RGBNumber.new(b, label: 'b')
44
32
  )
45
33
  end
34
+
35
+ def to_a
36
+ [red.value, green.value, blue.value]
37
+ end
38
+
39
+ def to_hex
40
+ "##{red}#{green}#{blue}"
41
+ end
46
42
  end
47
43
 
48
44
  rgb_color = RGBColor.new(r: 1, g: 1, b: 255)
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Micro::Struct::Factory
4
+ module CreateStruct
5
+ extend self
6
+
7
+ def with(required_members, optional_members, features, &block)
8
+ struct = ::Struct.new(*(required_members + optional_members))
9
+
10
+ def_initialize(struct, required_members, optional_members)
11
+ def_to_ary(struct) if features[:to_ary]
12
+ def_to_hash(struct) if features[:to_hash]
13
+ def_to_proc(struct) if features[:to_proc]
14
+ def_readonly(struct) if features[:readonly]
15
+ def_instance_copy(struct) if features[:instance_copy]
16
+
17
+ struct.class_eval(&block) if block
18
+
19
+ struct
20
+ end
21
+
22
+ private
23
+
24
+ def def_initialize(struct, required_members, optional_members)
25
+ required = "#{required_members.join(':, ')}#{':' unless required_members.empty?}"
26
+ optional = "#{optional_members.join(': nil, ')}#{': nil' unless optional_members.empty?}"
27
+
28
+ keywords_arguments = [required, optional].reject(&:empty?).join(', ')
29
+ positional_arguments = (required_members + optional_members).join(', ')
30
+
31
+ # The .new() method will require all required keyword arguments.
32
+ # We are doing this because the Struct constructor keyword init option treats everything as optional.
33
+ #
34
+ struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
35
+ class << self
36
+ undef_method :new
37
+
38
+ def new(#{keywords_arguments}) # def new(a:, b:, c: nil) do
39
+ instance = allocate # instance = allocate
40
+ instance.send(:initialize, #{positional_arguments}) # instance.send(:initialize, a, b, c)
41
+ instance # instance
42
+ end # end
43
+
44
+ alias __new__ new
45
+ end
46
+ RUBY
47
+ end
48
+
49
+ def def_to_ary(struct)
50
+ struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
51
+ def to_ary
52
+ to_a
53
+ end
54
+ RUBY
55
+ end
56
+
57
+ def def_to_hash(struct)
58
+ struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
59
+ def to_hash
60
+ to_h
61
+ end
62
+ RUBY
63
+ end
64
+
65
+ def def_readonly(struct)
66
+ struct.send(:private, *struct.members.map { |member| "#{member}=" })
67
+ end
68
+
69
+ def def_instance_copy(struct)
70
+ struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
71
+ def with(**members)
72
+ self.class.new(**to_h.merge(members))
73
+ end
74
+ RUBY
75
+ end
76
+
77
+ def def_to_proc(struct)
78
+ struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
79
+ def self.to_proc
80
+ ->(hash) { new(**hash) }
81
+ end
82
+ RUBY
83
+ end
84
+ end
85
+
86
+ private_constant :CreateStruct
87
+ end
@@ -1,9 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Micro::Struct
4
- class Creator
5
- require_relative 'creator/create_module'
6
- require_relative 'creator/create_struct'
4
+ class Factory
5
+ require_relative 'factory/create_struct'
7
6
 
8
7
  def initialize(features)
9
8
  @features = Features.require(features)
@@ -17,13 +16,9 @@ module Micro::Struct
17
16
  optional_members = NormalizeMemberNames[optional]
18
17
  required_members = NormalizeMemberNames[members] + NormalizeMemberNames[required]
19
18
 
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)
19
+ CreateStruct.with(required_members, optional_members, @features, &block)
25
20
  end
26
21
  end
27
22
 
28
- private_constant :Creator
23
+ private_constant :Factory
29
24
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Struct
5
- VERSION = '0.9.0'
5
+ VERSION = '0.10.0'
6
6
  end
7
7
  end
data/lib/micro/struct.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative 'struct/version'
4
4
  require_relative 'struct/features'
5
- require_relative 'struct/creator'
5
+ require_relative 'struct/factory'
6
6
  require_relative 'struct/normalize_names'
7
7
 
8
8
  module Micro
@@ -62,7 +62,7 @@ module Micro
62
62
  end
63
63
 
64
64
  def self.with(*features)
65
- Creator.new(features)
65
+ Factory.new(features)
66
66
  end
67
67
  end
68
68
  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.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
@@ -63,9 +63,8 @@ files:
63
63
  - examples/rgb_2.rb
64
64
  - examples/rgb_3.rb
65
65
  - lib/micro/struct.rb
66
- - lib/micro/struct/creator.rb
67
- - lib/micro/struct/creator/create_module.rb
68
- - lib/micro/struct/creator/create_struct.rb
66
+ - lib/micro/struct/factory.rb
67
+ - lib/micro/struct/factory/create_struct.rb
69
68
  - lib/micro/struct/features.rb
70
69
  - lib/micro/struct/normalize_names.rb
71
70
  - lib/micro/struct/version.rb
@@ -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