smart_initializer 0.1.0.alpha3 → 0.1.0.alpha4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +58 -0
- data/lib/smart_core/initializer/attribute/init_extension.rb +4 -0
- data/lib/smart_core/initializer/attribute.rb +0 -1
- data/lib/smart_core/initializer/{attribute → constructor}/definer.rb +30 -1
- data/lib/smart_core/initializer/constructor.rb +14 -0
- data/lib/smart_core/initializer/dsl/inheritance.rb +1 -0
- data/lib/smart_core/initializer/dsl.rb +23 -4
- data/lib/smart_core/initializer/extensions/abstract.rb +8 -0
- data/lib/smart_core/initializer/extensions/ext_init.rb +31 -0
- data/lib/smart_core/initializer/extensions/list.rb +74 -0
- data/lib/smart_core/initializer/extensions.rb +9 -0
- data/lib/smart_core/initializer/version.rb +1 -1
- data/lib/smart_core/initializer.rb +1 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 045121a7aff669e936532780b634d3212d6063c051534bb465efc3a81fdcfb8c
|
4
|
+
data.tar.gz: f3aee4d1ada5fbb1dea05a39b7b81beb34096a75b41e641a15ddee7ea04e049a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ede12d393005f5c9c60fb33f063bbc8cd5ae12ecdb831f7b1219538b6e66a0adb8002fe9f0608e03fbc9831ac653d0a3c9e038c65d4a26cf7b1724608b105169
|
7
|
+
data.tar.gz: e4b976324cdbe52f4e2a9546ff621987c9536f1faf08468b68a2bc2e25cb2b98e2560a2d46f0dd76ec5559e60f577018993a3ffb35c5f742987fd3e88a79c125
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,8 +20,22 @@ require 'smart_core/types'
|
|
20
20
|
|
21
21
|
---
|
22
22
|
|
23
|
+
## Table of contents
|
24
|
+
|
25
|
+
- [Synopsis](#synopsis)
|
26
|
+
- [Type Aliasing](#type-aliasing)
|
27
|
+
- [Initialization extension](#initialization-extension)
|
28
|
+
|
29
|
+
---
|
30
|
+
|
23
31
|
## Synopsis
|
24
32
|
|
33
|
+
**Initialization flow**:
|
34
|
+
|
35
|
+
1. Parameter + Option definitioning;
|
36
|
+
2. Original #initialize invokation;
|
37
|
+
3. Initialization extensions invokation;
|
38
|
+
|
25
39
|
**Constructor definition**:
|
26
40
|
|
27
41
|
- `param` - defines name-like attribute:
|
@@ -78,6 +92,8 @@ User.new(1, 'John', 'test123', role: :admin, metadata: {}, enabled: false)
|
|
78
92
|
|
79
93
|
## Type aliasing
|
80
94
|
|
95
|
+
- Usage:
|
96
|
+
|
81
97
|
```ruby
|
82
98
|
# define your own type alias
|
83
99
|
SmartCore::Initializer.type_alias(:hash, SmartCore::Types::Value::Hash)
|
@@ -90,6 +106,48 @@ class User
|
|
90
106
|
end
|
91
107
|
```
|
92
108
|
|
109
|
+
- Predefined aliases:
|
110
|
+
- `:nil` => `SmartCore::Types::Value::Nil`
|
111
|
+
- `:string` => `SmartCore::Types::Value::String`
|
112
|
+
- `:symbol` => `SmartCore::Types::Value::Symbol`
|
113
|
+
- `:text` => `SmartCore::Types::Value::Text`
|
114
|
+
- `:integer` => `SmartCore::Types::Value::Integer`
|
115
|
+
- `:float` => `SmartCore::Types::Value::Float`
|
116
|
+
- `:numeric` => `SmartCore::Types::Value::Numeric`
|
117
|
+
- `:boolean` => `SmartCore::Types::Value::Boolean`
|
118
|
+
- `:array` => `SmartCore::Types::Value::Array`
|
119
|
+
- `:hash` => `SmartCore::Types::Value::Hash`
|
120
|
+
- `:proc` => `SmartCore::Types::Value::Proc`
|
121
|
+
- `:class` => `SmartCore::Types::Value::Class`
|
122
|
+
- `:module` => `SmartCore::Types::Value::Module`
|
123
|
+
- `:any` => `SmartCore::Types::Value::Any`
|
124
|
+
|
125
|
+
---
|
126
|
+
|
127
|
+
## Initialization extension
|
128
|
+
|
129
|
+
- `ext_init(&block)`:
|
130
|
+
- you can define as many extensions as you want;
|
131
|
+
- extensions are invoked in the order they are defined;
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
class User
|
135
|
+
include SmartCore::Initializer
|
136
|
+
|
137
|
+
option :name, :name
|
138
|
+
option :age, :integer
|
139
|
+
|
140
|
+
ext_init { |instance| instance.define_singleton_method(:extra) { :ext1 } }
|
141
|
+
ext_init { |instance| instance.define_singleton_method(:extra2) { :ext2 } }
|
142
|
+
end
|
143
|
+
|
144
|
+
user = User.new(name: 'keka', age: 123)
|
145
|
+
user.name # => 'keka'
|
146
|
+
user.age # => 123
|
147
|
+
user.extra # => :ext1
|
148
|
+
user.extra2 # => :ext2
|
149
|
+
```
|
150
|
+
|
93
151
|
---
|
94
152
|
|
95
153
|
## Contributing
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# @api private
|
4
4
|
# @since 0.1.0
|
5
|
-
class SmartCore::Initializer::
|
5
|
+
class SmartCore::Initializer::Constructor::Definer
|
6
6
|
# @param klass [Class]
|
7
7
|
# @return [void]
|
8
8
|
#
|
@@ -13,6 +13,17 @@ class SmartCore::Initializer::Attribute::Definer
|
|
13
13
|
@lock = SmartCore::Engine::Lock.new
|
14
14
|
end
|
15
15
|
|
16
|
+
# @param block [Proc]
|
17
|
+
# @return [void]
|
18
|
+
#
|
19
|
+
# @api private
|
20
|
+
# @since 0.1.0
|
21
|
+
def define_init_extension(block)
|
22
|
+
thread_safe do
|
23
|
+
add_init_extension(build_init_extension(block))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
16
27
|
# @param name [String, Symbol]
|
17
28
|
# @param type [String, Symbol, SmartCore::Types::Primitive]
|
18
29
|
# @param privacy [String, Symbol]
|
@@ -121,6 +132,15 @@ class SmartCore::Initializer::Attribute::Definer
|
|
121
132
|
)
|
122
133
|
end
|
123
134
|
|
135
|
+
# @param block [Proc]
|
136
|
+
# @return [SmartCore::Initializer::Extensions::ExtInit]
|
137
|
+
#
|
138
|
+
# @api private
|
139
|
+
# @since 0.1.0
|
140
|
+
def build_init_extension(block)
|
141
|
+
SmartCore::Initializer::Extensions::ExtInit.new(block)
|
142
|
+
end
|
143
|
+
|
124
144
|
# @param parameter [SmartCore::Initializer::Attribute]
|
125
145
|
# @return [void]
|
126
146
|
#
|
@@ -143,6 +163,15 @@ class SmartCore::Initializer::Attribute::Definer
|
|
143
163
|
klass.send(option.privacy, option.name)
|
144
164
|
end
|
145
165
|
|
166
|
+
# @param extension [SmartCore::Initializer::Extensions::ExtInit]
|
167
|
+
# @return [void]
|
168
|
+
#
|
169
|
+
# @api private
|
170
|
+
# @since 0.1.0
|
171
|
+
def add_init_extension(extension)
|
172
|
+
klass.__init_extensions__ << extension
|
173
|
+
end
|
174
|
+
|
146
175
|
# @param parameter [SmartCore::Initializer::Attribute]
|
147
176
|
# @return [void]
|
148
177
|
#
|
@@ -3,6 +3,8 @@
|
|
3
3
|
# @api private
|
4
4
|
# @since 0.1.0
|
5
5
|
class SmartCore::Initializer::Constructor
|
6
|
+
require_relative 'constructor/definer'
|
7
|
+
|
6
8
|
# @param klass [Class<SmartCore::Initializer>]
|
7
9
|
# @param arguments [Array<Any>]
|
8
10
|
# @param block [Proc, NilClass]
|
@@ -27,6 +29,7 @@ class SmartCore::Initializer::Constructor
|
|
27
29
|
initialize_parameters(instance)
|
28
30
|
initialize_options(instance)
|
29
31
|
process_original_initializer(instance)
|
32
|
+
process_init_extensions(instance)
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
@@ -143,6 +146,17 @@ class SmartCore::Initializer::Constructor
|
|
143
146
|
instance.send(:initialize, *arguments, &block)
|
144
147
|
end
|
145
148
|
|
149
|
+
# @param instance [Any]
|
150
|
+
# @return [void]
|
151
|
+
#
|
152
|
+
# @api private
|
153
|
+
# @since 0.1.0
|
154
|
+
def process_init_extensions(instance)
|
155
|
+
klass.__init_extensions__.each do |extension|
|
156
|
+
extension.call(instance)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
146
160
|
# @param arguments [Array<Any>]
|
147
161
|
# @return [Array<Array<Any>,Hash<Symbol,Any>>]
|
148
162
|
#
|
@@ -13,9 +13,10 @@ module SmartCore::Initializer::DSL
|
|
13
13
|
# @since 0.1.0
|
14
14
|
def extended(base_klass)
|
15
15
|
base_klass.instance_eval do
|
16
|
-
instance_variable_set(:@__params__,
|
16
|
+
instance_variable_set(:@__params__, SmartCore::Initializer::Attribute::List.new)
|
17
17
|
instance_variable_set(:@__options__, SmartCore::Initializer::Attribute::List.new)
|
18
|
-
instance_variable_set(:@
|
18
|
+
instance_variable_set(:@__init_extensions__, SmartCore::Initializer::Extensions::List.new)
|
19
|
+
instance_variable_set(:@__definer__, SmartCore::Initializer::Constructor::Definer.new(self))
|
19
20
|
instance_variable_set(:@__deflock__, SmartCore::Engine::Lock.new)
|
20
21
|
end
|
21
22
|
base_klass.extend(ClassMethods)
|
@@ -33,9 +34,10 @@ module SmartCore::Initializer::DSL
|
|
33
34
|
# @since 0.1.0
|
34
35
|
def inherited(child_klass)
|
35
36
|
child_klass.instance_eval do
|
36
|
-
instance_variable_set(:@__params__,
|
37
|
+
instance_variable_set(:@__params__, SmartCore::Initializer::Attribute::List.new)
|
37
38
|
instance_variable_set(:@__options__, SmartCore::Initializer::Attribute::List.new)
|
38
|
-
instance_variable_set(:@
|
39
|
+
instance_variable_set(:@__init_extensions__, SmartCore::Initializer::Extensions::List.new)
|
40
|
+
instance_variable_set(:@__definer__, SmartCore::Initializer::Constructor::Definer.new(self))
|
39
41
|
instance_variable_set(:@__deflock__, SmartCore::Engine::Lock.new)
|
40
42
|
end
|
41
43
|
child_klass.extend(ClassMethods)
|
@@ -63,6 +65,14 @@ module SmartCore::Initializer::DSL
|
|
63
65
|
@__options__
|
64
66
|
end
|
65
67
|
|
68
|
+
# @return [SmartCore::Initializer::Extentions::List]
|
69
|
+
#
|
70
|
+
# @api private
|
71
|
+
# @since 0.1.0
|
72
|
+
def __init_extensions__
|
73
|
+
@__init_extensions__
|
74
|
+
end
|
75
|
+
|
66
76
|
# @return [SmartCore::Initializer::Attribute::Definer]
|
67
77
|
#
|
68
78
|
# @api private
|
@@ -140,5 +150,14 @@ module SmartCore::Initializer::DSL
|
|
140
150
|
def options(*names)
|
141
151
|
__definer__.define_options(*names)
|
142
152
|
end
|
153
|
+
|
154
|
+
# @param block [Block]
|
155
|
+
# @return [void]
|
156
|
+
#
|
157
|
+
# @api public
|
158
|
+
# @since 0.1.0
|
159
|
+
def ext_init(&block)
|
160
|
+
__definer__.define_init_extension(block)
|
161
|
+
end
|
143
162
|
end
|
144
163
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.1.0
|
5
|
+
class SmartCore::Initializer::Extensions::ExtInit < SmartCore::Initializer::Extensions::Abstract
|
6
|
+
# @param extender [Proc]
|
7
|
+
# @return [void]
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
# @since 0.1.0
|
11
|
+
def initialize(extender)
|
12
|
+
@extender = extender
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param instance [Any]
|
16
|
+
# @return [void]
|
17
|
+
#
|
18
|
+
# @api private
|
19
|
+
# @since 0.1.0
|
20
|
+
def call(instance)
|
21
|
+
extender.call(instance)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# @return [Proc]
|
27
|
+
#
|
28
|
+
# @api private
|
29
|
+
# @since0 0.1.0
|
30
|
+
attr_reader :extender
|
31
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.1.0
|
5
|
+
class SmartCore::Initializer::Extensions::List
|
6
|
+
# @since 0.1.0
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
# @return [void]
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
# @since 0.1.0
|
13
|
+
def initialize
|
14
|
+
@extensions = []
|
15
|
+
@lock = SmartCore::Engine::Lock.new
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param extension [SmartCore::Initializer::Extensions::Abstract]
|
19
|
+
# @return [void]
|
20
|
+
#
|
21
|
+
# @api private
|
22
|
+
# @since 0.1.0
|
23
|
+
def add(extension)
|
24
|
+
thread_safe { extensions << extension }
|
25
|
+
end
|
26
|
+
alias_method :<<, :add
|
27
|
+
|
28
|
+
# @param list [SmartCore::Initializer::Extensions::List]
|
29
|
+
# @return [void]
|
30
|
+
#
|
31
|
+
# @api private
|
32
|
+
# @since 0.1.0
|
33
|
+
def concat(list)
|
34
|
+
thread_safe do
|
35
|
+
list.each { |extension| add(extension.dup) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @param block [Block]
|
40
|
+
# @return [Enumerable]
|
41
|
+
#
|
42
|
+
# @api private
|
43
|
+
# @since 0.1.0
|
44
|
+
def each(&block)
|
45
|
+
thread_safe do
|
46
|
+
block_given? ? extensions.each(&block) : extensions.each
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [Integer]
|
51
|
+
#
|
52
|
+
# @api private
|
53
|
+
# @since 0.1.0
|
54
|
+
def size
|
55
|
+
thread_safe { extensions.size }
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# @return [Array<SmartCore::Initializer::Extensions::Abstract>]
|
61
|
+
#
|
62
|
+
# @api private
|
63
|
+
# @since 0.1.0
|
64
|
+
attr_reader :extensions
|
65
|
+
|
66
|
+
# @param block [Block]
|
67
|
+
# @return [Any]
|
68
|
+
#
|
69
|
+
# @api private
|
70
|
+
# @since 0.1.0
|
71
|
+
def thread_safe(&block)
|
72
|
+
@lock.synchronize(&block)
|
73
|
+
end
|
74
|
+
end
|
@@ -10,6 +10,7 @@ module SmartCore::Initializer
|
|
10
10
|
require_relative 'initializer/version'
|
11
11
|
require_relative 'initializer/errors'
|
12
12
|
require_relative 'initializer/attribute'
|
13
|
+
require_relative 'initializer/extensions'
|
13
14
|
require_relative 'initializer/constructor'
|
14
15
|
require_relative 'initializer/dsl'
|
15
16
|
require_relative 'initializer/type_aliasing'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_initializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.alpha4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rustam Ibragimov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02-
|
11
|
+
date: 2020-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: smart_engine
|
@@ -130,18 +130,23 @@ files:
|
|
130
130
|
- bin/setup
|
131
131
|
- lib/smart_core/initializer.rb
|
132
132
|
- lib/smart_core/initializer/attribute.rb
|
133
|
-
- lib/smart_core/initializer/attribute/definer.rb
|
134
133
|
- lib/smart_core/initializer/attribute/factory.rb
|
135
134
|
- lib/smart_core/initializer/attribute/finalizer.rb
|
136
135
|
- lib/smart_core/initializer/attribute/finalizer/abstract.rb
|
137
136
|
- lib/smart_core/initializer/attribute/finalizer/anonymous_block.rb
|
138
137
|
- lib/smart_core/initializer/attribute/finalizer/instance_method.rb
|
138
|
+
- lib/smart_core/initializer/attribute/init_extension.rb
|
139
139
|
- lib/smart_core/initializer/attribute/list.rb
|
140
140
|
- lib/smart_core/initializer/attribute/parameters.rb
|
141
141
|
- lib/smart_core/initializer/constructor.rb
|
142
|
+
- lib/smart_core/initializer/constructor/definer.rb
|
142
143
|
- lib/smart_core/initializer/dsl.rb
|
143
144
|
- lib/smart_core/initializer/dsl/inheritance.rb
|
144
145
|
- lib/smart_core/initializer/errors.rb
|
146
|
+
- lib/smart_core/initializer/extensions.rb
|
147
|
+
- lib/smart_core/initializer/extensions/abstract.rb
|
148
|
+
- lib/smart_core/initializer/extensions/ext_init.rb
|
149
|
+
- lib/smart_core/initializer/extensions/list.rb
|
145
150
|
- lib/smart_core/initializer/type_aliasing.rb
|
146
151
|
- lib/smart_core/initializer/type_aliasing/alias_list.rb
|
147
152
|
- lib/smart_core/initializer/version.rb
|