smart_initializer 0.1.0.alpha4 → 0.1.0

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.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/.travis.yml +36 -5
  4. data/CHANGELOG.md +3 -0
  5. data/Gemfile.lock +48 -31
  6. data/README.md +151 -22
  7. data/Rakefile +1 -1
  8. data/bin/rspec +54 -0
  9. data/gemfiles/with_external_deps.gemfile +7 -0
  10. data/gemfiles/with_external_deps.gemfile.lock +99 -0
  11. data/gemfiles/without_external_deps.gemfile +5 -0
  12. data/gemfiles/without_external_deps.gemfile.lock +97 -0
  13. data/lib/smart_core/initializer.rb +38 -35
  14. data/lib/smart_core/initializer/attribute.rb +12 -4
  15. data/lib/smart_core/initializer/attribute/factory.rb +47 -27
  16. data/lib/smart_core/initializer/attribute/parameters.rb +12 -4
  17. data/lib/smart_core/initializer/configurable_module.rb +27 -0
  18. data/lib/smart_core/initializer/configuration.rb +33 -0
  19. data/lib/smart_core/initializer/constructor.rb +9 -6
  20. data/lib/smart_core/initializer/constructor/definer.rb +34 -11
  21. data/lib/smart_core/initializer/dsl.rb +23 -6
  22. data/lib/smart_core/initializer/errors.rb +44 -0
  23. data/lib/smart_core/initializer/plugins.rb +17 -0
  24. data/lib/smart_core/initializer/plugins/abstract.rb +55 -0
  25. data/lib/smart_core/initializer/plugins/access_mixin.rb +47 -0
  26. data/lib/smart_core/initializer/plugins/registry.rb +166 -0
  27. data/lib/smart_core/initializer/plugins/registry_interface.rb +77 -0
  28. data/lib/smart_core/initializer/plugins/thy_types.rb +30 -0
  29. data/lib/smart_core/initializer/plugins/thy_types/errors.rb +11 -0
  30. data/lib/smart_core/initializer/plugins/thy_types/thy_types.rb +23 -0
  31. data/lib/smart_core/initializer/plugins/thy_types/thy_types/abstract_factory.rb +78 -0
  32. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation.rb +12 -0
  33. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/base.rb +9 -0
  34. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/cast.rb +21 -0
  35. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/valid.rb +16 -0
  36. data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/validate.rb +19 -0
  37. data/lib/smart_core/initializer/settings.rb +49 -0
  38. data/lib/smart_core/initializer/settings/duplicator.rb +20 -0
  39. data/lib/smart_core/initializer/settings/type_system.rb +69 -0
  40. data/lib/smart_core/initializer/type_system.rb +16 -0
  41. data/lib/smart_core/initializer/type_system/interop.rb +103 -0
  42. data/lib/smart_core/initializer/type_system/interop/abstract_factory.rb +70 -0
  43. data/lib/smart_core/initializer/type_system/interop/aliasing.rb +72 -0
  44. data/lib/smart_core/initializer/type_system/interop/aliasing/alias_list.rb +141 -0
  45. data/lib/smart_core/initializer/type_system/interop/operation.rb +30 -0
  46. data/lib/smart_core/initializer/type_system/registry.rb +174 -0
  47. data/lib/smart_core/initializer/type_system/registry_interface.rb +102 -0
  48. data/lib/smart_core/initializer/type_system/smart_types.rb +48 -0
  49. data/lib/smart_core/initializer/type_system/smart_types/abstract_factory.rb +72 -0
  50. data/lib/smart_core/initializer/type_system/smart_types/operation.rb +13 -0
  51. data/lib/smart_core/initializer/type_system/smart_types/operation/base.rb +8 -0
  52. data/lib/smart_core/initializer/type_system/smart_types/operation/cast.rb +16 -0
  53. data/lib/smart_core/initializer/type_system/smart_types/operation/valid.rb +16 -0
  54. data/lib/smart_core/initializer/type_system/smart_types/operation/validate.rb +16 -0
  55. data/lib/smart_core/initializer/version.rb +1 -1
  56. data/smart_initializer.gemspec +13 -9
  57. metadata +67 -17
  58. data/lib/smart_core/initializer/attribute/init_extension.rb +0 -4
  59. data/lib/smart_core/initializer/type_aliasing.rb +0 -50
  60. data/lib/smart_core/initializer/type_aliasing/alias_list.rb +0 -101
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 045121a7aff669e936532780b634d3212d6063c051534bb465efc3a81fdcfb8c
4
- data.tar.gz: f3aee4d1ada5fbb1dea05a39b7b81beb34096a75b41e641a15ddee7ea04e049a
3
+ metadata.gz: 756d03ef9a7c4f10bb075d1669bb4abe856d0dd6a68c0bd632b927a3a32662e6
4
+ data.tar.gz: 7deaef19a44a8e9a813bb5548aef332df88087b62f5cf6265929875f7eb20000
5
5
  SHA512:
6
- metadata.gz: ede12d393005f5c9c60fb33f063bbc8cd5ae12ecdb831f7b1219538b6e66a0adb8002fe9f0608e03fbc9831ac653d0a3c9e038c65d4a26cf7b1724608b105169
7
- data.tar.gz: e4b976324cdbe52f4e2a9546ff621987c9536f1faf08468b68a2bc2e25cb2b98e2560a2d46f0dd76ec5559e60f577018993a3ffb35c5f742987fd3e88a79c125
6
+ metadata.gz: 1c04e15b4f1896201bf08aec13c9bb2e5d770b65115960a9ca01d307088b04171f0c81beaa1b71d54fc2bf6888406d9e63ce0a49b5695baf41523f89b89ac85a
7
+ data.tar.gz: 3f3ff4b2fa17bb7727abdb1d73839bc954b0e0c2dba9f19ad8a46a95cfa9df3a17549bdfc73775d6753d799052a14f4e7a9732fefbb5dafbec5f20f72b8063b4
data/.rubocop.yml CHANGED
@@ -5,7 +5,7 @@ inherit_gem:
5
5
  - lib/rubocop.rspec.yml
6
6
 
7
7
  AllCops:
8
- TargetRubyVersion: 2.4.9
8
+ TargetRubyVersion: 2.7.1
9
9
  Include:
10
10
  - lib/**/*.rb
11
11
  - spec/**/*.rb
data/.travis.yml CHANGED
@@ -2,19 +2,50 @@
2
2
  language: ruby
3
3
  cache: bundler
4
4
  os: linux
5
- before_install: gem install bundler -v 2.1.2
5
+ dist: xenial
6
+ before_install: gem install bundler
6
7
  script:
7
8
  - bundle exec rake rubocop
8
9
  - bundle exec rake rspec
9
10
  jobs:
10
11
  fast_finish: true
11
12
  include:
12
- - rvm: 2.4.9
13
- - rvm: 2.5.7
14
- - rvm: 2.6.5
15
- - rvm: 2.7.0
13
+ - rvm: 2.4.10
14
+ gemfile: gemfiles/with_external_deps.gemfile
15
+ env: TEST_PLUGINS=true FULL_TEST_COVERAGE_CHECK=true
16
+ - rvm: 2.5.8
17
+ gemfile: gemfiles/with_external_deps.gemfile
18
+ env: TEST_PLUGINS=true FULL_TEST_COVERAGE_CHECK=true
19
+ - rvm: 2.6.6
20
+ gemfile: gemfiles/with_external_deps.gemfile
21
+ env: TEST_PLUGINS=true FULL_TEST_COVERAGE_CHECK=true
22
+ - rvm: 2.7.1
23
+ gemfile: gemfiles/with_external_deps.gemfile
24
+ env: TEST_PLUGINS=true FULL_TEST_COVERAGE_CHECK=true
16
25
  - rvm: ruby-head
26
+ gemfile: gemfiles/with_external_deps.gemfile
27
+ env: TEST_PLUGINS=true FULL_TEST_COVERAGE_CHECK=true
17
28
  - rvm: jruby-head
29
+ gemfile: gemfiles/with_external_deps.gemfile
30
+ env: TEST_PLUGINS=true FULL_TEST_COVERAGE_CHECK=true
31
+ - rvm: truffleruby
32
+ gemfile: gemfiles/with_external_deps.gemfile
33
+ env: TEST_PLUGINS=true FULL_TEST_COVERAGE_CHECK=true
34
+ - rvm: 2.4.10
35
+ gemfile: gemfiles/without_external_deps.gemfile
36
+ - rvm: 2.5.8
37
+ gemfile: gemfiles/without_external_deps.gemfile
38
+ - rvm: 2.6.6
39
+ gemfile: gemfiles/without_external_deps.gemfile
40
+ - rvm: 2.7.1
41
+ gemfile: gemfiles/without_external_deps.gemfile
42
+ - rvm: ruby-head
43
+ gemfile: gemfiles/without_external_deps.gemfile
44
+ - rvm: jruby-head
45
+ gemfile: gemfiles/without_external_deps.gemfile
46
+ - rvm: truffleruby
47
+ gemfile: gemfiles/without_external_deps.gemfile
18
48
  allow_failures:
19
49
  - rvm: ruby-head
20
50
  - rvm: jruby-head
51
+ - rvm: truffleruby
data/CHANGELOG.md CHANGED
@@ -1,2 +1,5 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
+
4
+ ## [0.1.0] - 2020-05-10
5
+ - Release :)
data/Gemfile.lock CHANGED
@@ -1,80 +1,97 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smart_initializer (0.1.0.alpha3)
5
- smart_engine (~> 0.5)
6
- smart_types (~> 0.1.0.alpha6)
4
+ smart_initializer (0.1.0.alpha4)
5
+ qonfig (~> 0.24)
6
+ smart_engine (~> 0.6)
7
+ smart_types (~> 0.1.0)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
11
- armitage-rubocop (0.79.0)
12
- rubocop (= 0.79.0)
12
+ activesupport (6.0.3)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ zeitwerk (~> 2.2, >= 2.2.2)
18
+ armitage-rubocop (0.82.0.2)
19
+ rubocop (= 0.82.0)
13
20
  rubocop-performance (= 1.5.2)
14
- rubocop-rails (= 2.4.1)
15
- rubocop-rake (= 0.5.0)
16
- rubocop-rspec (= 1.37.1)
21
+ rubocop-rails (= 2.5.2)
22
+ rubocop-rake (= 0.5.1)
23
+ rubocop-rspec (= 1.39.0)
17
24
  ast (2.4.0)
25
+ concurrent-ruby (1.1.6)
18
26
  diff-lcs (1.3)
19
27
  docile (1.3.2)
28
+ i18n (1.8.2)
29
+ concurrent-ruby (~> 1.0)
20
30
  jaro_winkler (1.5.4)
21
- json (2.3.0)
31
+ minitest (5.14.0)
22
32
  parallel (1.19.1)
23
- parser (2.7.0.2)
33
+ parser (2.7.1.2)
24
34
  ast (~> 2.4.0)
25
- rack (2.1.1)
35
+ qonfig (0.24.1)
36
+ rack (2.2.2)
26
37
  rainbow (3.0.0)
27
38
  rake (13.0.1)
39
+ rexml (3.2.4)
28
40
  rspec (3.9.0)
29
41
  rspec-core (~> 3.9.0)
30
42
  rspec-expectations (~> 3.9.0)
31
43
  rspec-mocks (~> 3.9.0)
32
- rspec-core (3.9.1)
33
- rspec-support (~> 3.9.1)
34
- rspec-expectations (3.9.0)
44
+ rspec-core (3.9.2)
45
+ rspec-support (~> 3.9.3)
46
+ rspec-expectations (3.9.2)
35
47
  diff-lcs (>= 1.2.0, < 2.0)
36
48
  rspec-support (~> 3.9.0)
37
49
  rspec-mocks (3.9.1)
38
50
  diff-lcs (>= 1.2.0, < 2.0)
39
51
  rspec-support (~> 3.9.0)
40
- rspec-support (3.9.2)
41
- rubocop (0.79.0)
52
+ rspec-support (3.9.3)
53
+ rubocop (0.82.0)
42
54
  jaro_winkler (~> 1.5.1)
43
55
  parallel (~> 1.10)
44
56
  parser (>= 2.7.0.1)
45
57
  rainbow (>= 2.2.2, < 4.0)
58
+ rexml
46
59
  ruby-progressbar (~> 1.7)
47
- unicode-display_width (>= 1.4.0, < 1.7)
60
+ unicode-display_width (>= 1.4.0, < 2.0)
48
61
  rubocop-performance (1.5.2)
49
62
  rubocop (>= 0.71.0)
50
- rubocop-rails (2.4.1)
63
+ rubocop-rails (2.5.2)
64
+ activesupport
51
65
  rack (>= 1.1)
52
66
  rubocop (>= 0.72.0)
53
- rubocop-rake (0.5.0)
67
+ rubocop-rake (0.5.1)
54
68
  rubocop
55
- rubocop-rspec (1.37.1)
69
+ rubocop-rspec (1.39.0)
56
70
  rubocop (>= 0.68.1)
57
71
  ruby-progressbar (1.10.1)
58
- simplecov (0.17.1)
72
+ simplecov (0.18.5)
59
73
  docile (~> 1.1)
60
- json (>= 1.8, < 3)
61
- simplecov-html (~> 0.10.0)
62
- simplecov-html (0.10.2)
63
- smart_engine (0.5.0)
64
- smart_types (0.1.0.alpha6)
65
- smart_engine (~> 0.5)
66
- unicode-display_width (1.6.1)
74
+ simplecov-html (~> 0.11)
75
+ simplecov-html (0.12.2)
76
+ smart_engine (0.6.0)
77
+ smart_types (0.1.0)
78
+ smart_engine (~> 0.6)
79
+ thread_safe (0.3.6)
80
+ tzinfo (1.2.7)
81
+ thread_safe (~> 0.1)
82
+ unicode-display_width (1.7.0)
83
+ zeitwerk (2.3.0)
67
84
 
68
85
  PLATFORMS
69
86
  ruby
70
87
 
71
88
  DEPENDENCIES
72
- armitage-rubocop (~> 0.78)
89
+ armitage-rubocop (~> 0.81)
73
90
  bundler (~> 2.1)
74
91
  rake (~> 13.0)
75
92
  rspec (~> 3.9)
76
- simplecov (~> 0.17)
93
+ simplecov (~> 0.18)
77
94
  smart_initializer!
78
95
 
79
96
  BUNDLED WITH
80
- 2.1.2
97
+ 2.1.4
data/README.md CHANGED
@@ -25,6 +25,9 @@ require 'smart_core/types'
25
25
  - [Synopsis](#synopsis)
26
26
  - [Type Aliasing](#type-aliasing)
27
27
  - [Initialization extension](#initialization-extension)
28
+ - [Plugins](#plugins)
29
+ - [thy-types](#plugin-thy-types)
30
+ - [How to run tests](#how-to-run-tests)
28
31
 
29
32
  ---
30
33
 
@@ -42,25 +45,50 @@ require 'smart_core/types'
42
45
  - `cast` - type-cast received value if value has invalid type;
43
46
  - `privacy` - reader incapsulation level;
44
47
  - `finalize` - value post-processing (receives method name or proc);
48
+ - `type_system` - differently chosen type system for the current attribute;
45
49
  - (limitation) param has no `:default` option;
46
50
  - `option` - defined kwarg-like attribute:
47
51
  - `cast` - type-cast received value if value has invalid type;
48
52
  - `privacy` - reader incapsulation level;
49
53
  - `finalize` - value post-processing (receives method name or proc);
50
54
  - `default` - defalut value (if an attribute is not provided);
55
+ - `type_system` - differently chosen type system for the current attribute;
51
56
  - last `Hash` argument will be treated as `kwarg`s;
52
57
 
53
- `param` signautre:
58
+ #### initializer integration
59
+
60
+ ```ruby
61
+ # with pre-configured type system (:smart_types, see Configuration doc)
62
+
63
+ class MyStructure
64
+ include SmartCore::Initializer
65
+ end
66
+ ```
67
+
68
+ ```ruby
69
+ # with manually chosen type system
70
+
71
+ class MyStructure
72
+ include SmartCore::Initializer(type_system: :smart_types)
73
+ end
74
+
75
+ class AnotherStructure
76
+ include SmartCore::Initializer(type_system: :thy_types)
77
+ end
78
+ ```
79
+
80
+ #### `param` signautre:
54
81
 
55
82
  ```ruby
56
83
  param <attribute_name>,
57
84
  <type=SmartCore::Types::Value::Any>, # Any by default
58
85
  cast: false, # false by default
59
86
  privacy: :public, # :public by default
60
- finalize: proc { |value| value } # no finalization by default
87
+ finalize: proc { |value| value }, # no finalization by default
88
+ type_system: :smart_types # used by default
61
89
  ```
62
90
 
63
- `option` signature:
91
+ #### `option` signature:
64
92
 
65
93
  ```ruby
66
94
  option <attribute_name>,
@@ -68,7 +96,8 @@ option <attribute_name>,
68
96
  cast: false, # false by default
69
97
  privacy: :public, # :public by default
70
98
  finalize: proc { |value| value }, # no finalization by default
71
- default: 123 # no default value by default
99
+ default: 123, # no default value by default
100
+ type_system: :smart_types # used by default
72
101
  ```
73
102
 
74
103
  Example:
@@ -77,6 +106,8 @@ Example:
77
106
  ```ruby
78
107
  class User
79
108
  include SmartCore::Initializer
109
+ # --- or ---
110
+ include SmartCore::Initializer(type_system: :smart_types)
80
111
 
81
112
  param :user_id, SmartCore::Types::Value::Integer, cast: false, privacy: :public
82
113
  option :role, default: :user, finalize: -> { |value| Role.find(name: value) }
@@ -90,37 +121,59 @@ User.new(1, 'John', 'test123', role: :admin, metadata: {}, enabled: false)
90
121
 
91
122
  ---
92
123
 
124
+ ## Configuration
125
+
126
+ - based on `Qonfig` gem;
127
+ - you can read config values via `[]` or `.config.settings` or `.config[key]`;
128
+ - setitngs:
129
+ - `default_type_system` - default type system (`smart_types` by default);
130
+
131
+ ```ruby
132
+ # configure:
133
+ SmartCore::Initializer::Configuration.configure do |config|
134
+ config.default_type_system = :smart_types # default setting value
135
+ end
136
+ ```
137
+
138
+ ```ruby
139
+ # read:
140
+ SmartCore::Initializer::Configuration[:default_type_system]
141
+ SmartCore::Initializer::Configuration.config[:default_type_system]
142
+ SmartCore::Initializer::Configuration.config.settings.default_type_system
143
+ ```
144
+
145
+ ---
146
+
93
147
  ## Type aliasing
94
148
 
95
149
  - Usage:
96
150
 
97
151
  ```ruby
98
- # define your own type alias
99
- SmartCore::Initializer.type_alias(:hash, SmartCore::Types::Value::Hash)
152
+ # for smart_types:
153
+ SmartCore::Initializer::TypeSystem::SmartTypes.type_alias('hsh', SmartCore::Types::Value::Hash)
154
+
155
+ # for thy:
156
+ SmartCore::Initializer::TypeSystem::ThyTypes.type_alias('int', Thy::Tyhes::Integer)
100
157
 
101
158
  class User
102
159
  include SmartCore::Initializer
103
160
 
104
- param :data, :hash # use your new defined type alias
105
- option :metadata, :hash # use your new defined type alias
161
+ param :data, 'hsh' # use your new defined type alias
162
+ option :metadata, :hsh # use your new defined type alias
163
+
164
+ param :age, 'int', type_system: :thy_types
106
165
  end
107
166
  ```
108
167
 
109
168
  - 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`
169
+
170
+ ```ruby
171
+ # for smart_types:
172
+ SmartCore::Initializer::TypeSystem::SmartTypes.type_aliases
173
+
174
+ # for thy_types:
175
+ SmartCore::Initializer::TypeSystem::ThyTypes.type_aliases
176
+ ```
124
177
 
125
178
  ---
126
179
 
@@ -150,6 +203,82 @@ user.extra2 # => :ext2
150
203
 
151
204
  ---
152
205
 
206
+ ## Plugins
207
+
208
+ - [thy-types](#plugin-thy-types)
209
+
210
+ ---
211
+
212
+ ## Plugin: thy-types
213
+
214
+ Support for `Thy::Types` type system ([gem](https://github.com/akxcv/thy))
215
+
216
+ - install `thy` types (`gem install thy`):
217
+
218
+ ```ruby
219
+ gem 'thy'
220
+ ```
221
+
222
+ ```shell
223
+ bundle install
224
+ ```
225
+
226
+ - enable `thy_types` plugin:
227
+
228
+ ```ruby
229
+ require 'thy'
230
+ SmartCore::Initializer::Configuration.plugin(:thy_types)
231
+ ```
232
+
233
+ - usage:
234
+
235
+ ```ruby
236
+ class User
237
+ include SmartCore::Initializer(type_system: :thy_types)
238
+
239
+ param :nickname, 'string'
240
+ param :email, 'value.text', type_system: :smart_types # mixing with smart_types
241
+ option :admin, Thy::Types::Boolean, default: false
242
+ option :age, (Thy::Type.new { |value| value > 18 }) # custom thy type is supported too
243
+ end
244
+
245
+ # valid case:
246
+ User.new('daiver', 'iamdaiver@gmail.com', { admin: true, age: 19 })
247
+ # => new user object
248
+
249
+ # invalid case (invalid age)
250
+ User.new('daiver', 'iamdaiver@gmail.com', { age: 17 })
251
+ # SmartCore::Initializer::ThyTypeValidationError
252
+
253
+ # invaldi case (invalid nickname)
254
+ User.new(123, 'test', { admin: true, age: 22 })
255
+ # => SmartCore::Initializer::ThyTypeValidationError
256
+ ```
257
+
258
+ ---
259
+
260
+ ## How to run tests
261
+
262
+ - with plugin tests:
263
+
264
+ ```shell
265
+ bin/rspec -w
266
+ ```
267
+
268
+ - without plugin tests:
269
+
270
+ ```shell
271
+ bin/rspec -g
272
+ ```
273
+
274
+ - help message:
275
+
276
+ ```shell
277
+ bin/rspec -h
278
+ ```
279
+
280
+ ---
281
+
153
282
  ## Contributing
154
283
 
155
284
  - Fork it ( https://github.com/smart-rb/smart_initializer )