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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ea298c0193785c3c404e220a34f34bd30f00ced83729c1dc576010e015b7165
4
- data.tar.gz: 38455c727045bccff6dfa5cb17795960fcd51356f942aed7c6b982de21267482
3
+ metadata.gz: 84fe70ccca99e40ef3520238a5d0228fbaba056a19d6ced57e0ec7c96054b899
4
+ data.tar.gz: 366a938712e572477d6065d7f56f31b8fbf88891438834c7812ab0bf2f59f93d
5
5
  SHA512:
6
- metadata.gz: 395c899676cbd7656ace2655062f68b191798cec6ff9a68bdee5bf7b889b9cfd8ace657f74c1aaefc2d7a214fc1f8c4d6463f531064641e9febc31233a8b6b71
7
- data.tar.gz: 749967441723e4dea3d01d43bb41cae4d6661a8826eb511b5431181098ad76a372c9634307a47069c2800d476f868202b3d823a2bdb5e74a6fa30b6ffbe30d2f
6
+ metadata.gz: 6b4b69ea28043773a8a508fc1d0b1f61b6ca580ebf8dfaf7af9725645b544d134c19381088a525541de6b597bfa92352e8daa2a26ed110eaa82c81587afdfb0c
7
+ data.tar.gz: a1c4937537fe9c000ae2c45a2d0120bf00ed808ffd4830fd570e1b8db44497d1cc769261ed307fc62769e60ada42dea66418ece4387129501c3d2d792429f8c1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.0.2 (17-Apr-22)
4
+
5
+ * added `#to_json` and `#from_json` methods for serialization
6
+
7
+ * Initial release
8
+
3
9
  ## 0.0.1 (04-Apr-22)
4
10
 
5
11
  * Initial release
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 hierarchy.
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
@@ -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
@@ -65,6 +65,8 @@ module SafeAndSound
65
65
  Class.new(parent_type) do
66
66
  include(Initializer)
67
67
  include(Functions)
68
+ include(ToJson)
69
+
68
70
  class << self
69
71
  attr_accessor :fields, :variant_name
70
72
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SafeAndSound
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  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.1.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 00:00:00.000000000 Z
11
+ date: 2022-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 13.0.6
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: 13.0.6
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.15'
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.15'
40
+ version: '1.5'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest-reporters
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.5'
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: '1.5'
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