• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-concurrency / concurrent-ruby / #2835

05 Oct 2014 10:16PM UTC coverage: 45.201% (-49.6%) from 94.81%
#2835

push

jdantonio
Merge pull request #158 from obrok/promise-composition

Promise composition

2 of 15 new or added lines in 1 file covered. (13.33%)

1514 existing lines in 84 files now uncovered.

1375 of 3042 relevant lines covered (45.2%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.67
/lib/concurrent/dereferenceable.rb
1
module Concurrent
1✔
2

3
  # Object references in Ruby are mutable. This can lead to serious problems when
4
  # the `#value` of a concurrent object is a mutable reference. Which is always the
5
  # case unless the value is a `Fixnum`, `Symbol`, or similar "primitive" data type.
6
  # Most classes in this library that expose a `#value` getter method do so using
7
  # this mixin module.
8
  module Dereferenceable
1✔
9

10
    # Return the value this object represents after applying the options specified
11
    # by the `#set_deref_options` method.
12
    #
13
    # When multiple deref options are set the order of operations is strictly defined.
14
    # The order of deref operations is:
15
    # * `:copy_on_deref`
16
    # * `:dup_on_deref`
17
    # * `:freeze_on_deref`
18
    #
19
    # Because of this ordering there is no need to `#freeze` an object created by a
20
    # provided `:copy_on_deref` block. Simply set `:freeze_on_deref` to `true`.
21
    # Setting both `:dup_on_deref` to `true` and `:freeze_on_deref` to `true` is
22
    # as close to the behavior of a "pure" functional language (like Erlang, Clojure,
23
    # or Haskell) as we are likely to get in Ruby.
24
    # 
25
    # This method is thread-safe and synchronized with the internal `#mutex`.
26
    #
27
    # @return [Object] the current value of the object
28
    def value
1✔
29
      mutex.lock
10✔
30
      apply_deref_options(@value)
10✔
31
    ensure
32
      mutex.unlock
10✔
33
    end
34

35
    alias_method :deref, :value
1✔
36

37
    protected
1✔
38

39
    # Set the internal value of this object
40
    #
41
    # @param [Object] val the new value
42
    def value=(val)
1✔
UNCOV
43
      mutex.lock
×
UNCOV
44
      @value = val
×
45
    ensure
UNCOV
46
      mutex.unlock
×
47
    end
48

49
    # A mutex lock used for synchronizing thread-safe operations. Methods defined
50
    # by `Dereferenceable` are synchronized using the `Mutex` returned from this
51
    # method. Operations performed by the including class that operate on the
52
    # `@value` instance variable should be locked with this `Mutex`.
53
    #
54
    # @return [Mutex] the synchronization object
55
    def mutex
1✔
56
      @mutex
80✔
57
    end
58

59
    # Initializes the internal `Mutex`. 
60
    #
61
    # @note This method *must* be called from within the constructor of the including class.
62
    #
63
    # @see #mutex
64
    def init_mutex
1✔
65
      @mutex = Mutex.new
6✔
66
    end
67

68
    # Set the options which define the operations #value performs before
69
    # returning data to the caller (dereferencing).
70
    #
71
    # @note Most classes that include this module will call `#set_deref_options`
72
    # from within the constructor, thus allowing these options to be set at
73
    # object creation.
74
    #
75
    # @param [Hash] opts the options defining dereference behavior.
76
    # @option opts [String] :dup_on_deref (false) call `#dup` before returning the data
77
    # @option opts [String] :freeze_on_deref (false) call `#freeze` before returning the data
78
    # @option opts [String] :copy_on_deref (nil) call the given `Proc` passing the internal value and
79
    #   returning the value returned from the proc
80
    def set_deref_options(opts = {})
1✔
81
      mutex.lock
6✔
82
      @dup_on_deref = opts[:dup_on_deref] || opts[:dup]
6✔
83
      @freeze_on_deref = opts[:freeze_on_deref] || opts[:freeze]
6✔
84
      @copy_on_deref = opts[:copy_on_deref] || opts[:copy]
6✔
85
      @do_nothing_on_deref = !(@dup_on_deref || @freeze_on_deref || @copy_on_deref)
6✔
86
      nil
87
    ensure
88
      mutex.unlock
6✔
89
    end
90

91
    # @!visibility private
92
    def apply_deref_options(value) # :nodoc:
1✔
93
      return nil if value.nil?
10✔
94
      return value if @do_nothing_on_deref
10✔
UNCOV
95
      value = @copy_on_deref.call(value) if @copy_on_deref
×
UNCOV
96
      value = value.dup if @dup_on_deref
×
UNCOV
97
      value = value.freeze if @freeze_on_deref
×
UNCOV
98
      value
×
99
    end
100
  end
101
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc