safe_and_sound 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +4 -3
- data/lib/safe_and_sound/json.rb +31 -0
- data/lib/safe_and_sound/type.rb +4 -0
- data/lib/safe_and_sound/variant.rb +2 -0
- data/lib/safe_and_sound/version.rb +1 -1
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84fe70ccca99e40ef3520238a5d0228fbaba056a19d6ced57e0ec7c96054b899
|
4
|
+
data.tar.gz: 366a938712e572477d6065d7f56f31b8fbf88891438834c7812ab0bf2f59f93d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b4b69ea28043773a8a508fc1d0b1f61b6ca580ebf8dfaf7af9725645b544d134c19381088a525541de6b597bfa92352e8daa2a26ed110eaa82c81587afdfb0c
|
7
|
+
data.tar.gz: a1c4937537fe9c000ae2c45a2d0120bf00ed808ffd4830fd570e1b8db44497d1cc769261ed307fc62769e60ada42dea66418ece4387129501c3d2d792429f8c1
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ It's inspired by the concise syntax to declare new types in [Elm](https://guide.
|
|
7
7
|
These types share some properties with types referred to as _algebaric data types_, _sum types_ or _union types_.
|
8
8
|
|
9
9
|
We can model similar relationships more verbosely in plain Ruby with classes and subclasses.
|
10
|
-
This library provides some syntactic shortcuts to create these
|
10
|
+
This library provides some syntactic shortcuts to create these hierarchies.
|
11
11
|
|
12
12
|
```ruby
|
13
13
|
Vehicle = SafeAndSound.new(
|
@@ -40,8 +40,8 @@ include SafeAndSound::Functions
|
|
40
40
|
|
41
41
|
def to_human(vehicle)
|
42
42
|
chase vehicle do
|
43
|
-
Vehicle::Car, -> { "I'm a car with #{horsepower} horsepower" }
|
44
|
-
Vehicle::Bike, -> { "I'm a bike with #{gears} gears" }
|
43
|
+
wenn Vehicle::Car, -> { "I'm a car with #{horsepower} horsepower" }
|
44
|
+
wenn Vehicle::Bike, -> { "I'm a bike with #{gears} gears" }
|
45
45
|
end
|
46
46
|
end
|
47
47
|
```
|
@@ -58,5 +58,6 @@ episode of the _Destroy all software_ screencast.
|
|
58
58
|
I'm not trying to change how Ruby code is written. This is merely an experiment how far the sum type concept can be taken in terms
|
59
59
|
of making a syntax for it look like the syntax in languages where this concept is more central.
|
60
60
|
|
61
|
+
Check out more examples in the [examples folder](examples).
|
61
62
|
|
62
63
|
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SafeAndSound
|
4
|
+
module ToJson
|
5
|
+
def as_json
|
6
|
+
hash = { 'type' => self.class.variant_name.to_s }
|
7
|
+
self.class.fields.each do |field_name, _|
|
8
|
+
hash[field_name.to_s] = send(field_name)
|
9
|
+
end
|
10
|
+
hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_json(*_args)
|
14
|
+
as_json.to_json
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module FromJson
|
19
|
+
def from_json(json_string)
|
20
|
+
hash =
|
21
|
+
JSON
|
22
|
+
.parse(json_string)
|
23
|
+
.transform_keys(&:to_sym)
|
24
|
+
variant_name = hash[:type]
|
25
|
+
variant = variants.find { |v| v.name.gsub(/.*::/, '') == variant_name }
|
26
|
+
raise ArgumentError, "Unable to find #{variant_name} for #{type.name}" unless variant
|
27
|
+
|
28
|
+
variant.new(**hash.except(:type))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/safe_and_sound/type.rb
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'safe_and_sound/json'
|
4
|
+
|
3
5
|
module SafeAndSound
|
4
6
|
class BaseTypeCannotBeInstantiated < StandardError; end
|
5
7
|
|
6
8
|
##
|
7
9
|
# base class for the newly defined types
|
8
10
|
class Type
|
11
|
+
extend FromJson
|
12
|
+
|
9
13
|
def initialize
|
10
14
|
raise BaseTypeCannotBeInstantiated, 'You cannot create instances of this type directly but only of its variants'
|
11
15
|
end
|
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe_and_sound
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Axel Tetzlaff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-04-
|
11
|
+
date: 2022-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: minitest
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '5.15'
|
20
20
|
type: :development
|
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:
|
26
|
+
version: '5.15'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: minitest
|
28
|
+
name: minitest-reporters
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '5
|
33
|
+
version: '1.5'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '5
|
40
|
+
version: '1.5'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 13.0.6
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 13.0.6
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rubocop
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- README.md
|
79
79
|
- lib/safe_and_sound.rb
|
80
80
|
- lib/safe_and_sound/functions.rb
|
81
|
+
- lib/safe_and_sound/json.rb
|
81
82
|
- lib/safe_and_sound/maybe.rb
|
82
83
|
- lib/safe_and_sound/result.rb
|
83
84
|
- lib/safe_and_sound/type.rb
|