u-struct 0.8.0 → 0.9.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: bebaeb99b56b26288a87f8907532e194a4388563a11ed486c90d203c34d9e951
4
- data.tar.gz: 66d1f615dcaa99c713d24b7ea375f00293396fe91d2314be223a487cfa90caa8
3
+ metadata.gz: '0765309623bfa9eb720102c66cd30f5ba785bdada60af711f19a03a51a7027bd'
4
+ data.tar.gz: 216dd4920427d682c9e797e5ec0a8631486b5d8c0613391440bd0f94f2778824
5
5
  SHA512:
6
- metadata.gz: 4131a340a7a5981a60a177a2a1c59846f12693053eb80cd77e289e83a3602e10df31b694c8bb8a0fbb9a8d60886ce4b0e27424cce27a1ae2e513570d981c0c67
7
- data.tar.gz: 2ed2f7a1a80265ace687639e575f5395f3cb1c663ac0c4b5dc044a52a2ba321540942523a6a7f0b9377df6d2a4569e029b35a8f1345e861ab23cd0f5cd0d885b
6
+ metadata.gz: ed911103da03769ac8014a618d713b313e20a19a65074ac1a737f2d4cd1717dbdf521d0f7693665d840bdbff145e518afe3e7ef875b1eef55d2555e198bf8d62
7
+ data.tar.gz: 683f94804b10f3ff03ed3f6ecbf4ff40c889dacf8dfd964496e8807974f96cbb611399fa21d95d73881a9c20d4dd0296975528cafc59dfbb20128471149c398a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.9.0] - 2021-12-14
4
+
5
+ - To-do
6
+
3
7
  ## [0.8.0] - 2021-12-05
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.8.0)
4
+ u-struct (0.9.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ source 'https://rubygems.org'
7
+
8
+ gem 'u-struct', path: '..'
9
+ end
10
+
11
+ Person = Micro::Struct.new(:first_name, :last_name, optional: :age) do
12
+ def name
13
+ "#{first_name} #{last_name}"
14
+ end
15
+ end
16
+
17
+ person = Person.new(first_name: 'Rodrigo', last_name: 'Serradura')
18
+
19
+ puts format('first_name: %p', person.first_name)
20
+ puts format('last_name: %p', person.last_name)
21
+ puts format('name: %p', person.name)
22
+ puts format('age: %p', person.age)
23
+ puts
24
+
25
+ person.age = rand(18..100)
26
+
27
+ puts format('age: %p', person.age)
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ source 'https://rubygems.org'
7
+
8
+ gem 'u-struct', path: '..'
9
+ end
10
+
11
+ Person = Micro::Struct.with(:readonly, :instance_copy).new(
12
+ required: [:first_name, :last_name],
13
+ optional: [:age]
14
+ ) do
15
+ def name
16
+ "#{first_name} #{last_name}"
17
+ end
18
+ end
19
+
20
+ person = Person.new(first_name: 'Rodrigo', last_name: 'Serradura')
21
+
22
+ puts format('first_name: %p', person.first_name)
23
+ puts format('last_name: %p', person.last_name)
24
+ puts format('name: %p', person.name)
25
+ puts format('age: %p', person.age)
26
+ puts
27
+
28
+ rand_age = rand(18..100)
29
+
30
+ new_person = person.with(age: rand_age)
31
+
32
+ puts new_person.equal?(person)
33
+
34
+ puts format('age: %p', person.age)
35
+ puts format('age: %p', new_person.age)
36
+
37
+ person.age = rand_age
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RGB
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
+ def self.new(r:, g:, b:)
16
+ __new__(
17
+ red: Number.new(r, label: 'r'),
18
+ green: Number.new(g, label: 'g'),
19
+ blue: Number.new(b, label: 'b')
20
+ )
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RGB
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
+ Input = Kind.object(name: 'Integer(>= 0 and <= 255)') do |value|
12
+ value.is_a?(::Integer) && value >= 0 && value <= 255
13
+ end
14
+
15
+ def self.new(value, label:)
16
+ __new__(value: Input[value, label: label])
17
+ end
18
+ end
19
+ end
data/examples/rgb_1.rb ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ source 'https://rubygems.org'
7
+
8
+ gem 'u-struct', path: '..'
9
+ gem 'kind'
10
+ end
11
+
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|
20
+ value.is_a?(::Integer) && value >= 0 && value <= 255
21
+ end
22
+
23
+ def self.new(r:, g:, b:)
24
+ __new__(
25
+ red: ColorNumber[r, label: 'r'],
26
+ green: ColorNumber[g, label: 'g'],
27
+ blue: ColorNumber[b, label: 'b']
28
+ )
29
+ end
30
+ end
31
+
32
+ rgb_color = RGBColor.new(r: 1, g: 1, b: 255)
33
+
34
+ p rgb_color
35
+
36
+ puts
37
+ puts format('to_hex: %p', rgb_color.to_hex)
38
+ puts format('to_a: %p', rgb_color.to_a)
39
+ puts
40
+
41
+ r, g, b = rgb_color
42
+
43
+ puts format('red: %p', r)
44
+ puts format('green: %p', g)
45
+ puts format('blue: %p', b)
46
+
47
+ RGBColor.new(r: 1, g: -1, b: 255) # Kind::Error (g: -1 expected to be a kind of Integer(>= 0 and <= 255))
data/examples/rgb_2.rb ADDED
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ source 'https://rubygems.org'
7
+
8
+ gem 'u-struct', path: '..'
9
+ gem 'kind'
10
+ end
11
+
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
+ Input = Kind.object(name: 'Integer(>= 0 and <= 255)') do |value|
20
+ value.is_a?(::Integer) && value >= 0 && value <= 255
21
+ end
22
+
23
+ def self.new(value, label:)
24
+ __new__(value: Input[value, label: label])
25
+ 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
+
33
+ def to_hex
34
+ "##{red}#{green}#{blue}"
35
+ end
36
+ end
37
+
38
+ module RGBColor
39
+ def self.new(r:, g:, b:)
40
+ __new__(
41
+ red: RGBNumber.new(r, label: 'r'),
42
+ green: RGBNumber.new(g, label: 'g'),
43
+ blue: RGBNumber.new(b, label: 'b')
44
+ )
45
+ end
46
+ end
47
+
48
+ rgb_color = RGBColor.new(r: 1, g: 1, b: 255)
49
+
50
+ p rgb_color
51
+
52
+ puts
53
+ puts format('to_hex: %p', rgb_color.to_hex)
54
+ puts format('to_a: %p', rgb_color.to_a)
55
+ puts
56
+
57
+ r, g, b = rgb_color
58
+
59
+ puts format('red: %p', r)
60
+ puts format('green: %p', g)
61
+ puts format('blue: %p', b)
62
+
63
+ RGB::Color.new(r: 1, g: -1, b: 255) # Kind::Error (g: -1 expected to be a kind of Integer(>= 0 and <= 255))
data/examples/rgb_3.rb ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/inline'
4
+
5
+ gemfile do
6
+ source 'https://rubygems.org'
7
+
8
+ gem 'u-struct', path: '..'
9
+ gem 'kind'
10
+ end
11
+
12
+ require_relative 'rgb/number'
13
+ require_relative 'rgb/color'
14
+
15
+ rgb_color = RGB::Color.new(r: 1, g: 1, b: 255)
16
+
17
+ p rgb_color
18
+
19
+ puts
20
+ puts format('to_hex: %p', rgb_color.to_hex)
21
+ puts format('to_a: %p', rgb_color.to_a)
22
+ puts
23
+
24
+ r, g, b = rgb_color
25
+
26
+ puts format('red: %p', r)
27
+ puts format('green: %p', g)
28
+ puts format('blue: %p', b)
29
+
30
+ RGB::Color.new(r: 1, g: -1, b: 255) # Kind::Error (g: -1 expected to be a kind of Integer(>= 0 and <= 255))
@@ -10,7 +10,7 @@ class Micro::Struct::Creator
10
10
  def_initialize(container, required_members, optional_members)
11
11
  def_triple_eq(container)
12
12
  def_members(container)
13
- def_to_proc(container, features)
13
+ def_to_proc(container) if features[:to_proc]
14
14
 
15
15
  container
16
16
  end
@@ -28,35 +28,37 @@ class Micro::Struct::Creator
28
28
  # We are doing this because the Struct constructor keyword init option treats everything as optional.
29
29
  #
30
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
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
34
38
  RUBY
35
39
  end
36
40
 
37
41
  def def_triple_eq(container)
38
42
  container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
39
- def self.===(other)
40
- Struct === other
41
- end
43
+ def self.===(other)
44
+ Struct === other
45
+ end
42
46
  RUBY
43
47
  end
44
48
 
45
49
  def def_members(container)
46
50
  container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
47
- def self.members
48
- Struct.members
49
- end
51
+ def self.members
52
+ Struct.members
53
+ end
50
54
  RUBY
51
55
  end
52
56
 
53
- def def_to_proc(container, features)
54
- return unless features[:to_proc]
55
-
57
+ def def_to_proc(container)
56
58
  container.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
57
- def self.to_proc
58
- ->(hash) { new(**hash) }
59
- end
59
+ def self.to_proc
60
+ ->(hash) { new(**hash) }
61
+ end
60
62
  RUBY
61
63
  end
62
64
  end
@@ -11,7 +11,7 @@ class Micro::Struct::Creator
11
11
  def_to_ary(struct) if features[:to_ary]
12
12
  def_to_hash(struct) if features[:to_hash]
13
13
  def_readonly(struct) if features[:readonly]
14
- def_instance_copy(struct) if features[:readonly] || features[:instance_copy]
14
+ def_instance_copy(struct) if features[:instance_copy]
15
15
 
16
16
  struct
17
17
  end
@@ -19,11 +19,19 @@ class Micro::Struct::Creator
19
19
  private
20
20
 
21
21
  def def_to_ary(struct)
22
- struct.send(:alias_method, :to_ary, :to_a)
22
+ struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
23
+ def to_ary
24
+ to_a
25
+ end
26
+ RUBY
23
27
  end
24
28
 
25
29
  def def_to_hash(struct)
26
- struct.send(:alias_method, :to_hash, :to_h)
30
+ struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
31
+ def to_hash
32
+ to_h
33
+ end
34
+ RUBY
27
35
  end
28
36
 
29
37
  def def_readonly(struct)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Struct
5
- VERSION = '0.8.0'
5
+ VERSION = '0.9.0'
6
6
  end
7
7
  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.8.0
4
+ version: 0.9.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-05 00:00:00.000000000 Z
11
+ date: 2021-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,6 +55,13 @@ files:
55
55
  - Rakefile
56
56
  - bin/console
57
57
  - bin/setup
58
+ - examples/person_1.rb
59
+ - examples/person_2.rb
60
+ - examples/rgb/color.rb
61
+ - examples/rgb/number.rb
62
+ - examples/rgb_1.rb
63
+ - examples/rgb_2.rb
64
+ - examples/rgb_3.rb
58
65
  - lib/micro/struct.rb
59
66
  - lib/micro/struct/creator.rb
60
67
  - lib/micro/struct/creator/create_module.rb