smart_initializer 0.1.0.alpha4 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/.travis.yml +36 -5
- data/CHANGELOG.md +23 -0
- data/Gemfile.lock +63 -43
- data/README.md +184 -26
- data/Rakefile +1 -1
- data/bin/rspec +54 -0
- data/gemfiles/with_external_deps.gemfile +7 -0
- data/gemfiles/with_external_deps.gemfile.lock +102 -0
- data/gemfiles/without_external_deps.gemfile +5 -0
- data/gemfiles/without_external_deps.gemfile.lock +100 -0
- data/lib/smart_core/initializer.rb +41 -35
- data/lib/smart_core/initializer/attribute.rb +12 -4
- data/lib/smart_core/initializer/attribute/factory.rb +47 -27
- data/lib/smart_core/initializer/attribute/parameters.rb +12 -4
- data/lib/smart_core/initializer/configuration.rb +33 -0
- data/lib/smart_core/initializer/constructor.rb +21 -8
- data/lib/smart_core/initializer/constructor/definer.rb +34 -11
- data/lib/smart_core/initializer/dsl.rb +27 -7
- data/lib/smart_core/initializer/errors.rb +44 -0
- data/lib/smart_core/initializer/functionality.rb +37 -0
- data/lib/smart_core/initializer/instance_attribute_accessing.rb +51 -0
- data/lib/smart_core/initializer/plugins.rb +17 -0
- data/lib/smart_core/initializer/plugins/abstract.rb +55 -0
- data/lib/smart_core/initializer/plugins/access_mixin.rb +47 -0
- data/lib/smart_core/initializer/plugins/registry.rb +166 -0
- data/lib/smart_core/initializer/plugins/registry_interface.rb +77 -0
- data/lib/smart_core/initializer/plugins/thy_types.rb +30 -0
- data/lib/smart_core/initializer/plugins/thy_types/errors.rb +11 -0
- data/lib/smart_core/initializer/plugins/thy_types/thy_types.rb +23 -0
- data/lib/smart_core/initializer/plugins/thy_types/thy_types/abstract_factory.rb +78 -0
- data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation.rb +12 -0
- data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/base.rb +9 -0
- data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/cast.rb +21 -0
- data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/valid.rb +16 -0
- data/lib/smart_core/initializer/plugins/thy_types/thy_types/operation/validate.rb +19 -0
- data/lib/smart_core/initializer/settings.rb +49 -0
- data/lib/smart_core/initializer/settings/duplicator.rb +20 -0
- data/lib/smart_core/initializer/settings/type_system.rb +69 -0
- data/lib/smart_core/initializer/type_system.rb +16 -0
- data/lib/smart_core/initializer/type_system/interop.rb +103 -0
- data/lib/smart_core/initializer/type_system/interop/abstract_factory.rb +70 -0
- data/lib/smart_core/initializer/type_system/interop/aliasing.rb +72 -0
- data/lib/smart_core/initializer/type_system/interop/aliasing/alias_list.rb +141 -0
- data/lib/smart_core/initializer/type_system/interop/operation.rb +30 -0
- data/lib/smart_core/initializer/type_system/registry.rb +174 -0
- data/lib/smart_core/initializer/type_system/registry_interface.rb +102 -0
- data/lib/smart_core/initializer/type_system/smart_types.rb +48 -0
- data/lib/smart_core/initializer/type_system/smart_types/abstract_factory.rb +72 -0
- data/lib/smart_core/initializer/type_system/smart_types/operation.rb +13 -0
- data/lib/smart_core/initializer/type_system/smart_types/operation/base.rb +8 -0
- data/lib/smart_core/initializer/type_system/smart_types/operation/cast.rb +16 -0
- data/lib/smart_core/initializer/type_system/smart_types/operation/valid.rb +16 -0
- data/lib/smart_core/initializer/type_system/smart_types/operation/validate.rb +16 -0
- data/lib/smart_core/initializer/version.rb +2 -1
- data/smart_initializer.gemspec +13 -9
- metadata +68 -17
- data/lib/smart_core/initializer/attribute/init_extension.rb +0 -4
- data/lib/smart_core/initializer/type_aliasing.rb +0 -50
- 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5f516b06f95abb4c257cf0507897bd8d9ee65a27db12f117c3f54fd8cbefab7
|
4
|
+
data.tar.gz: be6bf05b4dbdbca0cafde4ac3097926e4d5b45ea5f3a0c21f4e0679bf0a4e590
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b00d4d9be6148a7b24c6fe9b178b99d7934bb05fa6751d857a2d2c62abc1d3b5640ae6099b264f0c27c570e9e62632ae9f3f870e1a0ddb91bf51c6c3e9f23577
|
7
|
+
data.tar.gz: 28aad37b3850085764aed335bd93da10295f6f4e3cb32f76bf5a3dcac55fac2f1cd60de3f73fb4778f8e661662291c2c2deff3efbfd69dc8edce75c04cadd2ec
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -2,19 +2,50 @@
|
|
2
2
|
language: ruby
|
3
3
|
cache: bundler
|
4
4
|
os: linux
|
5
|
-
|
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.
|
13
|
-
|
14
|
-
|
15
|
-
- rvm: 2.
|
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,25 @@
|
|
1
1
|
# Changelog
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
## [0.3.2] - 2020-07-12
|
5
|
+
### Fixed
|
6
|
+
- Deeply inherited entities lose their `__initializer_settings__` entitiy;
|
7
|
+
|
8
|
+
## [0.3.1] - 2020-07-12
|
9
|
+
### Fixed
|
10
|
+
- Deeply inherited entities lose their class attribute definers;
|
11
|
+
|
12
|
+
## [0.3.0] - 2020-07-11
|
13
|
+
### Added
|
14
|
+
- `extend_initialization_flow` alias method for `SmartCore::Initializer.ext_init`;
|
15
|
+
- Access methods to the instance attribute lists (`#__params`, `#__options__`, `#__attributes__`);
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
- Updated development dependencies;
|
19
|
+
|
20
|
+
## [0.2.0] - 2020-05-16
|
21
|
+
### Changed
|
22
|
+
- **Constructor**: disallow unknown option attributes;
|
23
|
+
|
24
|
+
## [0.1.0] - 2020-05-10
|
25
|
+
- Release :)
|
data/Gemfile.lock
CHANGED
@@ -1,80 +1,100 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
smart_initializer (0.1
|
5
|
-
|
6
|
-
|
4
|
+
smart_initializer (0.3.1)
|
5
|
+
qonfig (~> 0.24)
|
6
|
+
smart_engine (~> 0.7)
|
7
|
+
smart_types (~> 0.1.0)
|
7
8
|
|
8
9
|
GEM
|
9
10
|
remote: https://rubygems.org/
|
10
11
|
specs:
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
activesupport (6.0.3.2)
|
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.87.1.2)
|
19
|
+
rubocop (= 0.87.1)
|
20
|
+
rubocop-performance (= 1.7.0)
|
21
|
+
rubocop-rails (= 2.6.0)
|
22
|
+
rubocop-rake (= 0.5.1)
|
23
|
+
rubocop-rspec (= 1.42.0)
|
24
|
+
ast (2.4.1)
|
25
|
+
concurrent-ruby (1.1.6)
|
26
|
+
diff-lcs (1.4.4)
|
19
27
|
docile (1.3.2)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
28
|
+
i18n (1.8.3)
|
29
|
+
concurrent-ruby (~> 1.0)
|
30
|
+
minitest (5.14.1)
|
31
|
+
parallel (1.19.2)
|
32
|
+
parser (2.7.1.4)
|
33
|
+
ast (~> 2.4.1)
|
34
|
+
qonfig (0.24.1)
|
35
|
+
rack (2.2.3)
|
26
36
|
rainbow (3.0.0)
|
27
37
|
rake (13.0.1)
|
38
|
+
regexp_parser (1.7.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.
|
33
|
-
rspec-support (~> 3.9.
|
34
|
-
rspec-expectations (3.9.
|
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.
|
41
|
-
rubocop (0.
|
42
|
-
jaro_winkler (~> 1.5.1)
|
52
|
+
rspec-support (3.9.3)
|
53
|
+
rubocop (0.87.1)
|
43
54
|
parallel (~> 1.10)
|
44
|
-
parser (>= 2.7.
|
55
|
+
parser (>= 2.7.1.1)
|
45
56
|
rainbow (>= 2.2.2, < 4.0)
|
57
|
+
regexp_parser (>= 1.7)
|
58
|
+
rexml
|
59
|
+
rubocop-ast (>= 0.1.0, < 1.0)
|
46
60
|
ruby-progressbar (~> 1.7)
|
47
|
-
unicode-display_width (>= 1.4.0, <
|
48
|
-
rubocop-
|
49
|
-
|
50
|
-
rubocop-
|
61
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
62
|
+
rubocop-ast (0.1.0)
|
63
|
+
parser (>= 2.7.0.1)
|
64
|
+
rubocop-performance (1.7.0)
|
65
|
+
rubocop (>= 0.82.0)
|
66
|
+
rubocop-rails (2.6.0)
|
67
|
+
activesupport (>= 4.2.0)
|
51
68
|
rack (>= 1.1)
|
52
|
-
rubocop (>= 0.
|
53
|
-
rubocop-rake (0.5.
|
69
|
+
rubocop (>= 0.82.0)
|
70
|
+
rubocop-rake (0.5.1)
|
54
71
|
rubocop
|
55
|
-
rubocop-rspec (1.
|
56
|
-
rubocop (>= 0.
|
72
|
+
rubocop-rspec (1.42.0)
|
73
|
+
rubocop (>= 0.87.0)
|
57
74
|
ruby-progressbar (1.10.1)
|
58
|
-
simplecov (0.
|
75
|
+
simplecov (0.18.5)
|
59
76
|
docile (~> 1.1)
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
77
|
+
simplecov-html (~> 0.11)
|
78
|
+
simplecov-html (0.12.2)
|
79
|
+
smart_engine (0.7.0)
|
80
|
+
smart_types (0.1.0)
|
81
|
+
smart_engine (~> 0.6)
|
82
|
+
thread_safe (0.3.6)
|
83
|
+
tzinfo (1.2.7)
|
84
|
+
thread_safe (~> 0.1)
|
85
|
+
unicode-display_width (1.7.0)
|
86
|
+
zeitwerk (2.3.1)
|
67
87
|
|
68
88
|
PLATFORMS
|
69
89
|
ruby
|
70
90
|
|
71
91
|
DEPENDENCIES
|
72
|
-
armitage-rubocop (~> 0.
|
92
|
+
armitage-rubocop (~> 0.87)
|
73
93
|
bundler (~> 2.1)
|
74
94
|
rake (~> 13.0)
|
75
95
|
rspec (~> 3.9)
|
76
|
-
simplecov (~> 0.
|
96
|
+
simplecov (~> 0.18)
|
77
97
|
smart_initializer!
|
78
98
|
|
79
99
|
BUNDLED WITH
|
80
|
-
2.1.
|
100
|
+
2.1.4
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# SmartCore::Initializer · [![Gem Version](https://badge.fury.io/rb/smart_initializer.svg)](https://badge.fury.io/rb/smart_initializer) [![Build Status](https://travis-ci.org/smart-rb/smart_initializer.svg?branch=master)](https://travis-ci.org/smart-rb/smart_initializer)
|
2
2
|
|
3
|
-
A simple and convenient way to declare complex constructors
|
3
|
+
A simple and convenient way to declare complex constructors with a support for various commonly used type systems.
|
4
|
+
(**in active development**).
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -11,11 +12,11 @@ gem 'smart_initializer'
|
|
11
12
|
```shell
|
12
13
|
bundle install
|
13
14
|
# --- or ---
|
14
|
-
gem install
|
15
|
+
gem install smart_initializer
|
15
16
|
```
|
16
17
|
|
17
18
|
```ruby
|
18
|
-
require 'smart_core/
|
19
|
+
require 'smart_core/initializer'
|
19
20
|
```
|
20
21
|
|
21
22
|
---
|
@@ -23,8 +24,13 @@ require 'smart_core/types'
|
|
23
24
|
## Table of contents
|
24
25
|
|
25
26
|
- [Synopsis](#synopsis)
|
26
|
-
- [
|
27
|
+
- [Access to the instance attributes](#access-to-the-instance-attributes)
|
28
|
+
- [Configuration](#configuration)
|
29
|
+
- [Type aliasing](#type-aliasing)
|
27
30
|
- [Initialization extension](#initialization-extension)
|
31
|
+
- [Plugins](#plugins)
|
32
|
+
- [thy-types](#plugin-thy-types)
|
33
|
+
- [How to run tests](#how-to-run-tests)
|
28
34
|
|
29
35
|
---
|
30
36
|
|
@@ -42,25 +48,50 @@ require 'smart_core/types'
|
|
42
48
|
- `cast` - type-cast received value if value has invalid type;
|
43
49
|
- `privacy` - reader incapsulation level;
|
44
50
|
- `finalize` - value post-processing (receives method name or proc);
|
51
|
+
- `type_system` - differently chosen type system for the current attribute;
|
45
52
|
- (limitation) param has no `:default` option;
|
46
53
|
- `option` - defined kwarg-like attribute:
|
47
54
|
- `cast` - type-cast received value if value has invalid type;
|
48
55
|
- `privacy` - reader incapsulation level;
|
49
56
|
- `finalize` - value post-processing (receives method name or proc);
|
50
57
|
- `default` - defalut value (if an attribute is not provided);
|
58
|
+
- `type_system` - differently chosen type system for the current attribute;
|
51
59
|
- last `Hash` argument will be treated as `kwarg`s;
|
52
60
|
|
53
|
-
|
61
|
+
#### initializer integration
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# with pre-configured type system (:smart_types, see Configuration doc)
|
65
|
+
|
66
|
+
class MyStructure
|
67
|
+
include SmartCore::Initializer
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
# with manually chosen type system
|
73
|
+
|
74
|
+
class MyStructure
|
75
|
+
include SmartCore::Initializer(type_system: :smart_types)
|
76
|
+
end
|
77
|
+
|
78
|
+
class AnotherStructure
|
79
|
+
include SmartCore::Initializer(type_system: :thy_types)
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
#### `param` signautre:
|
54
84
|
|
55
85
|
```ruby
|
56
86
|
param <attribute_name>,
|
57
87
|
<type=SmartCore::Types::Value::Any>, # Any by default
|
58
88
|
cast: false, # false by default
|
59
89
|
privacy: :public, # :public by default
|
60
|
-
finalize: proc { |value| value } # no finalization by default
|
90
|
+
finalize: proc { |value| value }, # no finalization by default
|
91
|
+
type_system: :smart_types # used by default
|
61
92
|
```
|
62
93
|
|
63
|
-
`option` signature:
|
94
|
+
#### `option` signature:
|
64
95
|
|
65
96
|
```ruby
|
66
97
|
option <attribute_name>,
|
@@ -68,7 +99,8 @@ option
|
|
68
99
|
cast: false, # false by default
|
69
100
|
privacy: :public, # :public by default
|
70
101
|
finalize: proc { |value| value }, # no finalization by default
|
71
|
-
default: 123 # no default value by default
|
102
|
+
default: 123, # no default value by default
|
103
|
+
type_system: :smart_types # used by default
|
72
104
|
```
|
73
105
|
|
74
106
|
Example:
|
@@ -77,6 +109,8 @@ Example:
|
|
77
109
|
```ruby
|
78
110
|
class User
|
79
111
|
include SmartCore::Initializer
|
112
|
+
# --- or ---
|
113
|
+
include SmartCore::Initializer(type_system: :smart_types)
|
80
114
|
|
81
115
|
param :user_id, SmartCore::Types::Value::Integer, cast: false, privacy: :public
|
82
116
|
option :role, default: :user, finalize: -> { |value| Role.find(name: value) }
|
@@ -90,37 +124,84 @@ User.new(1, 'John', 'test123', role: :admin, metadata: {}, enabled: false)
|
|
90
124
|
|
91
125
|
---
|
92
126
|
|
127
|
+
## Access to the instance attributes
|
128
|
+
|
129
|
+
- `#__params__` - returns a list of initialized params;
|
130
|
+
- `#__options__` - returns a list of initialized options;
|
131
|
+
- `#__attributes__` - returns a list of merged params and options;
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
class User
|
135
|
+
include SmartCore::Initializer
|
136
|
+
|
137
|
+
param :first_name, 'string'
|
138
|
+
param :second_name, 'string'
|
139
|
+
option :age, 'numeric'
|
140
|
+
option :is_admin, 'boolean', default: true
|
141
|
+
end
|
142
|
+
|
143
|
+
user = User.new('Rustam', 'Ibragimov', age: 28)
|
144
|
+
|
145
|
+
user.__params__ # => { first_name: 'Rustam', second_name: 'Ibragimov' }
|
146
|
+
user.__options__ # => { age: 28, is_admin: true }
|
147
|
+
user.__attributes__ # => { first_name: 'Rustam', second_name: 'Ibragimov', age: 28, is_admin: true }
|
148
|
+
```
|
149
|
+
|
150
|
+
---
|
151
|
+
|
152
|
+
## Configuration
|
153
|
+
|
154
|
+
- based on `Qonfig` gem;
|
155
|
+
- you can read config values via `[]` or `.config.settings` or `.config[key]`;
|
156
|
+
- setitngs:
|
157
|
+
- `default_type_system` - default type system (`smart_types` by default);
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
# configure:
|
161
|
+
SmartCore::Initializer::Configuration.configure do |config|
|
162
|
+
config.default_type_system = :smart_types # default setting value
|
163
|
+
end
|
164
|
+
```
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
# read:
|
168
|
+
SmartCore::Initializer::Configuration[:default_type_system]
|
169
|
+
SmartCore::Initializer::Configuration.config[:default_type_system]
|
170
|
+
SmartCore::Initializer::Configuration.config.settings.default_type_system
|
171
|
+
```
|
172
|
+
|
173
|
+
---
|
174
|
+
|
93
175
|
## Type aliasing
|
94
176
|
|
95
177
|
- Usage:
|
96
178
|
|
97
179
|
```ruby
|
98
|
-
#
|
99
|
-
SmartCore::Initializer.type_alias(
|
180
|
+
# for smart_types:
|
181
|
+
SmartCore::Initializer::TypeSystem::SmartTypes.type_alias('hsh', SmartCore::Types::Value::Hash)
|
182
|
+
|
183
|
+
# for thy:
|
184
|
+
SmartCore::Initializer::TypeSystem::ThyTypes.type_alias('int', Thy::Tyhes::Integer)
|
100
185
|
|
101
186
|
class User
|
102
187
|
include SmartCore::Initializer
|
103
188
|
|
104
|
-
param :data,
|
105
|
-
option :metadata, :
|
189
|
+
param :data, 'hsh' # use your new defined type alias
|
190
|
+
option :metadata, :hsh # use your new defined type alias
|
191
|
+
|
192
|
+
param :age, 'int', type_system: :thy_types
|
106
193
|
end
|
107
194
|
```
|
108
195
|
|
109
196
|
- Predefined aliases:
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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`
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
# for smart_types:
|
200
|
+
SmartCore::Initializer::TypeSystem::SmartTypes.type_aliases
|
201
|
+
|
202
|
+
# for thy_types:
|
203
|
+
SmartCore::Initializer::TypeSystem::ThyTypes.type_aliases
|
204
|
+
```
|
124
205
|
|
125
206
|
---
|
126
207
|
|
@@ -129,6 +210,7 @@ end
|
|
129
210
|
- `ext_init(&block)`:
|
130
211
|
- you can define as many extensions as you want;
|
131
212
|
- extensions are invoked in the order they are defined;
|
213
|
+
- alias method: `extend_initialization_flow`;
|
132
214
|
|
133
215
|
```ruby
|
134
216
|
class User
|
@@ -150,6 +232,82 @@ user.extra2 # => :ext2
|
|
150
232
|
|
151
233
|
---
|
152
234
|
|
235
|
+
## Plugins
|
236
|
+
|
237
|
+
- [thy-types](#plugin-thy-types)
|
238
|
+
|
239
|
+
---
|
240
|
+
|
241
|
+
## Plugin: thy-types
|
242
|
+
|
243
|
+
Support for `Thy::Types` type system ([gem](https://github.com/akxcv/thy))
|
244
|
+
|
245
|
+
- install `thy` types (`gem install thy`):
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
gem 'thy'
|
249
|
+
```
|
250
|
+
|
251
|
+
```shell
|
252
|
+
bundle install
|
253
|
+
```
|
254
|
+
|
255
|
+
- enable `thy_types` plugin:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
require 'thy'
|
259
|
+
SmartCore::Initializer::Configuration.plugin(:thy_types)
|
260
|
+
```
|
261
|
+
|
262
|
+
- usage:
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
class User
|
266
|
+
include SmartCore::Initializer(type_system: :thy_types)
|
267
|
+
|
268
|
+
param :nickname, 'string'
|
269
|
+
param :email, 'value.text', type_system: :smart_types # mixing with smart_types
|
270
|
+
option :admin, Thy::Types::Boolean, default: false
|
271
|
+
option :age, (Thy::Type.new { |value| value > 18 }) # custom thy type is supported too
|
272
|
+
end
|
273
|
+
|
274
|
+
# valid case:
|
275
|
+
User.new('daiver', 'iamdaiver@gmail.com', { admin: true, age: 19 })
|
276
|
+
# => new user object
|
277
|
+
|
278
|
+
# invalid case (invalid age)
|
279
|
+
User.new('daiver', 'iamdaiver@gmail.com', { age: 17 })
|
280
|
+
# SmartCore::Initializer::ThyTypeValidationError
|
281
|
+
|
282
|
+
# invaldi case (invalid nickname)
|
283
|
+
User.new(123, 'test', { admin: true, age: 22 })
|
284
|
+
# => SmartCore::Initializer::ThyTypeValidationError
|
285
|
+
```
|
286
|
+
|
287
|
+
---
|
288
|
+
|
289
|
+
## How to run tests
|
290
|
+
|
291
|
+
- with plugin tests:
|
292
|
+
|
293
|
+
```shell
|
294
|
+
bin/rspec -w
|
295
|
+
```
|
296
|
+
|
297
|
+
- without plugin tests:
|
298
|
+
|
299
|
+
```shell
|
300
|
+
bin/rspec -n
|
301
|
+
```
|
302
|
+
|
303
|
+
- help message:
|
304
|
+
|
305
|
+
```shell
|
306
|
+
bin/rspec -h
|
307
|
+
```
|
308
|
+
|
309
|
+
---
|
310
|
+
|
153
311
|
## Contributing
|
154
312
|
|
155
313
|
- Fork it ( https://github.com/smart-rb/smart_initializer )
|