core-state 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5ce4cb2ff3e96c75306c312db38fc09e9f95499d2e4d133d48093b38f64423b
4
- data.tar.gz: 04a0e78d72397976690d10d37431a6765809906a5c1708a7ac504bb20bf2d329
3
+ metadata.gz: 53361d09b54352ddbb50524bef81c5f6aba042e4770f1f8988a110e86f706b1d
4
+ data.tar.gz: 5d08a756908ce47e59bdfdd58ed96ddc71cbf12d148e44e1a0079724ceb0551b
5
5
  SHA512:
6
- metadata.gz: f03ad4241c69ef2d263741efc9ada4aecd984f8e699bf413ba503e8f0fe14174afcb6aa0a4ea602e7c39a68dbecd7da1d47418fa45c271c48dee822c78490871
7
- data.tar.gz: d4b2bbd0e812133cc774b399519e109c2b32787aa20866d134f3b55b2523cee40fdff7936ee3e70a49edd3ce56a5aacde2c6298a177801c59e2ec280e66f9d6f
6
+ metadata.gz: 8e575916779181a6cbe28a2a82333b5dea39cd41b7a2e863e25aa72fa1082115ef82fc8c60a0c9e9335f673362c18f0a9f1ca447070038995c36556b78d7a23a
7
+ data.tar.gz: 2d52fdd9fe0dba779271a9cbc095467fd98d705fe60f70edf35acab02b25537f9a5c4946d30b94e3c22222683d20af76e7e4179956bb1c4e5cc30e8e8ad3459a
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Core
4
4
  module State
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.2"
6
6
 
7
7
  def self.version
8
8
  VERSION
data/lib/is/stateful.rb CHANGED
@@ -1,16 +1,86 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "core/extension"
4
-
5
- require "refine/object/copy"
4
+ require "core/local"
6
5
 
7
6
  module Is
8
7
  # [public] Makes objects stateful.
9
8
  #
10
9
  module Stateful
10
+ # Adding core-copy as a dependency of core-state creates a recursive dependency, so just bundle it.
11
+ #
12
+ module Copy
13
+ DEFAULT = ::Object.new
14
+
15
+ refine ::Object do
16
+ # [public] Copies the object using `clone`.
17
+ #
18
+ if RbConfig::CONFIG["RUBY_PROGRAM_VERSION"] < "3"
19
+ def copy(freeze: DEFAULT)
20
+ should_freeze = resolve_freeze_argument(freeze)
21
+
22
+ value = clone(freeze: should_freeze)
23
+ value.freeze if should_freeze
24
+ value
25
+ end
26
+ else
27
+ def copy(freeze: DEFAULT)
28
+ clone(freeze: resolve_freeze_argument(freeze))
29
+ end
30
+ end
31
+
32
+ private def resolve_freeze_argument(value)
33
+ case value
34
+ when DEFAULT
35
+ frozen?
36
+ else
37
+ !!value
38
+ end
39
+ end
40
+ end
41
+
42
+ refine Array do
43
+ # [public] Copies the array, along with each value.
44
+ #
45
+ def copy(freeze: DEFAULT)
46
+ unless Stateful.copying?(self)
47
+ Stateful.prevent_recursion(self) do
48
+ array = map { |value|
49
+ value.copy(freeze: freeze)
50
+ }
51
+
52
+ array.freeze if resolve_freeze_argument(freeze)
53
+
54
+ array
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ refine Hash do
61
+ # [public] Copies the hash, along with each key and value.
62
+ #
63
+ def copy(freeze: DEFAULT)
64
+ unless Stateful.copying?(self)
65
+ Stateful.prevent_recursion(self) do
66
+ hash = {}
67
+
68
+ each_pair do |key, value|
69
+ hash[key.copy(freeze: freeze)] = value.copy(freeze: freeze)
70
+ end
71
+
72
+ hash.freeze if resolve_freeze_argument(freeze)
73
+
74
+ hash
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
11
81
  extend Is::Extension
12
82
 
13
- using Refine::Object::Copy
83
+ using Copy
14
84
 
15
85
  ALLOWED_FLAGS = %i[class instance].freeze
16
86
 
@@ -196,5 +266,25 @@ module Is
196
266
  @__defined_state_isolations ||= []
197
267
  end
198
268
  end
269
+
270
+ class << self
271
+ include Is::Localized
272
+
273
+ def prevent_recursion(object)
274
+ object_id = object.object_id
275
+ copied_objects[object_id] = true
276
+ yield
277
+ ensure
278
+ copied_objects.delete(object_id)
279
+ end
280
+
281
+ def copying?(object)
282
+ copied_objects[object.object_id]
283
+ end
284
+
285
+ def copied_objects
286
+ localized(:__corerb_copied_objects) || localize(:__corerb_copied_objects, {})
287
+ end
288
+ end
199
289
  end
200
290
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: core-state
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Powell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-15 00:00:00.000000000 Z
11
+ date: 2021-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: core-copy
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '0.0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '0.0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: core-extension
29
15
  requirement: !ruby/object:Gem::Requirement