File: read_copy_update.rb

package info (click to toggle)
ruby-hamster 3.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,932 kB
  • sloc: ruby: 16,915; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 668 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require "forwardable"
require "thread"

module Hamster
  # @private
  module ReadCopyUpdate
    extend Forwardable

    def initialize(content)
      @content = content
      @lock = Mutex.new
    end

    def eql?(other)
      instance_of?(other.class) && @content.eql?(other.instance_variable_get(:@content))
    end
    alias :== :eql?

    def_delegator :@content, :inspect
    def_delegator :@content, :to_s

    protected

    def transform
      @lock.synchronize do
        @content = yield(@content)
      end
      self
    end

    private

    def method_missing(name, *args, &block)
      @content.send(name, *args, &block) rescue super
    end
  end
end