uinit-structure 0.1.0 → 0.1.2

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: 3c8c42aab899fa0b64aa674b8096c9b1ed6be23e39643fd63d06f168876a18cd
4
- data.tar.gz: 3499d5ba022a96ab76bf9aca62313787388271343caa2dbf5760997a972ebcc9
3
+ metadata.gz: 0503a10252f0c6e44bb61a6853d5bc59380dc0b6568e50778d14ba28b0ad0616
4
+ data.tar.gz: b3235c878479a28a98d09859bddafbf011f92596c29c59a67f2862e638bb8a44
5
5
  SHA512:
6
- metadata.gz: ca55bfc366a4b15b90c6306f7b5b724988e341ade9f2feaa38aa58c86335e4a89b214ce014e19af4e73d3d768c06a202f8a7e98345a011e8ac503c56f95a367b
7
- data.tar.gz: 5c15f70a14a1b8ba09ab08302322e5bdb37c634110dfae0b27d77039ce13cd630d6a61ab1c5efb82e8e8ad6a8b9d86f8efbee83f64ea54e7103f53df9be31064
6
+ metadata.gz: 710a40c77b700bbd2a6a41a3dab9a6144677687f67fa255ac7b8afcdb44a024fb7439e729bc985e2b199f65ac6e8537862ef504669485d05e741e553ad25cf64
7
+ data.tar.gz: 1638b06baa9a4929e9bdca10b26056469ec8ad2edcee5e7af21d367cd27834a9986aa4b97dd5329ec73ab3e2628b027e8816505308c5e7e6b892231731724f92
data/README.md CHANGED
@@ -1,3 +1,10 @@
1
1
  # Uinit::Structure
2
2
 
3
- Typed structure
3
+ Typed structure
4
+
5
+ # TODO
6
+ - Predicate: don't use ActiveSupport
7
+ https://apidock.com/rails/Object/blank%3F
8
+ `respond_to?(:empty?) ? !!empty? : !self`
9
+
10
+
@@ -3,11 +3,18 @@
3
3
  module Uinit
4
4
  module Structure
5
5
  class AttributeBuilder
6
- def initialize(attribute = nil)
7
- @attribute = attribute || Attribute.new
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 = AttributeBuilder.new.private(*[att_or_get_set, *].compact)
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(AttributeBuilder.new.attr(...))
32
+ push_attribute(build_attribute_builder.attr(...))
29
33
  end
30
34
 
31
35
  def abstract(...)
32
- push_attribute(AttributeBuilder.new.abstract(...))
36
+ push_attribute(build_attribute_builder.abstract(...))
33
37
  end
34
38
 
35
39
  def using(attribute_builder)
36
- push_attribute(AttributeBuilder.new(attribute_builder.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))
@@ -87,7 +87,7 @@ module Uinit
87
87
 
88
88
  def compile_json_proc(name)
89
89
  <<~RUBY
90
- _structure_schema.#{name}.as_json.call(json, self.#{name}, :#{name})
90
+ get_structure_schema.#{name}.as_json.call(json, self.#{name}, :#{name})
91
91
  RUBY
92
92
  end
93
93
  end
@@ -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
- "_structure_schema.#{attribute.name}#{att ? ".#{att}" : ''}"
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} = _structure_schema.#{name}.default#{attribute.default.is_a?(Proc) ? '.call' : ''}
78
+ self.#{name} = get_structure_schema.#{name}.default#{attribute.default.is_a?(Proc) ? '.call' : ''}
79
79
  RUBY
80
80
  end
81
81
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Uinit
4
4
  module Structure
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.2'
6
6
  end
7
7
  end
@@ -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
- module ClassMethods
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(&).each do |attribute|
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
- private
65
+ private
66
+
67
+ attr_writer :attributes
68
+ end
58
69
 
59
- memo def _structure_schema = self.class.structure_schema
70
+ memo def get_structure_schema = self.class.structure_schema
71
+ memo def get_attributes = self.class.attributes
60
72
  end
61
73
  end
@@ -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.add_runtime_dependency 'zeitwerk', '~> 2.6'
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.0
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-05-08 00:00:00.000000000 Z
11
+ date: 2024-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: zeitwerk
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.6'
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: '2.6'
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: bundler
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: '0'
90
- type: :development
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: '0'
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
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Uinit
4
- module Structure
5
- module Compilers
6
- end
7
- end
8
- end