core-state 0.1.1 → 0.1.5

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: db495761cd564d756fb99e985b29d4e8176c532d605724a321670cfa8888e4b9
4
+ data.tar.gz: 96f7b2337e73e82a7ea5735a98e7b6a7055b9098656248e4c658584d5eb074b7
5
5
  SHA512:
6
- metadata.gz: f03ad4241c69ef2d263741efc9ada4aecd984f8e699bf413ba503e8f0fe14174afcb6aa0a4ea602e7c39a68dbecd7da1d47418fa45c271c48dee822c78490871
7
- data.tar.gz: d4b2bbd0e812133cc774b399519e109c2b32787aa20866d134f3b55b2523cee40fdff7936ee3e70a49edd3ce56a5aacde2c6298a177801c59e2ec280e66f9d6f
6
+ metadata.gz: fe3dfb3399854e9ffd96cd257b195b545cd31cda25d95db4af6be40c17890653322a015881672c75df64d4a451d4c11cf7004b90aa62cb83937d29de90d1ea9a
7
+ data.tar.gz: 229caf7f8a616ed01498420f688f6c8366663684791be54581e074836f035d52c1cd5d2123c953da2ac861bbf703984b0324fbbbd2d0b4142989cb20a4175c7d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## [v0.1.5](https://github.com/metabahn/corerb/releases/tag/2021-10-22)
2
+
3
+ *released on 2021-10-22*
4
+
5
+ * `fix` [#88](https://github.com/metabahn/corerb/pull/88) Fix an issue causing state to be shared between sibling subclasses ([bryanp](https://github.com/bryanp))
6
+
7
+ ## [v0.1.4](https://github.com/metabahn/corerb/releases/tag/2021-09-30)
8
+
9
+ *released on 2021-09-30*
10
+
11
+ * `fix` [#85](https://github.com/metabahn/corerb/pull/85) Initialize with class-level state when available ([bryanp](https://github.com/bryanp))
12
+
13
+ ## [v0.1.3](https://github.com/metabahn/corerb/releases/tag/2021-09-28)
14
+
15
+ *released on 2021-09-28*
16
+
17
+ * `fix` [#84](https://github.com/metabahn/corerb/pull/84) Define core-local as a dependency of core-state ([bryanp](https://github.com/bryanp))
18
+
1
19
  ## [v0.1.1](https://github.com/metabahn/corerb/releases/tag/2021-07-15)
2
20
 
3
21
  *released on 2021-07-15*
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Core
4
4
  module State
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.5"
6
6
 
7
7
  def self.version
8
8
  VERSION
data/lib/is/stateful.rb CHANGED
@@ -1,16 +1,80 @@
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 creates a recursive dependency, so just bundle it.
11
+ #
12
+ module Copy
13
+ DEFAULT = ::Object.new
14
+
15
+ refine ::Object do
16
+ if RbConfig::CONFIG["RUBY_PROGRAM_VERSION"] < "3"
17
+ def copy(freeze: DEFAULT)
18
+ should_freeze = resolve_freeze_argument(freeze)
19
+
20
+ value = clone(freeze: should_freeze)
21
+ value.freeze if should_freeze
22
+ value
23
+ end
24
+ else
25
+ def copy(freeze: DEFAULT)
26
+ clone(freeze: resolve_freeze_argument(freeze))
27
+ end
28
+ end
29
+
30
+ private def resolve_freeze_argument(value)
31
+ case value
32
+ when DEFAULT
33
+ frozen?
34
+ else
35
+ !!value
36
+ end
37
+ end
38
+ end
39
+
40
+ refine Array do
41
+ def copy(freeze: DEFAULT)
42
+ unless Stateful.copying?(self)
43
+ Stateful.prevent_recursion(self) do
44
+ array = map { |value|
45
+ value.copy(freeze: freeze)
46
+ }
47
+
48
+ array.freeze if resolve_freeze_argument(freeze)
49
+
50
+ array
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ refine Hash do
57
+ def copy(freeze: DEFAULT)
58
+ unless Stateful.copying?(self)
59
+ Stateful.prevent_recursion(self) do
60
+ hash = {}
61
+
62
+ each_pair do |key, value|
63
+ hash[key.copy(freeze: freeze)] = value.copy(freeze: freeze)
64
+ end
65
+
66
+ hash.freeze if resolve_freeze_argument(freeze)
67
+
68
+ hash
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+
11
75
  extend Is::Extension
12
76
 
13
- using Refine::Object::Copy
77
+ using Copy
14
78
 
15
79
  ALLOWED_FLAGS = %i[class instance].freeze
16
80
 
@@ -52,6 +116,10 @@ module Is
52
116
  defined_state[ivar_name] = state
53
117
  defined_state_isolations << ivar_name
54
118
 
119
+ if state[:class]
120
+ instance_variable_set(ivar_name, default)
121
+ end
122
+
55
123
  prefix = if private
56
124
  "private "
57
125
  else
@@ -118,7 +186,9 @@ module Is
118
186
  end
119
187
 
120
188
  subclass.send(:prepend_defined_state_modules)
121
- subclass.instance_variable_set("@__defined_state", @__defined_state.dup)
189
+ subclass.instance_variable_set("@__defined_state", @__defined_state.each_with_object({}) { |(key, value), state|
190
+ state[key] = value.dup
191
+ })
122
192
 
123
193
  super
124
194
  end
@@ -169,9 +239,14 @@ module Is
169
239
 
170
240
  extends :implementation, prepend: true do
171
241
  def initialize(...)
172
- self.class.send(:defined_state).each_pair do |name, state|
242
+ self_class = self.class
243
+ self_class.send(:defined_state).each_pair do |name, state|
173
244
  if state[:instance]
174
- instance_variable_set(name, state[:value])
245
+ if state[:class]
246
+ instance_variable_set(name, self_class.instance_variable_get(name))
247
+ else
248
+ instance_variable_set(name, state[:value])
249
+ end
175
250
  end
176
251
  end
177
252
 
@@ -196,5 +271,25 @@ module Is
196
271
  @__defined_state_isolations ||= []
197
272
  end
198
273
  end
274
+
275
+ class << self
276
+ include Is::Localized
277
+
278
+ def prevent_recursion(object)
279
+ object_id = object.object_id
280
+ copied_objects[object_id] = true
281
+ yield
282
+ ensure
283
+ copied_objects.delete(object_id)
284
+ end
285
+
286
+ def copying?(object)
287
+ copied_objects[object.object_id]
288
+ end
289
+
290
+ def copied_objects
291
+ localized(:__corerb_copied_objects) || localize(:__corerb_copied_objects, {})
292
+ end
293
+ end
199
294
  end
200
295
  end
metadata CHANGED
@@ -1,43 +1,43 @@
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.5
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-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: core-copy
14
+ name: core-extension
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.0'
19
+ version: '0.3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.0'
26
+ version: '0.3'
27
27
  - !ruby/object:Gem::Dependency
28
- name: core-extension
28
+ name: core-local
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.3'
33
+ version: '0.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.3'
40
+ version: '0.1'
41
41
  description: Easily manage object state.
42
42
  email: bryan@metabahn.com
43
43
  executables: []
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
- rubygems_version: 3.2.15
71
+ rubygems_version: 3.2.22
72
72
  signing_key:
73
73
  specification_version: 4
74
74
  summary: Easily manage object state.