jsonapi-resources-anchor 2.1.0 → 2.3.1

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: 4f39f4e1690cf63ea4437c270480ddb44170135ca973132c28be67a095add422
4
- data.tar.gz: 1af7acc60184d15bb7f2887ce3f906318f5a9ee05cf2fec6f2124dedb5fd9c61
3
+ metadata.gz: 64e06b2f2987b17e1c8d55c09f986e8f077497e38bed1f16a34e78d5efe9416a
4
+ data.tar.gz: 821a574d6e0df1d77184ba94b8ed0664d6db2ea25bbff31a0cc7fa4c477220eb
5
5
  SHA512:
6
- metadata.gz: f80462d3f298454a77222182d2fbb3d4a5c58ac3d441dc4108734c609f7630f80a466b32a2f7c729c0df1e15948e02a39b9c6c43be10908a9d38dd17d5f2d6c0
7
- data.tar.gz: bcaf6345537c377a37304caa122faa4bff143919ffcbe284b759c694ef4afc33d2f37ab0d31baa9c3e531063d5faf509fa9a95d9450796084cb1980b1775eacf
6
+ metadata.gz: f4bf643f1470c646a8b568dc81a17d392ad1db333ec704449497ca8210dfb81a87f78cc30071143e2be726c352fc2d2d6f4accf1fe5ff3d58f8bee5f4efd579c
7
+ data.tar.gz: '0381457aa5bcbb056ab9fb3ec2baf05f65504c54b0db2b7705bdcbb3490dd7b770997ff636a2a4dcbdda3443d97ffc0654a3fbdd4b3291c38ba87271f9dd3bdc'
@@ -158,7 +158,6 @@ module Anchor
158
158
  end
159
159
  end
160
160
 
161
- # rubocop:disable Layout/LineLength
162
161
  # @param rel [Relationship]
163
162
  # @param resource_klass [Anchor::Resource]
164
163
  # @param name [String, Symbol]
@@ -184,5 +183,4 @@ module Anchor
184
183
  wrapper.call(rel_type)
185
184
  end
186
185
  end
187
- # rubocop:enable Layout/LineLength
188
186
  end
@@ -24,14 +24,15 @@ module Anchor::TypeScript
24
24
  end
25
25
  end
26
26
 
27
- def initialize(definition)
27
+ def initialize(definition, extension: ".ts")
28
28
  @definition = definition
29
29
  @name = definition.name
30
30
  @object = definition.object
31
+ @extension = extension
31
32
  end
32
33
 
33
34
  def name
34
- "#{@definition.name}.ts"
35
+ "#{@definition.name}#{@extension}"
35
36
  end
36
37
 
37
38
  def to_code(manually_editable: false)
@@ -66,7 +67,7 @@ module Anchor::TypeScript
66
67
  def relationship_imports
67
68
  relationships_to_import
68
69
  .reject { |type| type.anchor_schema_name == @name }
69
- .map { |type| Import.new(file_name: "#{type.anchor_schema_name}.ts", type:) }
70
+ .map { |type| Import.new(file_name: "#{type.anchor_schema_name}#{@extension}", type:) }
70
71
  end
71
72
 
72
73
  def relationships_to_import
@@ -8,35 +8,47 @@ module Anchor::TypeScript
8
8
  new(...).call
9
9
  end
10
10
 
11
- def initialize(generator:, folder_path:, force: false, resource_file_extension: ".ts")
11
+ def initialize(generator:, folder_path:, force: false)
12
12
  @generator = generator
13
13
  @folder_path = folder_path
14
14
  @force = force
15
- @resource_file_extension = "." + resource_file_extension.sub(/^\./, "")
16
15
  end
17
16
 
18
17
  def call
19
18
  FileUtils.mkdir_p(@folder_path)
20
19
  results = @generator.call
21
- results.each { |result| save_result(result) }
20
+ modified_files = results.filter_map { |result| save_result(result) }
21
+ save_sha
22
+ modified_files
22
23
  end
23
24
 
24
25
  private
25
26
 
26
- def save_result(result)
27
- filename = if result.type == MultifileSchemaGenerator::FileType::RESOURCE
28
- resource_file_name(result.name)
29
- else
30
- result.name
31
- end
27
+ def sha_hash_path
28
+ Rails.root.join(@folder_path, "hash.json")
29
+ end
32
30
 
33
- path = Rails.root.join(@folder_path, filename)
31
+ def sha_hash
32
+ return @sha_hash if defined?(@sha_hash)
33
+
34
+ @sha_hash = File.exist?(sha_hash_path) ? JSON.parse(File.read(sha_hash_path)) : {}
35
+ end
36
+
37
+ def save_sha
38
+ File.open(sha_hash_path, "w") { |f| f.write(@generator.sha_hash.to_json) }
39
+ end
40
+
41
+ # @return [String, nil] file name of file that is written to. nil if not written to
42
+ def save_result(result)
43
+ path = Rails.root.join(@folder_path, result.name)
34
44
 
35
45
  if @force || !File.exist?(path)
36
46
  File.open(path, "w") { |f| f.write(result.text) }
37
- return
47
+ return result.name
38
48
  end
39
49
 
50
+ return if sha_hash[result.name] == Digest::SHA256.hexdigest(result.text)
51
+
40
52
  existing_content = File.read(path)
41
53
 
42
54
  new_content = if manually_editable?(existing_content)
@@ -49,10 +61,7 @@ module Anchor::TypeScript
49
61
  end
50
62
 
51
63
  File.open(path, "w") { |f| f.write(new_content) }
52
- end
53
-
54
- def resource_file_name(name)
55
- File.basename(name, ".ts") + @resource_file_extension
64
+ result.name
56
65
  end
57
66
 
58
67
  def manually_editable?(text)
@@ -7,16 +7,29 @@ module Anchor::TypeScript
7
7
  UTIL = "util"
8
8
  end
9
9
 
10
- def initialize(register:, context: {}, include_all_fields: false, exclude_fields: nil, manually_editable: true) # rubocop:disable Lint/MissingSuper
10
+ def initialize( # rubocop:disable Lint/MissingSuper
11
+ register:,
12
+ context: {},
13
+ include_all_fields: false,
14
+ exclude_fields: nil,
15
+ manually_editable: true,
16
+ resource_file_extension: ".ts"
17
+ )
11
18
  @register = register
12
19
  @context = context
13
20
  @include_all_fields = include_all_fields
14
21
  @exclude_fields = exclude_fields
15
22
  @manually_editable = manually_editable
23
+ @resource_file_extension = "." + resource_file_extension.sub(/^\./, "")
16
24
  end
17
25
 
18
26
  def call
19
- [shared_file] + resource_files
27
+ @call ||= [shared_file] + resource_files
28
+ end
29
+
30
+ # { res.name => hash(res.text) }
31
+ def sha_hash
32
+ @sha_hash ||= call.map { |res| [res.name, Digest::SHA256.hexdigest(res.text)] }.to_h
20
33
  end
21
34
 
22
35
  private
@@ -37,7 +50,7 @@ module Anchor::TypeScript
37
50
  exclude_fields: @exclude_fields.nil? ? [] : @exclude_fields[r.anchor_schema_name.to_sym],
38
51
  )
39
52
 
40
- file_structure = ::Anchor::TypeScript::FileStructure.new(definition)
53
+ file_structure = ::Anchor::TypeScript::FileStructure.new(definition, extension: @resource_file_extension)
41
54
  text = file_structure.to_code(manually_editable: @manually_editable)
42
55
  name = file_structure.name
43
56
  Result.new(name:, text:, type: FileType::RESOURCE)
@@ -1,8 +1,6 @@
1
1
  module Anchor::TypeScript
2
2
  class Serializer
3
3
  class << self
4
- # rubocop:disable Layout/LineLength
5
-
6
4
  def type_string(type, depth = 1)
7
5
  case type
8
6
  when Anchor::Types::String.singleton_class then "string"
@@ -23,7 +21,6 @@ module Anchor::TypeScript
23
21
  else raise RuntimeError
24
22
  end
25
23
  end
26
- # rubocop:enable Layout/LineLength
27
24
 
28
25
  private
29
26
 
@@ -1,8 +1,6 @@
1
1
  module Anchor::Types::Inference
2
2
  module ActiveRecord
3
3
  class << self
4
- # rubocop:disable Layout/LineLength
5
-
6
4
  # @return [Proc{Type => Type, Anchor::Types::Maybe<Type>, Anchor::Types::Array<Type>}]
7
5
  def wrapper_from_reflection(reflection)
8
6
  case reflection
@@ -14,7 +12,6 @@ module Anchor::Types::Inference
14
12
  else raise "#{reflection.class.name} not supported"
15
13
  end
16
14
  end
17
- # rubocop:enable Layout/LineLength
18
15
 
19
16
  private
20
17
 
@@ -1,3 +1,3 @@
1
1
  module Anchor
2
- VERSION = "2.1.0"
2
+ VERSION = "2.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources-anchor
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-15 00:00:00.000000000 Z
11
+ date: 2025-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-resources