nina 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +12 -0
- data/lib/nina/builder/callbacks.rb +24 -0
- data/lib/nina/builder/initialization.rb +29 -0
- data/lib/nina/builder.rb +17 -46
- data/lib/nina/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28d86e0716c73ae59b0b6ac85076436ae54d1856d072e11114c576cd0b66365e
|
4
|
+
data.tar.gz: 255f6b2b4ac6b2a6f56f0cbea4fdf38e991494c855cd373417ec99f95af174ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 745f85388243699c7a2143c8849039b46aad9e343bf3ec005d3f04025d21d4860a7e43969ce06a3ab0357d865bfc4a4efd9fc14c550f551b4aa455a0aa33ab38
|
7
|
+
data.tar.gz: 6e1a5d9e21e28c3667a56e96b85c1e7c2e6a304ea377d607f1bc1b4b161af4a1e314f4dc28752c2ccc31e982b6cb9b3b0dbf077573dc02c4cd34376baaf4ef75
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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
|
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
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.
|
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-
|
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:
|