uinit-structure 0.1.0 → 0.1.2
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 +4 -4
- data/README.md +8 -1
- data/lib/uinit/structure/attribute_builder.rb +11 -4
- data/lib/uinit/structure/attribute_context.rb +14 -4
- data/lib/uinit/structure/compilers/as_json.rb +1 -1
- data/lib/uinit/structure/compilers/attribute.rb +1 -2
- data/lib/uinit/structure/compilers/constructor.rb +1 -1
- data/lib/uinit/structure/version.rb +1 -1
- data/lib/uinit/structure.rb +21 -9
- data/uinit-structure.gemspec +2 -5
- metadata +11 -40
- data/lib/uinit/structure/compilers.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0503a10252f0c6e44bb61a6853d5bc59380dc0b6568e50778d14ba28b0ad0616
|
4
|
+
data.tar.gz: b3235c878479a28a98d09859bddafbf011f92596c29c59a67f2862e638bb8a44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 710a40c77b700bbd2a6a41a3dab9a6144677687f67fa255ac7b8afcdb44a024fb7439e729bc985e2b199f65ac6e8537862ef504669485d05e741e553ad25cf64
|
7
|
+
data.tar.gz: 1638b06baa9a4929e9bdca10b26056469ec8ad2edcee5e7af21d367cd27834a9986aa4b97dd5329ec73ab3e2628b027e8816505308c5e7e6b892231731724f92
|
data/README.md
CHANGED
@@ -3,11 +3,18 @@
|
|
3
3
|
module Uinit
|
4
4
|
module Structure
|
5
5
|
class AttributeBuilder
|
6
|
-
def initialize(attribute = nil)
|
7
|
-
|
6
|
+
def initialize(defaults, attribute = nil)
|
7
|
+
self.attribute = attribute || Attribute.new
|
8
|
+
self.defaults = defaults
|
9
|
+
|
10
|
+
return if attribute
|
11
|
+
|
12
|
+
defaults.each do |(name, value)|
|
13
|
+
self.attribute.send(:"#{name}=", value)
|
14
|
+
end
|
8
15
|
end
|
9
16
|
|
10
|
-
attr_accessor :attribute
|
17
|
+
attr_accessor :attribute, :defaults
|
11
18
|
|
12
19
|
def attr(name, type = nil, default = Attribute::UNDEFINED, &)
|
13
20
|
return build_mutliple(name, type, default, &) if name.is_a?(Array)
|
@@ -103,7 +110,7 @@ module Uinit
|
|
103
110
|
|
104
111
|
def build_mutliple(name, type, default, &)
|
105
112
|
name.map do |nm|
|
106
|
-
builder = AttributeBuilder.new(attribute.clone)
|
113
|
+
builder = AttributeBuilder.new(defaults, attribute.clone)
|
107
114
|
builder.attr(nm, type, default, &)
|
108
115
|
end
|
109
116
|
end
|
@@ -15,29 +15,39 @@ module Uinit
|
|
15
15
|
|
16
16
|
memo def attributes = []
|
17
17
|
|
18
|
+
def defaults(**defaults)
|
19
|
+
self.attribute_defaults = defaults
|
20
|
+
end
|
21
|
+
|
18
22
|
def private(att_or_get_set = nil, *)
|
19
23
|
return att_or_getset.private(:get, :set) if att_or_get_set.is_a?(AttributeBuilder)
|
20
24
|
|
21
|
-
builder =
|
25
|
+
builder = build_attribute_builder.private(*[att_or_get_set, *].compact)
|
22
26
|
attributes << builder.attribute
|
23
27
|
|
24
28
|
builder
|
25
29
|
end
|
26
30
|
|
27
31
|
def attr(...)
|
28
|
-
push_attribute(
|
32
|
+
push_attribute(build_attribute_builder.attr(...))
|
29
33
|
end
|
30
34
|
|
31
35
|
def abstract(...)
|
32
|
-
push_attribute(
|
36
|
+
push_attribute(build_attribute_builder.abstract(...))
|
33
37
|
end
|
34
38
|
|
35
39
|
def using(attribute_builder)
|
36
|
-
push_attribute(
|
40
|
+
push_attribute(build_attribute_builder(attribute_builder.attribute))
|
37
41
|
end
|
38
42
|
|
39
43
|
private
|
40
44
|
|
45
|
+
attr_accessor :attribute_defaults
|
46
|
+
|
47
|
+
def build_attribute_builder(attribute = nil)
|
48
|
+
AttributeBuilder.new(attribute_defaults || {}, attribute)
|
49
|
+
end
|
50
|
+
|
41
51
|
def push_attribute(builder)
|
42
52
|
if builder.is_a?(Array)
|
43
53
|
attributes.push(*builder.map(&:attribute))
|
@@ -86,7 +86,6 @@ module Uinit
|
|
86
86
|
compile_optional_check(<<~RUBY)
|
87
87
|
unless value.is_a?(#{lookup(:struct)})
|
88
88
|
unless value.is_a?(Hash)
|
89
|
-
binding.pry
|
90
89
|
raise AttributeError.new(#{lookup}, "no implicit conversion of \#{ value.class } into Hash")
|
91
90
|
end
|
92
91
|
|
@@ -181,7 +180,7 @@ module Uinit
|
|
181
180
|
end
|
182
181
|
|
183
182
|
def lookup(att = nil)
|
184
|
-
"
|
183
|
+
"get_structure_schema.#{attribute.name}#{att ? ".#{att}" : ''}"
|
185
184
|
end
|
186
185
|
end
|
187
186
|
# rubocop:enable Metrics/ClassLength
|
@@ -75,7 +75,7 @@ module Uinit
|
|
75
75
|
|
76
76
|
def compile_default_attribute(name, attribute)
|
77
77
|
<<~RUBY
|
78
|
-
self.#{name} =
|
78
|
+
self.#{name} = get_structure_schema.#{name}.default#{attribute.default.is_a?(Proc) ? '.call' : ''}
|
79
79
|
RUBY
|
80
80
|
end
|
81
81
|
end
|
data/lib/uinit/structure.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'zeitwerk'
|
4
4
|
|
5
|
+
require 'active_support'
|
6
|
+
|
5
7
|
require 'uinit/type'
|
6
8
|
require 'uinit/memoizable'
|
7
9
|
|
@@ -14,17 +16,16 @@ end
|
|
14
16
|
|
15
17
|
module Uinit
|
16
18
|
module Structure
|
19
|
+
extend ActiveSupport::Concern
|
17
20
|
include Memoizable
|
18
21
|
|
19
|
-
def self.included(base)
|
20
|
-
base.extend(ClassMethods)
|
21
|
-
end
|
22
|
-
|
23
22
|
class Schema; end
|
24
23
|
|
25
|
-
|
24
|
+
class_methods do
|
26
25
|
include Memoizable
|
27
26
|
|
27
|
+
attr_reader :attributes
|
28
|
+
|
28
29
|
memo def structure_schema
|
29
30
|
if respond_to?(:superclass) && superclass.respond_to?(:structure_schema)
|
30
31
|
return Class.new(superclass.structure_schema.class).new
|
@@ -42,7 +43,9 @@ module Uinit
|
|
42
43
|
end
|
43
44
|
|
44
45
|
def struct(&)
|
45
|
-
AttributeContext.scope(&)
|
46
|
+
attributes = AttributeContext.scope(&)
|
47
|
+
|
48
|
+
attributes.each do |attribute|
|
46
49
|
raise NameError, 'Attribute must have a name' unless attribute.name
|
47
50
|
|
48
51
|
structure_schema.class.define_method(attribute.name) { attribute }
|
@@ -51,11 +54,20 @@ module Uinit
|
|
51
54
|
|
52
55
|
Compilers::Constructor.compile(structure_module, structure_schema)
|
53
56
|
Compilers::AsJson.compile(structure_module, structure_schema)
|
57
|
+
|
58
|
+
sup_attributes = superclass < Structure ? superclass.attributes.dup : {}
|
59
|
+
|
60
|
+
self.attributes = attributes.each_with_object(sup_attributes) do |attribute, hsh|
|
61
|
+
hsh[attribute.name] = attribute
|
62
|
+
end
|
54
63
|
end
|
55
|
-
end
|
56
64
|
|
57
|
-
|
65
|
+
private
|
66
|
+
|
67
|
+
attr_writer :attributes
|
68
|
+
end
|
58
69
|
|
59
|
-
memo def
|
70
|
+
memo def get_structure_schema = self.class.structure_schema
|
71
|
+
memo def get_attributes = self.class.attributes
|
60
72
|
end
|
61
73
|
end
|
data/uinit-structure.gemspec
CHANGED
@@ -26,13 +26,10 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.metadata['changelog_uri'] = 'https://github.com/Kimoja/uinit-structure/blob/main/CHANGELOG.md'
|
27
27
|
spec.metadata['bug_tracker_uri'] = 'https://github.com/Kimoja/uinit-structure/issues'
|
28
28
|
|
29
|
-
spec.
|
30
|
-
|
29
|
+
spec.add_dependency 'activesupport', '~> 7.1.3'
|
31
30
|
spec.add_dependency 'uinit-memoizable', '~> 0.1.0'
|
32
31
|
spec.add_dependency 'uinit-type', '~> 0.1.0'
|
32
|
+
spec.add_dependency 'zeitwerk', '~> 2.6'
|
33
33
|
|
34
|
-
spec.add_development_dependency 'bundler'
|
35
|
-
spec.add_development_dependency 'rspec'
|
36
|
-
spec.add_development_dependency 'rubocop'
|
37
34
|
spec.metadata['rubygems_mfa_required'] = 'true'
|
38
35
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uinit-structure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kimoja
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 7.1.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 7.1.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: uinit-memoizable
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,47 +53,19 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 0.1.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rspec
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: rubocop
|
56
|
+
name: zeitwerk
|
85
57
|
requirement: !ruby/object:Gem::Requirement
|
86
58
|
requirements:
|
87
|
-
- - "
|
59
|
+
- - "~>"
|
88
60
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
90
|
-
type: :
|
61
|
+
version: '2.6'
|
62
|
+
type: :runtime
|
91
63
|
prerelease: false
|
92
64
|
version_requirements: !ruby/object:Gem::Requirement
|
93
65
|
requirements:
|
94
|
-
- - "
|
66
|
+
- - "~>"
|
95
67
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
68
|
+
version: '2.6'
|
97
69
|
description: Typed structure
|
98
70
|
email:
|
99
71
|
- joakim.carrilho@cheerz.com
|
@@ -110,7 +82,6 @@ files:
|
|
110
82
|
- lib/uinit/structure/attribute_context.rb
|
111
83
|
- lib/uinit/structure/attribute_error.rb
|
112
84
|
- lib/uinit/structure/attribute_type_error.rb
|
113
|
-
- lib/uinit/structure/compilers.rb
|
114
85
|
- lib/uinit/structure/compilers/as_json.rb
|
115
86
|
- lib/uinit/structure/compilers/attribute.rb
|
116
87
|
- lib/uinit/structure/compilers/base.rb
|