nina 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59b40ebfbf0d151bfa494c0656ee33725b11e3ce6bb3934f20ee84d62cdec19c
4
- data.tar.gz: a821c8aa025985bbfe3036e4a091cf29c4007939e32f3ba183379c0f9902bf59
3
+ metadata.gz: 28d86e0716c73ae59b0b6ac85076436ae54d1856d072e11114c576cd0b66365e
4
+ data.tar.gz: 255f6b2b4ac6b2a6f56f0cbea4fdf38e991494c855cd373417ec99f95af174ac
5
5
  SHA512:
6
- metadata.gz: 2441cf19ef6c542fc567384f475f6c21956de6a89dd1dd100f3b993c92e463034527235a3a599088c759c7f32de3ece32d38822a4bb45afbb87087080d81a865
7
- data.tar.gz: c5b7297094aab27bac3bae21f438360ee63b2314c875179c21bdede94ba7b0fece93633706d46e34c040a9346b5efb64577412ea7d90dec4a33e4777f46bbdc5
6
+ metadata.gz: 745f85388243699c7a2143c8849039b46aad9e343bf3ec005d3f04025d21d4860a7e43969ce06a3ab0357d865bfc4a4efd9fc14c550f551b4aa455a0aa33ab38
7
+ data.tar.gz: 6e1a5d9e21e28c3667a56e96b85c1e7c2e6a304ea377d607f1bc1b4b161af4a1e314f4dc28752c2ccc31e982b6cb9b3b0dbf077573dc02c4cd34376baaf4ef75
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2023-07-12
4
+
5
+ ### Improved
6
+ * Builder#with_callbacks will copy callbacks from previous calls.
7
+
3
8
  ## [0.1.1] - 2023-07-05
4
9
 
5
10
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nina (0.1.1)
4
+ nina (0.1.2)
5
5
  toritori (= 0.1.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -111,6 +111,18 @@ instance.query.b # => 2
111
111
  instance.c # => nil
112
112
  ```
113
113
 
114
+ We are copying callbacks from builder if had some defined
115
+ ```ruby
116
+ builder_with_callbacks = builder.with_callbacks do |c|
117
+ c.params { _1.a = 1 }
118
+ end
119
+ builder_with_callbacks_with_callbacks = builder_with_callbacks.with_callbacks do |c|
120
+ c.params { _1.a += 1 }
121
+ end
122
+ instance = builder_with_callbacks.wrap
123
+ instance.a # => 2
124
+ ```
125
+
114
126
  ## Development
115
127
 
116
128
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nina
4
+ class Builder
5
+ # Utility to get user defined callbacks
6
+ class Callbacks < Initialization
7
+ def copy
8
+ Callbacks.new(@allow_list, to_h.dup)
9
+ end
10
+
11
+ def method_missing(method, *args, **kwargs, &block)
12
+ return super unless @allow_list.include?(method)
13
+
14
+ @atts[method] unless block
15
+ @atts[method] ||= []
16
+ @atts[method] << block
17
+ end
18
+
19
+ def respond_to_missing?(method, include_private = false)
20
+ super
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nina
4
+ class Builder
5
+ # A way to call methods from initalization proc on base_class
6
+ class Initialization < BasicObject
7
+ attr_reader :allow_list
8
+
9
+ def initialize(allow_list, atts = {})
10
+ @allow_list = allow_list
11
+ @atts = atts
12
+ end
13
+
14
+ def method_missing(method, *args, **kwargs, &block)
15
+ return super unless @allow_list.include?(method)
16
+
17
+ @atts[method] = [args, kwargs, block]
18
+ end
19
+
20
+ def respond_to_missing?(method, include_private = false)
21
+ @allow_list.include?(method) || super
22
+ end
23
+
24
+ def to_h
25
+ @atts
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/nina/builder.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'nina/builder/initialization'
4
+ require 'nina/builder/callbacks'
5
+
3
6
  # This should be a kind of factory that creates complex objects
4
7
  # from simple ones. It should use torirori to create objects.
5
8
  # It also enriches objects with some methods that make them more
@@ -7,41 +10,7 @@
7
10
  module Nina
8
11
  # Generates module that adds support for objects creation
9
12
  class Builder
10
- attr_reader :name, :abstract_factory, :def_block
11
- attr_accessor :callbacks
12
-
13
- # A way to call methods from initalization proc on base_class
14
- class Initialization < BasicObject
15
- def initialize(list)
16
- @list = list
17
- @atts = {}
18
- end
19
-
20
- def method_missing(method, *args, **kwargs, &block)
21
- return super unless @list.include?(method)
22
-
23
- @atts[method] = [args, kwargs, block]
24
- end
25
-
26
- def respond_to_missing?(method, include_private = false)
27
- @list.include?(method) || super
28
- end
29
-
30
- def to_h
31
- @atts
32
- end
33
- end
34
-
35
- # Utility to get user defined callbacks
36
- class Callbacks < Initialization
37
- def method_missing(method, *args, **kwargs, &block)
38
- return super unless @list.include?(method)
39
-
40
- @atts[method] unless block
41
- @atts[method] ||= []
42
- @atts[method] << block
43
- end
44
- end
13
+ attr_reader :name, :abstract_factory, :def_block, :callbacks
45
14
 
46
15
  # Definaes support methods and variables for concrete builder
47
16
  module ClassMethods
@@ -68,17 +37,7 @@ module Nina
68
37
  end
69
38
  end
70
39
 
71
- def copy
72
- self.class.new(name, abstract_factory: abstract_factory)
73
- end
74
-
75
- def with_callbacks(&block)
76
- yield c = Callbacks.new(abstract_factory.factories.keys) if block
77
-
78
- self.class.new(name, abstract_factory: abstract_factory).tap { _1.callbacks = c }
79
- end
80
-
81
- def initialize(name, abstract_factory: nil, &def_block)
40
+ def initialize(name, abstract_factory: nil, callbacks: nil, &def_block)
82
41
  @name = name
83
42
  @def_block = def_block
84
43
  @abstract_factory = abstract_factory.include(Toritori).extend(ClassMethods)
@@ -86,6 +45,18 @@ module Nina
86
45
  @abstract_factory.build_order_list.freeze
87
46
  @initialization = Initialization.new(@abstract_factory.factories.keys)
88
47
  @assembler = Assembler.new(@abstract_factory)
48
+ @callbacks = callbacks
49
+ end
50
+
51
+ def copy
52
+ self.class.new(name, abstract_factory: abstract_factory)
53
+ end
54
+
55
+ def with_callbacks(&block)
56
+ c = callbacks&.copy || Callbacks.new(abstract_factory.factories.keys)
57
+ yield c if block
58
+
59
+ self.class.new(name, abstract_factory: abstract_factory, callbacks: c)
89
60
  end
90
61
 
91
62
  def wrap(delegate: false, &block)
data/lib/nina/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nina
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nina
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-05 00:00:00.000000000 Z
11
+ date: 2023-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: toritori
@@ -43,6 +43,8 @@ files:
43
43
  - lib/nina.rb
44
44
  - lib/nina/assembler.rb
45
45
  - lib/nina/builder.rb
46
+ - lib/nina/builder/callbacks.rb
47
+ - lib/nina/builder/initialization.rb
46
48
  - lib/nina/version.rb
47
49
  homepage: https://github.com/andriy-baran/nina
48
50
  licenses: