persistent-dmnd 1.0.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.
@@ -0,0 +1,74 @@
1
+ # encoding: UTF-8
2
+
3
+ # Persistent-๐Ÿ’Ž: Ruby gem for easily creating immutable data structures
4
+ # Copyright (c) 2017 Ivo Anjo <ivo.anjo@ist.utl.pt>
5
+ #
6
+ # This file is part of Persistent-๐Ÿ’Ž.
7
+ #
8
+ # MIT License
9
+ #
10
+ # Copyright (c) 2017 Ivo Anjo
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to deal
14
+ # in the Software without restriction, including without limitation the rights
15
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ # copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in all
20
+ # copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ # SOFTWARE.
29
+
30
+ # frozen_string_literal: true
31
+
32
+ require 'persistent_dmnd/ruby_1_9_and_2_0_support'
33
+
34
+ require 'hamster'
35
+
36
+ module Persistent๐Ÿ’Ž
37
+ class Dmndifier
38
+ extend Persistent๐Ÿ’Ž
39
+
40
+ DEFAULT_OPTIONS = {
41
+ on_unknown: proc { |arg|
42
+ raise ArgumentError, "Could not ๐Ÿ’Žify an object of class #{arg.class}. Maybe you need to implement :to_๐Ÿ’Ž?"
43
+ }
44
+ }.freeze
45
+
46
+ private_constant :DEFAULT_OPTIONS
47
+
48
+ def self.[](arg, options = DEFAULT_OPTIONS)
49
+ options = DEFAULT_OPTIONS.merge(options)
50
+ case
51
+ when arg.respond_to?(:to_๐Ÿ’Ž)
52
+ arg.to_๐Ÿ’Ž
53
+ when arg.respond_to?(:to_dmnd)
54
+ arg.to_dmnd
55
+ when arg.respond_to?(:to_hash)
56
+ h๐Ÿ’Ž[arg.to_hash]
57
+ when arg.is_a?(Hamster::Set) || arg.is_a?(Hamster::SortedSet)
58
+ s๐Ÿ’Ž[*arg.to_a]
59
+ when arg.respond_to?(:to_ary)
60
+ a๐Ÿ’Ž[*arg.to_ary]
61
+ when defined?(Concurrent::Tuple) && arg.is_a?(Concurrent::Tuple)
62
+ a๐Ÿ’Ž[*arg.to_a]
63
+ when arg.respond_to?(:to_set)
64
+ s๐Ÿ’Ž[*arg.to_set.to_a]
65
+ when defined?(Concurrent::Map) && arg.is_a?(Concurrent::Map)
66
+ h๐Ÿ’Ž[Persistent๐Ÿ’Ž::Ruby19And20Support.enumerable_to_h(arg.enum_for(:each_pair))]
67
+ when arg.respond_to?(:each_pair)
68
+ h๐Ÿ’Ž[Persistent๐Ÿ’Ž::Ruby19And20Support.enumerable_to_h(arg.each_pair)]
69
+ else
70
+ options.fetch(:on_unknown).call(arg)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: UTF-8
2
+
3
+ # Persistent-๐Ÿ’Ž: Ruby gem for easily creating immutable data structures
4
+ # Copyright (c) 2017 Ivo Anjo <ivo.anjo@ist.utl.pt>
5
+ #
6
+ # This file is part of Persistent-๐Ÿ’Ž.
7
+ #
8
+ # MIT License
9
+ #
10
+ # Copyright (c) 2017 Ivo Anjo
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to deal
14
+ # in the Software without restriction, including without limitation the rights
15
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ # copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in all
20
+ # copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ # SOFTWARE.
29
+
30
+ # frozen_string_literal: true
31
+
32
+ require 'persistent-๐Ÿ’Ž'
33
+
34
+ # Add our module to the main object, allowing our syntax to be used everywhere
35
+ include Persistent๐Ÿ’Ž
@@ -0,0 +1,125 @@
1
+ # encoding: UTF-8
2
+
3
+ # Persistent-๐Ÿ’Ž: Ruby gem for easily creating immutable data structures
4
+ # Copyright (c) 2017 Ivo Anjo <ivo.anjo@ist.utl.pt>
5
+ #
6
+ # This file is part of Persistent-๐Ÿ’Ž.
7
+ #
8
+ # MIT License
9
+ #
10
+ # Copyright (c) 2017 Ivo Anjo
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to deal
14
+ # in the Software without restriction, including without limitation the rights
15
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ # copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in all
20
+ # copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ # SOFTWARE.
29
+
30
+ # frozen_string_literal: true
31
+
32
+ require 'persistent_dmnd/self_conversion'
33
+ require 'persistent_dmnd/is_persistent'
34
+ require 'persistent_dmnd/concurrent_ruby_support'
35
+ require 'persistent_dmnd/jruby_workaround'
36
+
37
+ require 'hamster'
38
+ require 'set'
39
+
40
+ module Persistent๐Ÿ’Ž
41
+ class Hash < Hamster::Hash
42
+ include SelfConversion
43
+ include IsPersistent
44
+ include JRubyWorkaround
45
+ include Persistent๐Ÿ’Ž
46
+
47
+ # Hashes are not arrays and thus should not implement :to_ary
48
+ undef_method :to_ary
49
+
50
+ # Return Concurrent::Hash with contents of Persistent๐Ÿ’Ž::Hash
51
+ #
52
+ # @example
53
+ # my_hash = h๐Ÿ’Ž[hello: :world]
54
+ # my_concurrent_hash = my_hash.to_concurrent_hash
55
+ #
56
+ def to_concurrent_hash
57
+ ConcurrentRubySupport::ensure_concurrent_ruby_loaded
58
+ Concurrent::Hash[self]
59
+ end
60
+ alias :to_concurrent :to_concurrent_hash
61
+
62
+ # Return Concurrent::Map with contents of Persistent๐Ÿ’Ž::Hash
63
+ #
64
+ # @example
65
+ # my_hash = h๐Ÿ’Ž[hello: :world]
66
+ # my_concurrent_map = my_hash.to_concurrent_map
67
+ # # => #<Concurrent::Map:0x0055ad9b283ea0 entries=1 default_proc=nil>
68
+ #
69
+ def to_concurrent_map
70
+ ConcurrentRubySupport::ensure_concurrent_ruby_loaded
71
+ each_with_object(Concurrent::Map.new(initial_capacity: size)) do |(key, value), result|
72
+ result[key] = value
73
+ end
74
+ end
75
+
76
+ def to_set
77
+ ::Set.new(self)
78
+ end
79
+
80
+ def <(other)
81
+ if size >= other.size
82
+ false
83
+ else
84
+ self <= other
85
+ end
86
+ end
87
+
88
+ def <=(other)
89
+ if size > other.size
90
+ false
91
+ else
92
+ each do |key, value|
93
+ return false if other[key] != value
94
+ end
95
+
96
+ true
97
+ end
98
+ end
99
+
100
+ # Return each entry as a key, value pair inside an immutable array
101
+ def each๐Ÿ’Ž
102
+ if block_given?
103
+ each { |pair| yield a๐Ÿ’Ž[*pair] }
104
+ else
105
+ enum_for(:each๐Ÿ’Ž)
106
+ end
107
+ end
108
+ alias_method :eachDmnd, :each๐Ÿ’Ž
109
+
110
+ def to_a๐Ÿ’Ž
111
+ a๐Ÿ’Ž[*each๐Ÿ’Ž]
112
+ end
113
+ alias_method :to_aDmnd, :to_a๐Ÿ’Ž
114
+
115
+ def to_h๐Ÿ’Ž
116
+ self
117
+ end
118
+ alias_method :to_hDmnd, :to_h๐Ÿ’Ž
119
+
120
+ def to_s๐Ÿ’Ž
121
+ s๐Ÿ’Ž[*each๐Ÿ’Ž]
122
+ end
123
+ alias_method :to_sDmnd, :to_s๐Ÿ’Ž
124
+ end
125
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+
3
+ # Persistent-๐Ÿ’Ž: Ruby gem for easily creating immutable data structures
4
+ # Copyright (c) 2017 Ivo Anjo <ivo.anjo@ist.utl.pt>
5
+ #
6
+ # This file is part of Persistent-๐Ÿ’Ž.
7
+ #
8
+ # MIT License
9
+ #
10
+ # Copyright (c) 2017 Ivo Anjo
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to deal
14
+ # in the Software without restriction, including without limitation the rights
15
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ # copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in all
20
+ # copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ # SOFTWARE.
29
+
30
+ # frozen_string_literal: true
31
+
32
+ module Persistent๐Ÿ’Ž
33
+ # Marks instances as being persistent
34
+ module IsPersistent
35
+ def persistent?
36
+ true
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,107 @@
1
+ # encoding: UTF-8
2
+
3
+ # Persistent-๐Ÿ’Ž: Ruby gem for easily creating immutable data structures
4
+ # Copyright (c) 2017 Ivo Anjo <ivo.anjo@ist.utl.pt>
5
+ #
6
+ # This file is part of Persistent-๐Ÿ’Ž.
7
+ #
8
+ # MIT License
9
+ #
10
+ # Copyright (c) 2017 Ivo Anjo
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to deal
14
+ # in the Software without restriction, including without limitation the rights
15
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ # copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in all
20
+ # copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ # SOFTWARE.
29
+
30
+ # frozen_string_literal: true
31
+
32
+ module Persistent๐Ÿ’Ž
33
+ module JRubyWorkaround
34
+ if RUBY_PLATFORM == 'java' # Only supposed to do something on JRuby
35
+
36
+ BROKEN_ENCODING = "\x00"
37
+ private_constant :BROKEN_ENCODING
38
+
39
+ BROKEN_ENCODING_JRUBY_1_7 = [239, 146, 142].pack('c*').force_encoding('UTF-8')
40
+ private_constant :BROKEN_ENCODING_JRUBY_1_7
41
+
42
+ def self.included(klass)
43
+ # Make methods on this module also available inside classes (e.g. not just in instances)
44
+ klass.extend(self)
45
+ end
46
+
47
+ def method_added(name)
48
+ super
49
+ maybe_fix_broken_encoding(name)
50
+ end
51
+
52
+ def singleton_method_added(name)
53
+ super
54
+ maybe_fix_broken_encoding(name, self.singleton_class)
55
+ end
56
+
57
+ private
58
+
59
+ # Workaround JRuby encoding bug
60
+ # See https://github.com/jruby/jruby/issues/4878
61
+ # and https://github.com/ivoanjo/persistent-dmnd/issues/5
62
+ #
63
+ # The trick here is that on JRuby, methods with emojis in the name are defined, but the resulting name is mangled
64
+ # so instead of for instance #to_๐Ÿ’Ž the result is #to_?\x00. (Or #๐Ÿ’Žify will become #?ify\x00.) This behavior is
65
+ # consistent between def and calling a method, so def will register a method #to_?\x00, and a normal method call
66
+ # will do the same mangling and thus look for a method **WITH** the mangled name.
67
+ #
68
+ # But there are still other places in the code that rely on the correct name, such as alias and public_send/send,
69
+ # so we additionally use this module to define the correct name as an alias for the broken name.
70
+ #
71
+ # Thus, it all works on JRuby, albeit in a very roundabout way!
72
+ #
73
+ # FOR JRUBY 1.7:
74
+ # On JRuby 1.7 it seems that the mangling is different, and the emoji just gets replaced with ?, so for
75
+ # instance #to_๐Ÿ’Ž becomes #to_?. But then, to make things interesting, on symbols created without quotes, it
76
+ # gets mangled with the BROKEN_ENCODING_JRUBY_1_7, e.g :to_๐Ÿ’Ž becomes :to_BROKEN_ENCODING_JRUBY_1_7 (but
77
+ # :"to_๐Ÿ’Ž" is still correct).
78
+ # To support both of these, we add an extra alias on JRuby, so that everything else seems to work even if it is
79
+ # actually using the name with the mangled encoding.
80
+ #
81
+ def maybe_fix_broken_encoding(name, target = nil)
82
+ name_string = name.to_s
83
+
84
+ if JRUBY_VERSION.start_with?('1.7.')
85
+ unless name_string.match(/\?.+/) ||
86
+ ['a?', 'h?', 's?', 'each?'].include?(name_string) ||
87
+ ['_?', '_a?', '_h?', '_s?'].any? { |suffix| name_string.end_with?(suffix) }
88
+ return
89
+ end
90
+ else
91
+ return unless name_string.end_with?(BROKEN_ENCODING)
92
+ end
93
+
94
+ fixed_name = name_string.sub('?', '๐Ÿ’Ž').delete(BROKEN_ENCODING).to_sym
95
+ fixed_name_jruby_1_7 = name_string.sub('?', BROKEN_ENCODING_JRUBY_1_7).to_sym
96
+
97
+ if target
98
+ target.instance_eval { alias_method(fixed_name, name) }
99
+ target.instance_eval { alias_method(fixed_name_jruby_1_7, name) } if JRUBY_VERSION.start_with?('1.7.')
100
+ else
101
+ alias_method(fixed_name, name)
102
+ alias_method(fixed_name_jruby_1_7, name) if JRUBY_VERSION.start_with?('1.7.')
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+
3
+ # Persistent-๐Ÿ’Ž: Ruby gem for easily creating immutable data structures
4
+ # Copyright (c) 2017 Ivo Anjo <ivo.anjo@ist.utl.pt>
5
+ #
6
+ # This file is part of Persistent-๐Ÿ’Ž.
7
+ #
8
+ # MIT License
9
+ #
10
+ # Copyright (c) 2017 Ivo Anjo
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to deal
14
+ # in the Software without restriction, including without limitation the rights
15
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ # copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in all
20
+ # copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ # SOFTWARE.
29
+
30
+ # frozen_string_literal: true
31
+
32
+ module Persistent๐Ÿ’Ž
33
+ module Ruby19And20Support
34
+ # Enumerable#to_h was only introduced in Ruby 2.1 so we have to do it by hand on 2.0/1.9
35
+ if RUBY_VERSION.start_with?('2.0.') || RUBY_VERSION.start_with?('1.9.')
36
+ def self.enumerable_to_h(enumerable)
37
+ enumerable.each_with_object({}) do |(key, value), result|
38
+ result[key] = value
39
+ end
40
+ end
41
+ else
42
+ def self.enumerable_to_h(enumerable)
43
+ enumerable.to_h
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: UTF-8
2
+
3
+ # Persistent-๐Ÿ’Ž: Ruby gem for easily creating immutable data structures
4
+ # Copyright (c) 2017 Ivo Anjo <ivo.anjo@ist.utl.pt>
5
+ #
6
+ # This file is part of Persistent-๐Ÿ’Ž.
7
+ #
8
+ # MIT License
9
+ #
10
+ # Copyright (c) 2017 Ivo Anjo
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to deal
14
+ # in the Software without restriction, including without limitation the rights
15
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ # copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in all
20
+ # copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ # SOFTWARE.
29
+
30
+ # frozen_string_literal: true
31
+
32
+ require 'persistent_dmnd/jruby_workaround'
33
+
34
+ module Persistent๐Ÿ’Ž
35
+ # Implements trivial conversion to persistent data structures for our own classes
36
+ module SelfConversion
37
+ include JRubyWorkaround
38
+
39
+ def to_๐Ÿ’Ž
40
+ self
41
+ end
42
+ alias_method :to_dmnd, :to_๐Ÿ’Ž
43
+ end
44
+ end