nina 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c948dd7e2dadd58475f4153f5dd7162912d41706e4fbec08118470d69f8783db
4
- data.tar.gz: 9ad7e8f6dd835220d21d2afe5120bfcea1cf5dcd13241f531f14d3fcb448edaf
3
+ metadata.gz: 28d86e0716c73ae59b0b6ac85076436ae54d1856d072e11114c576cd0b66365e
4
+ data.tar.gz: 255f6b2b4ac6b2a6f56f0cbea4fdf38e991494c855cd373417ec99f95af174ac
5
5
  SHA512:
6
- metadata.gz: b24e8b48fa8744df3395b5f936cacd723c97285d5acc2b9f0e5d5edb5fac6e86caad86a8ef20968bd53210f37f663d56520c2a2becaeda38d92b95162aa2112b
7
- data.tar.gz: 64eac812b99e18afa87ce146d0eb2cf77ad08efba781303b397b70d6edb320fa66856879635af1d98ae6cd86d9970790e1b419b0d08c190e330336ea0f27cb24
6
+ metadata.gz: 745f85388243699c7a2143c8849039b46aad9e343bf3ec005d3f04025d21d4860a7e43969ce06a3ab0357d865bfc4a4efd9fc14c550f551b4aa455a0aa33ab38
7
+ data.tar.gz: 6e1a5d9e21e28c3667a56e96b85c1e7c2e6a304ea377d607f1bc1b4b161af4a1e314f4dc28752c2ccc31e982b6cb9b3b0dbf077573dc02c4cd34376baaf4ef75
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
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
+
8
+ ## [0.1.1] - 2023-07-05
9
+
10
+ ### Added
11
+ * Support for multiple callbacks
12
+
3
13
  ## [0.1.0] - 2023-05-18
4
14
 
5
15
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nina (0.1.0)
4
+ nina (0.1.2)
5
5
  toritori (= 0.1.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Nina
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/nina.svg)](https://badge.fury.io/rb/nina)
3
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/435ee6e0ae846e9deb88/maintainability)](https://codeclimate.com/github/andriy-baran/nina/maintainability)
4
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/435ee6e0ae846e9deb88/test_coverage)](https://codeclimate.com/github/andriy-baran/nina/test_coverage)
5
6
 
@@ -83,6 +84,7 @@ instance = builder.nest(delegate: true)
83
84
  instance.a # => nil
84
85
  instance.b # => nil
85
86
  instance.c # => nil
87
+ instance.query.c # => nil
86
88
  ```
87
89
  If you need provide an initalization parameters for the objects
88
90
  ```ruby
@@ -94,18 +96,31 @@ end
94
96
  instance.a # => 1
95
97
  instance.b # => 2
96
98
  instance.c # => 3
97
- instance.query.c # => 3
98
99
  ```
99
100
  To do something between stages (after creation of object)
100
101
  ```ruby
101
- instance = builder.wrap(delegate: true) do |b|
102
- b.params { _1.a = 'a' }
103
- b.query { _1.b = 'b' }
104
- b.command { _1.c = 'c' }
102
+ builder_with_callbacks = builder.with_callbacks do |c|
103
+ c.params { _1.a = 1 }
104
+ c.params { _1.a += 3 }
105
+ c.params { _1.a += 2 }
106
+ c.query { _1.b = 2 }
107
+ end
108
+ instance = builder_with_callbacks.wrap
109
+ instance.query.params.a # => 6
110
+ instance.query.b # => 2
111
+ instance.c # => nil
112
+ ```
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 }
105
121
  end
106
- instance.a # => 'a'
107
- instance.b # => 'b'
108
- instance.c # => 'c'
122
+ instance = builder_with_callbacks.wrap
123
+ instance.a # => 2
109
124
  ```
110
125
 
111
126
  ## Development
@@ -3,8 +3,6 @@
3
3
  module Nina
4
4
  # Generates module that adds support for objects creation
5
5
  class Assembler
6
- NOOP_PROC = proc {}
7
-
8
6
  # Adds ability to delegeate methods via method_missing
9
7
  module MethodMissingDelegation
10
8
  def method_missing(name, *attrs, &block)
@@ -34,7 +32,7 @@ module Nina
34
32
  build_order.each.with_index(-1).inject(nil) do |prev, (name, idx)|
35
33
  object = create_object(name, initialization)
36
34
  self.class.def_accessor(build_order[idx], on: object, to: prev, delegate: delegate) if prev
37
- callbacks.to_h.fetch(name, NOOP_PROC)[object] if callbacks
35
+ callbacks.to_h.fetch(name, []).each { |c| c.call(object) } if callbacks
38
36
  object
39
37
  end
40
38
  end
@@ -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,40 +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] = block
42
- end
43
- end
13
+ attr_reader :name, :abstract_factory, :def_block, :callbacks
44
14
 
45
15
  # Definaes support methods and variables for concrete builder
46
16
  module ClassMethods
@@ -67,17 +37,7 @@ module Nina
67
37
  end
68
38
  end
69
39
 
70
- def copy
71
- self.class.new(name, abstract_factory: abstract_factory)
72
- end
73
-
74
- def with_callbacks(&block)
75
- yield c = Callbacks.new(abstract_factory.factories.keys) if block
76
-
77
- self.class.new(name, abstract_factory: abstract_factory).tap { _1.callbacks = c }
78
- end
79
-
80
- def initialize(name, abstract_factory: nil, &def_block)
40
+ def initialize(name, abstract_factory: nil, callbacks: nil, &def_block)
81
41
  @name = name
82
42
  @def_block = def_block
83
43
  @abstract_factory = abstract_factory.include(Toritori).extend(ClassMethods)
@@ -85,6 +45,18 @@ module Nina
85
45
  @abstract_factory.build_order_list.freeze
86
46
  @initialization = Initialization.new(@abstract_factory.factories.keys)
87
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)
88
60
  end
89
61
 
90
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.0'
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.0
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-06-17 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: