smart_types 0.0.0 → 0.1.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +45 -1
- data/lib/smart_core/types/primitive/mult_factory.rb +1 -1
- data/lib/smart_core/types/primitive/nilable_checker.rb +37 -0
- data/lib/smart_core/types/primitive/nilable_factory.rb +49 -0
- data/lib/smart_core/types/primitive/sum_factory.rb +1 -1
- data/lib/smart_core/types/primitive/undefined_caster.rb +10 -1
- data/lib/smart_core/types/primitive.rb +23 -2
- data/lib/smart_core/types/struct.rb +6 -0
- data/lib/smart_core/types/system/definition_dsl.rb +40 -0
- data/lib/smart_core/types/system.rb +4 -0
- data/lib/smart_core/types/value/any.rb +0 -1
- data/lib/smart_core/types/value/text.rb +4 -7
- data/lib/smart_core/types/version.rb +2 -1
- data/smart_types.gemspec +2 -2
- metadata +11 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 984ff8c37feb29e6b3141b845c0f9f4266bdafc0c22fdf195ed9c4ab2036d7a9
|
4
|
+
data.tar.gz: 689d1cc015483cea2ca32cbd3c2e93c0e5cf486c31dc016f37d267171ec6f23a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc955ee3f9f07f82be3a0144d382f949cdfce0b2c274e4ca3f229d1d4ca935eaea44ffe54d0d60a461cce43ab27a9a0bc357bd2e5ebf4d637b9450aca1b78e19
|
7
|
+
data.tar.gz: c0451f6106a9e7d59f10c187edcd9b9cc6a9bc8992df0b44fa3354d0456091cf479135e2ae7565f96922f0ddcef87b243c04b06ebefa35636a508c0a405fa659
|
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@ PATH
|
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
4
|
smart_types (0.1.0)
|
5
|
-
smart_engine (~> 0.
|
5
|
+
smart_engine (~> 0.4)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -59,7 +59,7 @@ GEM
|
|
59
59
|
json (>= 1.8, < 3)
|
60
60
|
simplecov-html (~> 0.10.0)
|
61
61
|
simplecov-html (0.10.2)
|
62
|
-
smart_engine (0.
|
62
|
+
smart_engine (0.4.0)
|
63
63
|
unicode-display_width (1.6.0)
|
64
64
|
|
65
65
|
PLATFORMS
|
data/README.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
# SmartCore::Types · [![Gem Version](https://badge.fury.io/rb/smart_types.svg)](https://badge.fury.io/rb/smart_types) [![Build Status](https://travis-ci.org/smart-rb/smart_types.svg?branch=master)](https://travis-ci.org/smart-rb/smart_types)
|
2
2
|
|
3
|
-
|
3
|
+
> A set of objects that acts like types (type checking and type casting) with a support for basic type algebra.
|
4
|
+
|
5
|
+
Full-featured type system for any ruby project. Supports custom type definitioning,
|
4
6
|
type validation, type casting and type categorizing. Provides a set of commonly used type
|
5
7
|
categories and general purpose types. Has a flexible and simplest type definition toolchain.
|
6
8
|
|
9
|
+
Just add and use :) Enjoy! :)
|
10
|
+
|
7
11
|
## Installation
|
8
12
|
|
9
13
|
```ruby
|
@@ -22,6 +26,18 @@ require 'smart_core/types'
|
|
22
26
|
|
23
27
|
---
|
24
28
|
|
29
|
+
## Type Interface
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# documentation is coming
|
33
|
+
|
34
|
+
type.valid?(value)
|
35
|
+
type.cast(value)
|
36
|
+
type.nilable
|
37
|
+
type3 = type1 | type2
|
38
|
+
type4 = type1 & type2
|
39
|
+
```
|
40
|
+
|
25
41
|
## Supported types
|
26
42
|
|
27
43
|
- Primitive Value Types:
|
@@ -44,6 +60,26 @@ SmartCore::Types::Value::Module
|
|
44
60
|
|
45
61
|
---
|
46
62
|
|
63
|
+
## Nilable types
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
SmartCore::Types::Value::Any.nilable
|
67
|
+
SmartCore::Types::Value::String.nilable
|
68
|
+
SmartCore::Types::Value::Symbol.nilable
|
69
|
+
SmartCore::Types::Value::Text.nilable
|
70
|
+
SmartCore::Types::Value::Integer.nilable
|
71
|
+
SmartCore::Types::Value::Float.nilable
|
72
|
+
SmartCore::Types::Value::Numeric.nilable
|
73
|
+
SmartCore::Types::Value::Boolean.nilable
|
74
|
+
SmartCore::Types::Value::Array.nilable
|
75
|
+
SmartCore::Types::Value::Hash.nilable
|
76
|
+
SmartCore::Types::Value::Proc.nilable
|
77
|
+
SmartCore::Types::Value::Class.nilable
|
78
|
+
SmartCore::Types::Value::Module.nilable
|
79
|
+
```
|
80
|
+
|
81
|
+
---
|
82
|
+
|
47
83
|
## Type validation and type casting
|
48
84
|
|
49
85
|
```ruby
|
@@ -60,6 +96,14 @@ SmartCore::Types::Value::Module
|
|
60
96
|
|
61
97
|
---
|
62
98
|
|
99
|
+
## Basic type algebra
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
# documentation is coming
|
103
|
+
```
|
104
|
+
|
105
|
+
---
|
106
|
+
|
63
107
|
## Type system integration
|
64
108
|
|
65
109
|
```ruby
|
@@ -27,7 +27,7 @@ module SmartCore::Types::Primitive::MultFactory
|
|
27
27
|
# @pai private
|
28
28
|
# @since 0.1.0
|
29
29
|
def build_type_definitions(type_definition)
|
30
|
-
|
30
|
+
SmartCore::Types::Primitive::MultFactory::DefinitionContext.new.tap do |context|
|
31
31
|
context.instance_eval(&type_definition) if type_definition
|
32
32
|
end
|
33
33
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.1.0
|
5
|
+
class SmartCore::Types::Primitive::NilableChecker
|
6
|
+
# @param checker [Checker, SumChecker, MultChecker, NilableChecker]
|
7
|
+
# @return [void]
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
# @since 0.1.0
|
11
|
+
def initialize(checker)
|
12
|
+
@checker = checker
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param value [Any]
|
16
|
+
# @return [Boolean]
|
17
|
+
#
|
18
|
+
# @api private
|
19
|
+
# @since 0.1.0
|
20
|
+
def call(value)
|
21
|
+
# rubocop:disable Style/NilComparison
|
22
|
+
# NOTE: #nil? is not used cuz BasicObject has no #nil? method
|
23
|
+
(value == nil) ? true : checker.call(value)
|
24
|
+
# rubocop:enable Style/NilComparison
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# @return [SmartCore::Types::Primitive::Checker]
|
30
|
+
# @return [SmartCore::Types::Primitive::MultChecker]
|
31
|
+
# @return [SmartCore::Types::Primitive::SumChecker]
|
32
|
+
# @return [SmartCore::Types::Primitive::NilableChecker]
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
# @since 0.1.0
|
36
|
+
attr_reader :checker
|
37
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.1.0
|
5
|
+
module SmartCore::Types::Primitive::NilableFactory
|
6
|
+
class << self
|
7
|
+
# @param type [SmartCore::Types::Primitive]
|
8
|
+
# @return [SmartCore::Type::Primitive]
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
# @since 0.1.0
|
12
|
+
def create_type(type)
|
13
|
+
type_checker = build_type_checker(type)
|
14
|
+
type_caster = build_type_caster(type)
|
15
|
+
build_type(type, type_checker, type_caster)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# @param type [SmartCore::Types::Primitive]
|
21
|
+
# @return [SmartCore::Types::Primitive::NilableChecker]
|
22
|
+
#
|
23
|
+
# @api private
|
24
|
+
# @since 0.1.0
|
25
|
+
def build_type_checker(type)
|
26
|
+
SmartCore::Types::Primitive::NilableChecker.new(type.checker)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param type [SmartCore::Types::Primitive]
|
30
|
+
# @return [SmartCore::Types::Primitive::Caster]
|
31
|
+
#
|
32
|
+
# @api private
|
33
|
+
# @since 0.1.0
|
34
|
+
def build_type_caster(type)
|
35
|
+
type.caster
|
36
|
+
end
|
37
|
+
|
38
|
+
# @param type [SmartCore::Types::Primitive]
|
39
|
+
# @param type_checler [SmartCore::Types::Primitive::NilableChecker]
|
40
|
+
# @param type_caster [SmartCore::Types::Caster]
|
41
|
+
# @return [SmartCore::Type::Primitive]
|
42
|
+
#
|
43
|
+
# @api private
|
44
|
+
# @since 0.1.0
|
45
|
+
def build_type(type, type_checker, type_caster)
|
46
|
+
Class.new(type.class).new(type_checker, type_caster)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -27,7 +27,7 @@ module SmartCore::Types::Primitive::SumFactory
|
|
27
27
|
# @pai private
|
28
28
|
# @since 0.1.0
|
29
29
|
def build_type_definitions(type_definition)
|
30
|
-
|
30
|
+
SmartCore::Types::Primitive::SumFactory::DefinitionContext.new.tap do |context|
|
31
31
|
context.instance_eval(&type_definition) if type_definition
|
32
32
|
end
|
33
33
|
end
|
@@ -2,7 +2,16 @@
|
|
2
2
|
|
3
3
|
# @api private
|
4
4
|
# @since 0.1.0
|
5
|
-
class SmartCore::Types::Primitive::UndefinedCaster
|
5
|
+
class SmartCore::Types::Primitive::UndefinedCaster < SmartCore::Types::Primitive::Caster
|
6
|
+
# @param expression [NilClass, Any]
|
7
|
+
# @return [void]
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
# @since 0.1.0
|
11
|
+
def initialize(expression = nil)
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
6
15
|
# @param value [Any]
|
7
16
|
# @return [void]
|
8
17
|
#
|
@@ -6,11 +6,13 @@ class SmartCore::Types::Primitive
|
|
6
6
|
require_relative 'primitive/caster'
|
7
7
|
require_relative 'primitive/undefined_caster'
|
8
8
|
require_relative 'primitive/checker'
|
9
|
+
require_relative 'primitive/nilable_checker'
|
9
10
|
require_relative 'primitive/sum_checker'
|
10
11
|
require_relative 'primitive/mult_checker'
|
11
12
|
require_relative 'primitive/factory'
|
12
13
|
require_relative 'primitive/sum_factory'
|
13
14
|
require_relative 'primitive/mult_factory'
|
15
|
+
require_relative 'primitive/nilable_factory'
|
14
16
|
|
15
17
|
# @since 0.1.0
|
16
18
|
include SmartCore::Types::System::ProducerDSL
|
@@ -29,13 +31,16 @@ class SmartCore::Types::Primitive
|
|
29
31
|
|
30
32
|
# @param checker [SmartCore::Types::Primitive::Checker]
|
31
33
|
# @param caster [SmartCore::Types::Primitive::Caster]
|
34
|
+
# @param nilable [SmartCore::Types::Primitive]
|
32
35
|
# @return [void]
|
33
36
|
#
|
34
37
|
# @api private
|
35
38
|
# @since 0.1.0
|
36
39
|
def initialize(checker, caster)
|
40
|
+
@lock = SmartCore::Engine::Lock.new
|
37
41
|
@checker = checker
|
38
42
|
@caster = caster
|
43
|
+
@nilable = nil
|
39
44
|
end
|
40
45
|
|
41
46
|
# @param value [Any]
|
@@ -56,13 +61,21 @@ class SmartCore::Types::Primitive
|
|
56
61
|
caster.call(value)
|
57
62
|
end
|
58
63
|
|
64
|
+
# @return [SmartCore::Types::Primitive]
|
65
|
+
#
|
66
|
+
# @api public
|
67
|
+
# @since 0.1.0
|
68
|
+
def nilable
|
69
|
+
lock.synchronize { @nilable ||= self.class::NilableFactory.create_type(self) }
|
70
|
+
end
|
71
|
+
|
59
72
|
# @param another_primitive [SmartCore::Types::Primitive]
|
60
73
|
# @return [SmartCore::Types::Primitive]
|
61
74
|
#
|
62
75
|
# @api public
|
63
76
|
# @since 0.1.0
|
64
77
|
def |(another_primitive)
|
65
|
-
self.class::SumFactory.create_type(self, another_primitive)
|
78
|
+
self.class::SumFactory.create_type([self, another_primitive])
|
66
79
|
end
|
67
80
|
|
68
81
|
# @param another_primitive [SmartCore::Types::Primitive]
|
@@ -71,6 +84,14 @@ class SmartCore::Types::Primitive
|
|
71
84
|
# @api public
|
72
85
|
# @since 0.1.0
|
73
86
|
def &(another_primitive)
|
74
|
-
self.class::MultFactory.create_type(self, another_primitive)
|
87
|
+
self.class::MultFactory.create_type([self, another_primitive])
|
75
88
|
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
# @return [SmartCore::Engine::Lock]
|
93
|
+
#
|
94
|
+
# @api private
|
95
|
+
# @since 0.1.0
|
96
|
+
attr_reader :lock
|
76
97
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# @api private
|
4
|
+
# @since 0.1.0
|
5
|
+
module SmartCore::Types::System::DefinitionDSL
|
6
|
+
class << self
|
7
|
+
# @param base_klass [Class]
|
8
|
+
# @return [void]
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
# @since 0.1.0
|
12
|
+
def included(base_klass)
|
13
|
+
base_klass.extend(ClassMethods)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# @api private
|
18
|
+
# @since 0.1.0
|
19
|
+
module ClassMethods
|
20
|
+
# @param types [Array<SmartCore::Types::Pirmitive>]
|
21
|
+
# @param type_definition [Block]
|
22
|
+
# @return [SmartCore::Types::Primitive]
|
23
|
+
#
|
24
|
+
# @api public
|
25
|
+
# @since 0.1.0
|
26
|
+
def type_sum(*types, &type_definition)
|
27
|
+
SmartCore::Types::Primitive::SumFactory.create_type(types, type_definition)
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param types [Array<SmartCore::Types::Pirmitive>]
|
31
|
+
# @param type_definition [Block]
|
32
|
+
# @return [SmartCore::Types::Primitive]
|
33
|
+
#
|
34
|
+
# @api public
|
35
|
+
# @since 0.1.0
|
36
|
+
def type_mult(*types, &type_definition)
|
37
|
+
SmartCore::Types::Primitive::MultFactory.create_type(types, type_definition)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -2,10 +2,7 @@
|
|
2
2
|
|
3
3
|
# @api public
|
4
4
|
# @since 0.1.0
|
5
|
-
SmartCore::Types::Value
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
type.define_caster(&:to_s)
|
11
|
-
end
|
5
|
+
SmartCore::Types::Value::Text = SmartCore::Types::System.type_sum(
|
6
|
+
SmartCore::Types::Value::String,
|
7
|
+
SmartCore::Types::Value::Symbol
|
8
|
+
) { |type| type.define_caster(&:to_s) }
|
data/smart_types.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
|
13
13
|
spec.summary = 'Full-featured type system for any ruby project.'
|
14
14
|
spec.description = <<~DESCRIPTION
|
15
|
-
Full-featured type system for any ruby project.
|
15
|
+
Full-featured type system for any ruby project. Supports custom type definitioning,
|
16
16
|
type validation, type casting and type categorizing. Provides a set of commonly used type
|
17
17
|
categories and general purpose types. Has a flexible and simplest type definition toolchain.
|
18
18
|
DESCRIPTION
|
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
33
|
spec.require_paths = ['lib']
|
34
34
|
|
35
|
-
spec.add_dependency 'smart_engine', '~> 0.
|
35
|
+
spec.add_dependency 'smart_engine', '~> 0.4'
|
36
36
|
|
37
37
|
spec.add_development_dependency 'bundler', '~> 2.1'
|
38
38
|
spec.add_development_dependency 'rake', '~> 13.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_types
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.1.0.alpha
|
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-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: smart_engine
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.4'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.4'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,7 +95,7 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.17'
|
97
97
|
description: |
|
98
|
-
Full-featured type system for any ruby project.
|
98
|
+
Full-featured type system for any ruby project. Supports custom type definitioning,
|
99
99
|
type validation, type casting and type categorizing. Provides a set of commonly used type
|
100
100
|
categories and general purpose types. Has a flexible and simplest type definition toolchain.
|
101
101
|
email:
|
@@ -127,11 +127,15 @@ files:
|
|
127
127
|
- lib/smart_core/types/primitive/mult_checker.rb
|
128
128
|
- lib/smart_core/types/primitive/mult_factory.rb
|
129
129
|
- lib/smart_core/types/primitive/mult_factory/definition_context.rb
|
130
|
+
- lib/smart_core/types/primitive/nilable_checker.rb
|
131
|
+
- lib/smart_core/types/primitive/nilable_factory.rb
|
130
132
|
- lib/smart_core/types/primitive/sum_checker.rb
|
131
133
|
- lib/smart_core/types/primitive/sum_factory.rb
|
132
134
|
- lib/smart_core/types/primitive/sum_factory/definition_context.rb
|
133
135
|
- lib/smart_core/types/primitive/undefined_caster.rb
|
136
|
+
- lib/smart_core/types/struct.rb
|
134
137
|
- lib/smart_core/types/system.rb
|
138
|
+
- lib/smart_core/types/system/definition_dsl.rb
|
135
139
|
- lib/smart_core/types/system/producer_dsl.rb
|
136
140
|
- lib/smart_core/types/value.rb
|
137
141
|
- lib/smart_core/types/value/any.rb
|
@@ -167,9 +171,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
171
|
version: 2.4.9
|
168
172
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
173
|
requirements:
|
170
|
-
- - "
|
174
|
+
- - ">"
|
171
175
|
- !ruby/object:Gem::Version
|
172
|
-
version:
|
176
|
+
version: 1.3.1
|
173
177
|
requirements: []
|
174
178
|
rubygems_version: 3.1.2
|
175
179
|
signing_key:
|