primitive 1.0.0 → 1.0.1

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: f5fea6de5e8ac8bc4cf0ed94ffa76a3a935e9ebfe33e47460e96e232379a9339
4
- data.tar.gz: 795db0a28c8f71eea22e5e299a32d137724cea0500366dbfab3005a89beb7dd0
3
+ metadata.gz: 40ee5850f19f0fc29653907baf90b65348998d2af630a1599fcba4b63a55c35f
4
+ data.tar.gz: fe83bc660e0ed2480d3f3fee6eea8d51d56b20e819eb6f8e29b79dd74b954802
5
5
  SHA512:
6
- metadata.gz: f1c90290b182bf50464cc325f308ac3c9858d5028c514b7368f26a43d75c678766a338e1435e0726cf3c32991c38a55c92b674e6f2593b19b882c41d327b1489
7
- data.tar.gz: 8f69f0bda83956ae55d8dbfdb0c62bfbb661cf6378270ec0321751168198be4daa5c6c606a931ad3594725ba1650700d0baea3e552f48ec15a70b1e65e99e05e
6
+ metadata.gz: 9b31365c588140386e21bd10fe17e6fc472fa6dc74be50a66c6d5cb238d35e02747e4bdd3f4132edf57478641340b25e919a043351afa73b5d206cdb37b7de03
7
+ data.tar.gz: 3438616f17a12a2cfb3491509e3e65d9c4edb29e585b6df7588d2050eed1cdf3aeeacf31aba41488d615e4cf397a837c9120d5ed5003470f4ebae3c169082a83
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ### 1.0.1
2
+
3
+ * Making Sorbet a required runtime dependency. This is highly opionated and specific to the desired caller use-cases and may not be suitable for all callers.
4
+ * Adding Repository interface.
5
+ * Adding CompactFile as a quick example for implementing a basic repository.
6
+
1
7
  ### 1.0.0
2
8
 
3
9
  * `Primitive::Entity` added as the first primitive building block.
data/README.md CHANGED
@@ -20,10 +20,10 @@ bundle add primitive
20
20
 
21
21
  ### Primitive::Entity
22
22
 
23
- Base class for objects identified by a string value (ID). Extension example:
23
+ Base class for objects identified by a string value (ID). Extension examples:
24
24
 
25
25
  ```ruby
26
- class Team < Primitive
26
+ class Team < Primitive::Entity
27
27
  attr_reader :name
28
28
 
29
29
  def initialize(id:, name:)
@@ -32,6 +32,17 @@ class Team < Primitive
32
32
  @name = name
33
33
  end
34
34
  end
35
+
36
+ # Not allowing direct manipulation of ID in the case it is controlled by the repository.
37
+ class Player < Primitive::Entity
38
+ attr_reader :name
39
+
40
+ def initialize(name:)
41
+ super()
42
+
43
+ @name = name
44
+ end
45
+ end
35
46
  ```
36
47
 
37
48
  Several features are now provided by its base-class via the ID attribute:
@@ -41,6 +52,10 @@ Several features are now provided by its base-class via the ID attribute:
41
52
  * Sorting: `#<=>`
42
53
  * Outputting: `#to_s`
43
54
 
55
+ ### Primitive::Repository
56
+
57
+ An interface that describes how a repository should function for loading/saving entities. A concrete example (albeit simple) is the `Primitive::CompactFile` class which leverages YAML for serialization. Note that ID, in this case, is representative of the file path.
58
+
44
59
  ## Contributing
45
60
 
46
61
  ### Development Environment Configuration
@@ -0,0 +1,60 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative 'repository'
5
+
6
+ module Primitive
7
+ # Knows how to load and save an Entity from/to local disk to a single file.
8
+ # YAML serialization will be used so in order to safely deserialize, the list of
9
+ # permitted classes that can be serialized must be specified.
10
+ class CompactFile
11
+ extend T::Sig
12
+ include Repository
13
+
14
+ sig { returns(T::Array[T::Class[T.anything]]) }
15
+ attr_reader :permitted_classes
16
+
17
+ sig { params(permitted_classes: T::Array[T::Class[T.anything]]).void }
18
+ def initialize(permitted_classes: [Primitive::Entity])
19
+ @permitted_classes = permitted_classes
20
+ end
21
+
22
+ sig { override.params(entity: Primitive::Entity, id: T.nilable(String)).returns(String) }
23
+ def save(entity, id = nil)
24
+ raise ArgumentError, 'id is required' unless id
25
+
26
+ FileUtils.mkdir_p(File.dirname(id))
27
+
28
+ entity.send('id=', id)
29
+
30
+ File.write(id, serialize(entity))
31
+
32
+ id
33
+ end
34
+
35
+ sig { override.params(id: String).returns(Primitive::Entity) }
36
+ def retrieve(id)
37
+ deserialize(File.read(id)).tap do |entity|
38
+ entity.send('id=', id)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ sig { overridable.params(entity: Primitive::Entity).returns(String) }
45
+ def serialize(entity)
46
+ StringIO.new.tap do |io|
47
+ YAML.dump(entity, io)
48
+ end.string
49
+ end
50
+
51
+ sig { overridable.params(string: String).returns(Primitive::Entity) }
52
+ def deserialize(string)
53
+ YAML.load(
54
+ string,
55
+ permitted_classes:,
56
+ aliases: true
57
+ )
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,18 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Primitive
5
+ # Defines what a repository class should implement.
6
+ module Repository
7
+ extend T::Sig
8
+ extend T::Helpers
9
+
10
+ interface!
11
+
12
+ sig { abstract.params(entity: Primitive::Entity, id: T.nilable(String)).returns(String) }
13
+ def save(entity, id = nil); end
14
+
15
+ sig { abstract.params(id: String).returns(Primitive::Entity) }
16
+ def retrieve(id); end
17
+ end
18
+ end
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Primitive
5
- VERSION = '1.0.0'
5
+ VERSION = '1.0.1'
6
6
  end
data/lib/primitive.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'sorbet-runtime-stub' unless defined?(T)
4
+ require 'sorbet-runtime'
5
+ require 'yaml'
5
6
 
7
+ require_relative 'primitive/compact_file'
6
8
  require_relative 'primitive/entity'
data/primitive.gemspec CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
 
27
27
  s.required_ruby_version = '>= 2.7.6'
28
28
 
29
- s.add_runtime_dependency('sorbet-runtime-stub')
29
+ s.add_runtime_dependency('sorbet-runtime')
30
30
 
31
31
  s.add_development_dependency('bundler-audit')
32
32
  s.add_development_dependency('guard-rspec')
@@ -40,6 +40,5 @@ Gem::Specification.new do |s|
40
40
  s.add_development_dependency('simplecov')
41
41
  s.add_development_dependency('simplecov-console')
42
42
  s.add_development_dependency('sorbet')
43
- s.add_development_dependency('sorbet-runtime')
44
43
  s.add_development_dependency('tapioca')
45
44
  end