xamidimura 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e4794d563170d4e8044b61886a26dd0410ad329b0462c1daaf680fa994b72526
4
+ data.tar.gz: 448b8c2f55e29a185bda32f5068de0894124a687b45ebd745a99a16d3bb96792
5
+ SHA512:
6
+ metadata.gz: d5af662e534c95e5ff7be4e91a77cd57fb8538aeb1a1abfc644a5623be5982fabcde5856f52566a84d2de79f2c5e5f4c5a87be4b5c02bdea5550131b5ff2ce43
7
+ data.tar.gz: 61f3560994e263cf249a8da92528b2e6158758b2d052e12e6b4bf71a650ca36b10a3a0919341cb6063a83866b04f5ffe30a77ca8d833ecc02d073505fb3e2632
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 - 2026-09-23
4
+
5
+ - Initial release.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yudai Takada
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # Xamidimura
2
+
3
+ Xamidimura provides the shared file-revision contract used by Canopus, Hadar,
4
+ and Rukbat. It reads a stable byte snapshot, records a filesystem signature,
5
+ and computes a SHA-256 revision for optimistic save-conflict checks.
6
+
7
+ The gem is named for μ¹ Scorpii: Xamidimura is an IAU-adopted Khoekhoe name
8
+ derived from *xami di mura*, “eyes of the beast” ([All Skies Encyclopaedia](https://xing.fmi.uni-jena.de/mediawiki/index.php/Xamidimura)).
9
+
10
+ ```ruby
11
+ snapshot = Xamidimura::SourceRevision.read("notes.md", reject_symlink: true)
12
+ snapshot.bytes # exact bytes read
13
+ snapshot.signature # stable filesystem signature
14
+ snapshot.digest # SHA-256 of those bytes
15
+ ```
16
+
17
+ `read` retries if the file changes during the read. Use `signature` for cheap
18
+ change checks, `digest` for bytes already in memory, and `file_digest` to hash
19
+ a file without loading it all into memory.
20
+
21
+ Xamidimura requires Ruby 3.2 or newer and has no runtime dependencies.
@@ -0,0 +1,16 @@
1
+ # ADR-NNN: Title
2
+
3
+ - Status: Proposed
4
+ - Date: YYYY-MM-DD
5
+
6
+ ## Context
7
+
8
+ What decision needs to be made?
9
+
10
+ ## Decision
11
+
12
+ What did we decide?
13
+
14
+ ## Consequences
15
+
16
+ What becomes easier or harder as a result?
@@ -0,0 +1,29 @@
1
+ # ADR-001: Share source revision checks, not application policy
2
+
3
+ - Status: Accepted
4
+ - Date: 2026-09-23
5
+
6
+ ## Context
7
+
8
+ Canopus, Hadar, and Rukbat each prevent an application save from silently
9
+ overwriting external edits, but they use different policies: Canopus and Rukbat
10
+ compare content digests, while Hadar also uses filesystem signatures to avoid
11
+ reading a deck on every poll. Their settings, commands, keymaps, and themes do
12
+ not share an implementation contract.
13
+
14
+ The gem is named for μ¹ Scorpii (Xamidimura), an IAU-adopted Khoekhoe name
15
+ derived from *xami di mura*, “eyes of the beast.”
16
+
17
+ ## Decision
18
+
19
+ Xamidimura owns only stable byte snapshots, filesystem signatures, and SHA-256
20
+ content revisions. Application-specific reload, save, and conflict behavior
21
+ stays in each application. Do not move settings, keymaps, command registries,
22
+ theme loading, or file watchers here without a demonstrated three-application
23
+ contract.
24
+
25
+ ## Consequences
26
+
27
+ The three applications can agree on how a source revision is observed without
28
+ coupling their editing models or conflict messages. The gem remains small; its
29
+ scope can expand only when another shared behavior is proven by real use.
@@ -0,0 +1,5 @@
1
+ # Architecture decision records
2
+
3
+ ADRs record decisions that affect Xamidimura's public API or its role in the
4
+ application ecosystem. Accepted decisions are append-only; supersede them with
5
+ a new record rather than rewriting history.
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+
5
+ module Xamidimura
6
+ class SourceRevision
7
+ Snapshot = Data.define(:bytes, :signature, :digest)
8
+
9
+ def self.digest(bytes)
10
+ raise TypeError, "source bytes must be a String" unless bytes.is_a?(String)
11
+
12
+ Digest::SHA256.hexdigest(bytes)
13
+ end
14
+
15
+ def self.file_digest(path)
16
+ raise TypeError, "path must be a String" unless path.is_a?(String)
17
+
18
+ Digest::SHA256.file(path).hexdigest
19
+ rescue SystemCallError => error
20
+ raise Error, error.message
21
+ end
22
+
23
+ def self.signature(path, reject_symlink: false)
24
+ raise TypeError, "path must be a String" unless path.is_a?(String)
25
+ unless reject_symlink == true || reject_symlink == false
26
+ raise ArgumentError, "reject_symlink must be true or false"
27
+ end
28
+
29
+ stat = reject_symlink ? File.lstat(path) : File.stat(path)
30
+ raise Error, "source path is not a regular file" unless stat.file? && !(reject_symlink && stat.symlink?)
31
+
32
+ [stat.dev, stat.ino, stat.size, stat.mtime.to_i, stat.mtime.nsec,
33
+ stat.ctime.to_i, stat.ctime.nsec].freeze
34
+ rescue SystemCallError => error
35
+ raise Error, error.message
36
+ end
37
+
38
+ def self.read(path, attempts: 3, reject_symlink: false)
39
+ unless attempts.is_a?(Integer) && attempts.between?(1, 10)
40
+ raise ArgumentError, "attempts must be an integer between 1 and 10"
41
+ end
42
+
43
+ attempts.times do
44
+ before = signature(path, reject_symlink: reject_symlink)
45
+ bytes = File.binread(path)
46
+ after = signature(path, reject_symlink: reject_symlink)
47
+ next unless before == after
48
+
49
+ return Snapshot.new(bytes.freeze, after, digest(bytes).freeze)
50
+ end
51
+ raise Error, "source file changed while it was being read; retry"
52
+ rescue SystemCallError => error
53
+ raise Error, error.message
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xamidimura
4
+ VERSION = "0.1.0"
5
+ end
data/lib/xamidimura.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "xamidimura/version"
4
+ require_relative "xamidimura/source_revision"
5
+
6
+ module Xamidimura
7
+ class Error < StandardError; end
8
+ end
@@ -0,0 +1,17 @@
1
+ module Xamidimura
2
+ class Error < StandardError
3
+ end
4
+
5
+ class SourceRevision
6
+ class Snapshot
7
+ attr_reader bytes: String
8
+ attr_reader signature: [Integer, Integer, Integer, Integer, Integer, Integer, Integer]
9
+ attr_reader digest: String
10
+ end
11
+
12
+ def self.digest: (String bytes) -> String
13
+ def self.file_digest: (String path) -> String
14
+ def self.signature: (String path, ?reject_symlink: bool) -> [Integer, Integer, Integer, Integer, Integer, Integer, Integer]
15
+ def self.read: (String path, ?attempts: Integer, ?reject_symlink: bool) -> Snapshot
16
+ end
17
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test_helper"
4
+ require "minitest/mock"
5
+
6
+ class SourceRevisionTest < Minitest::Test
7
+ def test_read_returns_bytes_digest_and_stable_signature
8
+ Dir.mktmpdir do |directory|
9
+ path = File.join(directory, "source.md")
10
+ File.binwrite(path, "hello\n")
11
+
12
+ snapshot = Xamidimura::SourceRevision.read(path)
13
+
14
+ assert_equal "hello\n", snapshot.bytes
15
+ assert_equal Xamidimura::SourceRevision.digest(snapshot.bytes), snapshot.digest
16
+ assert_equal snapshot.digest, Xamidimura::SourceRevision.file_digest(path)
17
+ assert_equal Xamidimura::SourceRevision.signature(path), snapshot.signature
18
+ assert_predicate snapshot.bytes, :frozen?
19
+ assert_predicate snapshot.signature, :frozen?
20
+ assert_predicate snapshot.digest, :frozen?
21
+ end
22
+ end
23
+
24
+ def test_reject_symlink_and_invalid_arguments
25
+ Dir.mktmpdir do |directory|
26
+ path = File.join(directory, "source")
27
+ link = File.join(directory, "link")
28
+ File.write(path, "bytes")
29
+ File.symlink(path, link)
30
+
31
+ assert_raises(Xamidimura::Error) { Xamidimura::SourceRevision.signature(link, reject_symlink: true) }
32
+ assert_raises(Xamidimura::Error) { Xamidimura::SourceRevision.read(link, reject_symlink: true) }
33
+ assert_raises(ArgumentError) { Xamidimura::SourceRevision.signature(path, reject_symlink: :yes) }
34
+ assert_raises(TypeError) { Xamidimura::SourceRevision.signature(Object.new) }
35
+ [0, 11, 1.5].each do |attempts|
36
+ assert_raises(ArgumentError) { Xamidimura::SourceRevision.read(path, attempts: attempts) }
37
+ end
38
+ end
39
+ end
40
+
41
+ def test_read_retries_if_the_file_changes_during_the_read
42
+ Dir.mktmpdir do |directory|
43
+ path = File.join(directory, "source")
44
+ File.write(path, "before")
45
+ original_read = File.method(:binread)
46
+ change_after_first_read = true
47
+
48
+ snapshot = File.stub(:binread, ->(target, *arguments) {
49
+ bytes = original_read.call(target, *arguments)
50
+ if change_after_first_read
51
+ change_after_first_read = false
52
+ File.write(path, "after")
53
+ end
54
+ bytes
55
+ }) do
56
+ Xamidimura::SourceRevision.read(path)
57
+ end
58
+
59
+ assert_equal "after", snapshot.bytes
60
+ assert_equal Xamidimura::SourceRevision.signature(path), snapshot.signature
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest/autorun"
4
+ require "tmpdir"
5
+ require "xamidimura"
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xamidimura
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yudai Takada
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Stable file snapshots and SHA-256 revisions for conflict-safe application
13
+ saves.
14
+ email:
15
+ - t.yudai92@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - docs/adr/000-template.md
24
+ - docs/adr/001-source-revision.md
25
+ - docs/adr/README.md
26
+ - lib/xamidimura.rb
27
+ - lib/xamidimura/source_revision.rb
28
+ - lib/xamidimura/version.rb
29
+ - sig/xamidimura.rbs
30
+ - test/source_revision_test.rb
31
+ - test/test_helper.rb
32
+ homepage: https://github.com/noxdea/xamidimura
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ source_code_uri: https://github.com/noxdea/xamidimura/tree/main
37
+ changelog_uri: https://github.com/noxdea/xamidimura/blob/main/CHANGELOG.md
38
+ allowed_push_host: https://rubygems.org
39
+ rubygems_mfa_required: 'true'
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 4.0.16
55
+ specification_version: 4
56
+ summary: Shared source revision primitives for Ruby desktop applications
57
+ test_files: []