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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f835709c40984d70109e260c33764194c3f63422f37dafa4ba32b8b3b29b745d
4
- data.tar.gz: de748657c5ac079a378e916c24eb7781ecc40790b429c97d9af1bffc74129200
3
+ metadata.gz: 483a56f55f7fb7b8e67eb20b1779df0b59f33935544d19cd6c5cd0e018e9b3b1
4
+ data.tar.gz: 9b1fc252e8821e68b22d38fe06e0418e5792860bc6e583caafd4ff5f786f509a
5
5
  SHA512:
6
- metadata.gz: 79232e232cf4f5148fd3f98e87093a6b06fbfbaf8773e497dff082dde601101dbc4fe7ca9ed10de55d4559a6d2d03eb30659299ac7d1018f296884fb7ab0f3ed
7
- data.tar.gz: 9e8ac0af865c59a16535a70ae9006cd0de3cc9b680354401f81d9de8baba6524f2fd008cba0404600cd0b160cf21500dd3e5da63c02034e43ab2337e1c7d47c7
6
+ metadata.gz: d77018f9b4be1a8f0c7befa771313d01debf97144e2bdc0d10248f357347eedeb6c0734997e6946fa0a35b810a6d7a1b59c395c8855f9e708a48021f0a94c9e7
7
+ data.tar.gz: 9cf8e06b7d6c89951330beff5698cca00502183ed625b06c3e620a6e6df0d654db4b55788d26a68b9da390217d9eb42966b609a773ebd5baa3e71bfa6d838f17
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smart_initializer (0.1.0.alpha1)
4
+ smart_initializer (0.1.0.alpha2)
5
5
  smart_engine (~> 0.5)
6
6
  smart_types (~> 0.1.0.alpha6)
7
7
 
data/README.md CHANGED
@@ -20,7 +20,45 @@ require 'smart_core/types'
20
20
 
21
21
  ---
22
22
 
23
- ## Synopsis (DEMO)
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
- **Limitations**:
40
-
41
- - `param` has no :default option (at all);
42
- - last hash argument will be treated as `kwarg`s;
77
+ ---
43
78
 
44
- **Type aliasing**:
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
- @passed_parameters, @passed_options = extract_attributes(arguments)
17
- @passed_block = block
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 :passed_parameters
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 :passed_options
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 :passed_block
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 #{passed_parameters.size}, expected #{required_parameter_count})"
79
- ) unless passed_parameters.size == required_parameter_count
79
+ "(given #{parameters.size}, expected #{required_parameter_count})"
80
+ ) unless parameters.size == required_parameter_count
80
81
 
81
- missing_options = required_options.reject { |option| passed_options.key?(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(passed_parameters)]
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 = passed_options.fetch(attribute.name) { attribute.default }
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
- parameters = []
143
- options = {}
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
- parameters = arguments[0..-2]
151
- options = arguments.last
160
+ extracted_parameters = arguments[0..-2]
161
+ extracted_options = arguments.last
152
162
  else
153
- parameters = arguments
163
+ extracted_parameters = arguments
154
164
  end
155
165
 
156
- [parameters, options]
166
+ [extracted_parameters, extracted_options]
157
167
  end
158
168
  end
@@ -6,6 +6,6 @@ module SmartCore
6
6
  #
7
7
  # @api public
8
8
  # @since 0.1.0
9
- VERSION = '0.1.0.alpha2'
9
+ VERSION = '0.1.0.alpha3'
10
10
  end
11
11
  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.alpha2
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-30 00:00:00.000000000 Z
11
+ date: 2020-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: smart_engine