chronicle-core 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a168e93bd8ec5d03b056c25bdfa2b500ba88bd98869121ac427c4e6452b2c51e
4
- data.tar.gz: 638310a4a890e3b9ece2d26a99403f78293975f257d05df065dd3107146fffea
3
+ metadata.gz: 21efb5e6a7bf0dfb29a8e4187dfdfddbac79e47b4a13f636cd7be6e77ff95eea
4
+ data.tar.gz: 1527959dfd39a7f4345d784f009d3f5aaf0e67008b31c1734fdf2e1aa0b782db
5
5
  SHA512:
6
- metadata.gz: 8462b795ea806166e4ca538042c91c16d22e641bcae19e7228714911c8604cace59efe8bea79ca4f7e582191c51b09d8c5b73b33ac34255ab04693e89d7ce85c
7
- data.tar.gz: b2ead2ca9b9cb9afddba562881cc9026488c4177c9da62e282bb1acb17a9e11e1ec7782269ac5093ca1143de83951d8db6906864e0803d58c858bc3ec0a7f3f4
6
+ metadata.gz: 10caca6156eb0ffac0cbfbc15fbcdf0b1edc289862f74f7dea019bbd57111acccd08d0490f1c893af022ad1337b0b90858f1b54d3b9940f1dca05459c3aced44
7
+ data.tar.gz: f780be776364b03063b033e5f39df64eddee82ff6643b0e75fd6840bf9e144742f2f123696163e76d90bee0dc3a33174988f9daaf6ac162e73eed8a5b6861233
@@ -0,0 +1,30 @@
1
+ name: Rspec
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Run specs on Ruby ${{ matrix.ruby_version }}
12
+ runs-on: ubuntu-latest
13
+
14
+ strategy:
15
+ matrix:
16
+ ruby_version: ['2.7', '3.0']
17
+ fail-fast: false
18
+
19
+ steps:
20
+ - name: Checkout code
21
+ uses: actions/checkout@v2
22
+
23
+ - name: Set up Ruby ${{ matrix.ruby_version }}
24
+ uses: ruby/setup-ruby@v1
25
+ with:
26
+ ruby-version: ${{ matrix.ruby_version }}
27
+ bundler-cache: true # This will automatically set up bundler and cache gems
28
+
29
+ - name: Run specs
30
+ run: bundle exec rspec
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Style/FrozenStringLiteralComment:
2
+ Enabled: false
3
+
4
+ StringLiterals:
5
+ Enabled: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Andrew Louis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -36,5 +36,7 @@ Gem::Specification.new do |spec|
36
36
 
37
37
  spec.add_development_dependency "guard-rspec", "~> 4.7.3"
38
38
  spec.add_development_dependency "pry-byebug", "~> 3.9"
39
+ spec.add_development_dependency "rubocop", "~> 1.25.1"
39
40
  spec.add_development_dependency "rspec", "~> 3.9"
41
+ spec.add_development_dependency "simplecov", "~> 0.21"
40
42
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Chronicle
4
4
  module Core
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
@@ -18,27 +18,17 @@ module Chronicle::Schema
18
18
  end
19
19
  end
20
20
 
21
+ def assign_attributes(attributes)
22
+ @properties.merge!(attributes)
23
+ end
24
+
21
25
  def identifier_hash
22
26
  {
23
- type: self.class::TYPE,
24
- id: @id
27
+ id: @id,
28
+ type: self.class::TYPE
25
29
  }.compact
26
30
  end
27
31
 
28
- def to_h_jsonapi
29
- identifier_hash.merge({
30
- attributes: attributes,
31
- relationships: associations.map do |k, v|
32
- if v.is_a?(Array)
33
- [k, { data: v.map(&:to_h_jsonapi) }]
34
- else
35
- [k, { data: v.to_h_jsonapi }]
36
- end
37
- end.to_h,
38
- }).merge(meta_hash)
39
- .compact
40
- end
41
-
42
32
  def attributes
43
33
  @properties.reject { |k, v| associations.keys.include?(k) }.compact
44
34
  end
@@ -53,47 +43,20 @@ module Chronicle::Schema
53
43
  end
54
44
  end
55
45
 
56
- def associations_hash
57
- associations.map do |k, v|
58
- if v.is_a?(Array)
59
- [k, v.map(&:to_h)]
60
- else
61
- [k, v.to_h]
62
- end
63
- end.to_h
64
- end
65
-
66
46
  def meta
67
47
  {
68
48
  dedupe_on: @dedupe_on.map{|d| d.map(&:to_s).join(",")}
69
49
  }
70
50
  end
71
51
 
72
- def meta_hash
73
- {
74
- meta: meta
75
- }
76
- end
77
-
78
- def identifier_hash
79
- {
80
- id: @id,
81
- type: self.class::TYPE
82
- }.compact
83
- end
84
-
85
- def to_h_flattened
86
- Chronicle::Utils::Hash.flatten_keys(to_h)
87
- end
88
-
89
52
  def to_h
90
53
  identifier_hash
91
54
  .merge(@properties)
92
- .merge(meta_hash)
55
+ .merge({ meta: meta })
93
56
  end
94
57
 
95
- def assign_attributes(attributes)
96
- @properties.merge!(attributes)
58
+ def to_h_flattened
59
+ Chronicle::Utils::Hash.flatten_keys(to_h)
97
60
  end
98
61
  end
99
62
  end
@@ -2,6 +2,8 @@ require 'dry/schema' # TODO: Lazy load this
2
2
  Dry::Schema.load_extensions(:hints)
3
3
  Dry::Schema.load_extensions(:info)
4
4
 
5
+ require 'chronicle/serialization'
6
+
5
7
  module Chronicle::Schema
6
8
  class Validator
7
9
  EntitySchema = Dry::Schema.Params do
@@ -33,14 +35,14 @@ module Chronicle::Schema
33
35
  end
34
36
  end
35
37
 
36
- def self.validate(input)
37
- case input
38
+ def self.validate(record)
39
+ case record
38
40
  when Entity
39
- result = EntitySchema.call(input.to_h_jsonapi)
41
+ result = EntitySchema.call(Chronicle::Serialization::JSONAPISerializer.serialize(record))
40
42
  when Activity
41
- result = ActivitySchema.call(input.to_h_jsonapi)
43
+ result = ActivitySchema.call(Chronicle::Serialization::JSONAPISerializer.serialize(record))
42
44
  else
43
- raise ArgumentError, "Unsupported type: #{input.class}"
45
+ raise ArgumentError, "Unsupported type: #{record.class}"
44
46
  end
45
47
 
46
48
  if result.success?
@@ -0,0 +1,7 @@
1
+ module Chronicle::Serialization
2
+ class HashSerializer < Chronicle::Serialization::Serializer
3
+ def serializable_hash
4
+ @record.to_h
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ module Chronicle::Serialization
2
+ class JSONAPISerializer < Chronicle::Serialization::Serializer
3
+ def serializable_hash
4
+ identifier_hash.merge({
5
+ attributes: attribute_hash,
6
+ relationships: associations_hash,
7
+ meta: @record.meta
8
+ })
9
+ end
10
+
11
+ private
12
+
13
+ def identifier_hash
14
+ {
15
+ type: @record.class::TYPE,
16
+ id: @record.id
17
+ }.compact
18
+ end
19
+
20
+ def attribute_hash
21
+ @record.attributes.compact
22
+ end
23
+
24
+ def associations_hash
25
+ @record.associations.map do |k, v|
26
+ if v.is_a?(Array)
27
+ [k, { data: v.map{|record| JSONAPISerializer.new(record).serializable_hash} }]
28
+ else
29
+ [k, { data: JSONAPISerializer.new(v).serializable_hash }]
30
+ end
31
+ end.to_h
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ module Chronicle::Serialization
2
+ class Serializer
3
+ # Construct a new instance of this serializer.
4
+ # == Parameters:
5
+ # options::
6
+ # Options for configuring this Serializers
7
+ def initialize(record, options = {})
8
+ raise(SerializationError, "Record must be a subclass of Chronicle::Schema::Base") unless record.is_a?(Chronicle::Schema::Base)
9
+
10
+ @record = record
11
+ @options = options
12
+ end
13
+
14
+ # Serialize a record as a hash
15
+ def serializable_hash
16
+ raise NotImplementedError
17
+ end
18
+
19
+ def self.serialize(record)
20
+ serializer = self.new(record)
21
+ serializer.serializable_hash
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ require "chronicle/schema"
2
+
3
+ module Chronicle
4
+ module Serialization
5
+ class Error < StandardError; end
6
+
7
+ class SerializationError < Error; end
8
+ end
9
+ end
10
+
11
+ require_relative "serialization/serializer"
12
+ require_relative "serialization/hash_serializer"
13
+ require_relative "serialization/jsonapi_serializer"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chronicle-core
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
  - Andrew Louis
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.25.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.25.1
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '3.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.21'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.21'
69
97
  description: Core libraries for Chronicle including models and schema definitions.
70
98
  email:
71
99
  - andrew@hyfen.net
@@ -73,11 +101,14 @@ executables: []
73
101
  extensions: []
74
102
  extra_rdoc_files: []
75
103
  files:
104
+ - ".github/workflows/rspec.yml"
76
105
  - ".gitignore"
77
106
  - ".rspec"
107
+ - ".rubocop.yml"
78
108
  - CODE_OF_CONDUCT.md
79
109
  - Gemfile
80
110
  - Guardfile
111
+ - LICENSE.txt
81
112
  - README.md
82
113
  - Rakefile
83
114
  - bin/console
@@ -91,6 +122,10 @@ files:
91
122
  - lib/chronicle/schema/entity.rb
92
123
  - lib/chronicle/schema/raw.rb
93
124
  - lib/chronicle/schema/validator.rb
125
+ - lib/chronicle/serialization.rb
126
+ - lib/chronicle/serialization/hash_serializer.rb
127
+ - lib/chronicle/serialization/jsonapi_serializer.rb
128
+ - lib/chronicle/serialization/serializer.rb
94
129
  - lib/chronicle/utils/hash.rb
95
130
  - sig/chronicle/core.rbs
96
131
  homepage: https://github.com/chronicle-app