uinit-structure 0.1.0 → 0.1.1

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: 3c8c42aab899fa0b64aa674b8096c9b1ed6be23e39643fd63d06f168876a18cd
4
- data.tar.gz: 3499d5ba022a96ab76bf9aca62313787388271343caa2dbf5760997a972ebcc9
3
+ metadata.gz: a7a743c0abc2104544f146a736c252aa06fd453bc6efbc6b24badbcd637a4e19
4
+ data.tar.gz: 296f8f382e991c6ea709732ae344d654d96a063855296f16c180514e4085c3a1
5
5
  SHA512:
6
- metadata.gz: ca55bfc366a4b15b90c6306f7b5b724988e341ade9f2feaa38aa58c86335e4a89b214ce014e19af4e73d3d768c06a202f8a7e98345a011e8ac503c56f95a367b
7
- data.tar.gz: 5c15f70a14a1b8ba09ab08302322e5bdb37c634110dfae0b27d77039ce13cd630d6a61ab1c5efb82e8e8ad6a8b9d86f8efbee83f64ea54e7103f53df9be31064
6
+ metadata.gz: 07a92cf903e3528bd04e476d0df7351e2d4987d1184a77ea43d7c4eabc4439ba9a06a2919ec201bb3cddc314f279f5cda09b7b25893c3b06d7713c0e07c22841
7
+ data.tar.gz: 3ee54c822deef14f1632acb740d5ef1c686cf99e49c12dcec94377915036688b570ba94d1c3a1b1946dc176e3b1ee74e9d9a149f634cdfaf596541289e338ac9
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))
@@ -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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Uinit
4
4
  module Structure
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
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.1
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
14
  name: zeitwerk