smart_initializer 0.1.0.alpha2 → 0.1.0.alpha3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +44 -8
- data/lib/smart_core/initializer/constructor.rb +26 -16
- data/lib/smart_core/initializer/version.rb +1 -1
- data/lib/smart_core/initializer.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 483a56f55f7fb7b8e67eb20b1779df0b59f33935544d19cd6c5cd0e018e9b3b1
|
4
|
+
data.tar.gz: 9b1fc252e8821e68b22d38fe06e0418e5792860bc6e583caafd4ff5f786f509a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d77018f9b4be1a8f0c7befa771313d01debf97144e2bdc0d10248f357347eedeb6c0734997e6946fa0a35b810a6d7a1b59c395c8855f9e708a48021f0a94c9e7
|
7
|
+
data.tar.gz: 9cf8e06b7d6c89951330beff5698cca00502183ed625b06c3e620a6e6df0d654db4b55788d26a68b9da390217d9eb42966b609a773ebd5baa3e71bfa6d838f17
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,45 @@ require 'smart_core/types'
|
|
20
20
|
|
21
21
|
---
|
22
22
|
|
23
|
-
## Synopsis
|
23
|
+
## Synopsis
|
24
|
+
|
25
|
+
**Constructor definition**:
|
26
|
+
|
27
|
+
- `param` - defines name-like attribute:
|
28
|
+
- `cast` - type-cast received value if value has invalid type;
|
29
|
+
- `privacy` - reader incapsulation level;
|
30
|
+
- `finalize` - value post-processing (receives method name or proc);
|
31
|
+
- (limitation) param has no `:default` option;
|
32
|
+
- `option` - defined kwarg-like attribute:
|
33
|
+
- `cast` - type-cast received value if value has invalid type;
|
34
|
+
- `privacy` - reader incapsulation level;
|
35
|
+
- `finalize` - value post-processing (receives method name or proc);
|
36
|
+
- `default` - defalut value (if an attribute is not provided);
|
37
|
+
- last `Hash` argument will be treated as `kwarg`s;
|
38
|
+
|
39
|
+
`param` signautre:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
param <attribute_name>,
|
43
|
+
<type=SmartCore::Types::Value::Any>, # Any by default
|
44
|
+
cast: false, # false by default
|
45
|
+
privacy: :public, # :public by default
|
46
|
+
finalize: proc { |value| value } # no finalization by default
|
47
|
+
```
|
48
|
+
|
49
|
+
`option` signature:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
option <attribute_name>,
|
53
|
+
<type=SmartCore::Types::Value::Any>, # Any by default
|
54
|
+
cast: false, # false by default
|
55
|
+
privacy: :public, # :public by default
|
56
|
+
finalize: proc { |value| value }, # no finalization by default
|
57
|
+
default: 123 # no default value by default
|
58
|
+
```
|
59
|
+
|
60
|
+
Example:
|
61
|
+
|
24
62
|
|
25
63
|
```ruby
|
26
64
|
class User
|
@@ -36,21 +74,19 @@ end
|
|
36
74
|
User.new(1, 'John', 'test123', role: :admin, metadata: {}, enabled: false)
|
37
75
|
```
|
38
76
|
|
39
|
-
|
40
|
-
|
41
|
-
- `param` has no :default option (at all);
|
42
|
-
- last hash argument will be treated as `kwarg`s;
|
77
|
+
---
|
43
78
|
|
44
|
-
|
79
|
+
## Type aliasing
|
45
80
|
|
46
81
|
```ruby
|
82
|
+
# define your own type alias
|
47
83
|
SmartCore::Initializer.type_alias(:hash, SmartCore::Types::Value::Hash)
|
48
84
|
|
49
85
|
class User
|
50
86
|
include SmartCore::Initializer
|
51
87
|
|
52
|
-
param :data, :hash
|
53
|
-
option :metadata, :hash
|
88
|
+
param :data, :hash # use your new defined type alias
|
89
|
+
option :metadata, :hash # use your new defined type alias
|
54
90
|
end
|
55
91
|
```
|
56
92
|
|
@@ -13,8 +13,8 @@ class SmartCore::Initializer::Constructor
|
|
13
13
|
def initialize(klass, arguments, block)
|
14
14
|
@klass = klass
|
15
15
|
@arguments = arguments
|
16
|
-
@
|
17
|
-
@
|
16
|
+
@parameters, @options = extract_attributes(arguments)
|
17
|
+
@block = block
|
18
18
|
end
|
19
19
|
|
20
20
|
# @return [Any]
|
@@ -26,6 +26,7 @@ class SmartCore::Initializer::Constructor
|
|
26
26
|
prevent_attribute_insufficiency
|
27
27
|
initialize_parameters(instance)
|
28
28
|
initialize_options(instance)
|
29
|
+
process_original_initializer(instance)
|
29
30
|
end
|
30
31
|
end
|
31
32
|
|
@@ -47,19 +48,19 @@ class SmartCore::Initializer::Constructor
|
|
47
48
|
#
|
48
49
|
# @api private
|
49
50
|
# @since 0.1.0
|
50
|
-
attr_reader :
|
51
|
+
attr_reader :parameters
|
51
52
|
|
52
53
|
# @return [Hash<Symbol,Any>]
|
53
54
|
#
|
54
55
|
# @api private
|
55
56
|
# @since 0.1.0
|
56
|
-
attr_reader :
|
57
|
+
attr_reader :options
|
57
58
|
|
58
59
|
# @return [Proc, NilClass]
|
59
60
|
#
|
60
61
|
# @api private
|
61
62
|
# @since 0.1.0
|
62
|
-
attr_reader :
|
63
|
+
attr_reader :block
|
63
64
|
|
64
65
|
# @return [void]
|
65
66
|
#
|
@@ -75,10 +76,10 @@ class SmartCore::Initializer::Constructor
|
|
75
76
|
raise(
|
76
77
|
SmartCore::Initializer::ParameterArgumentError,
|
77
78
|
"Wrong number of parameters " \
|
78
|
-
"(given #{
|
79
|
-
) unless
|
79
|
+
"(given #{parameters.size}, expected #{required_parameter_count})"
|
80
|
+
) unless parameters.size == required_parameter_count
|
80
81
|
|
81
|
-
missing_options = required_options.reject { |option|
|
82
|
+
missing_options = required_options.reject { |option| options.key?(option) }
|
82
83
|
|
83
84
|
raise(
|
84
85
|
SmartCore::Initializer::OptionArgumentError,
|
@@ -100,7 +101,7 @@ class SmartCore::Initializer::Constructor
|
|
100
101
|
# @api private
|
101
102
|
# @since 0.1.0
|
102
103
|
def initialize_parameters(instance)
|
103
|
-
parameter_definitions = Hash[klass.__params__.zip(
|
104
|
+
parameter_definitions = Hash[klass.__params__.zip(parameters)]
|
104
105
|
|
105
106
|
parameter_definitions.each_pair do |attribute, parameter_value|
|
106
107
|
parameter_value = attribute.type.cast(parameter_value) if attribute.cast?
|
@@ -121,7 +122,7 @@ class SmartCore::Initializer::Constructor
|
|
121
122
|
# @since 0.1.0
|
122
123
|
def initialize_options(instance)
|
123
124
|
klass.__options__.each do |attribute|
|
124
|
-
option_value =
|
125
|
+
option_value = options.fetch(attribute.name) { attribute.default }
|
125
126
|
option_value = attribute.type.cast(option_value) if attribute.cast?
|
126
127
|
|
127
128
|
# TODO: fail with SmartCore::Initializer::ArgumentError
|
@@ -133,26 +134,35 @@ class SmartCore::Initializer::Constructor
|
|
133
134
|
end
|
134
135
|
end
|
135
136
|
|
137
|
+
# @param instance [Any]
|
138
|
+
# @return [void]
|
139
|
+
#
|
140
|
+
# @api private
|
141
|
+
# @since 0.1.0
|
142
|
+
def process_original_initializer(instance)
|
143
|
+
instance.send(:initialize, *arguments, &block)
|
144
|
+
end
|
145
|
+
|
136
146
|
# @param arguments [Array<Any>]
|
137
147
|
# @return [Array<Array<Any>,Hash<Symbol,Any>>]
|
138
148
|
#
|
139
149
|
# @api private
|
140
150
|
# @since 0.1.0
|
141
151
|
def extract_attributes(arguments)
|
142
|
-
|
143
|
-
|
152
|
+
extracted_parameters = []
|
153
|
+
extracted_options = {}
|
144
154
|
|
145
155
|
if (
|
146
156
|
klass.__options__.size >= 1 &&
|
147
157
|
arguments.last.is_a?(Hash) &&
|
148
158
|
arguments.last.keys.all? { |key| key.is_a?(Symbol) }
|
149
159
|
)
|
150
|
-
|
151
|
-
|
160
|
+
extracted_parameters = arguments[0..-2]
|
161
|
+
extracted_options = arguments.last
|
152
162
|
else
|
153
|
-
|
163
|
+
extracted_parameters = arguments
|
154
164
|
end
|
155
165
|
|
156
|
-
[
|
166
|
+
[extracted_parameters, extracted_options]
|
157
167
|
end
|
158
168
|
end
|
@@ -28,6 +28,12 @@ module SmartCore::Initializer
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
# @return [void]
|
32
|
+
#
|
33
|
+
# @api private
|
34
|
+
# @since 0.1.0
|
35
|
+
def initialize(*); end
|
36
|
+
|
31
37
|
type_alias(:nil, SmartCore::Types::Value::Nil)
|
32
38
|
type_alias(:string, SmartCore::Types::Value::String)
|
33
39
|
type_alias(:symbol, SmartCore::Types::Value::Symbol)
|
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.alpha3
|
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-01
|
11
|
+
date: 2020-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: smart_engine
|