core-state 0.0.0 → 0.1.3

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: 5064c918f78abedb88b383958fc832354960acdb99fde67a9f2d24374a7ce413
4
- data.tar.gz: 3aceb46e96a0c03c5f32ee158dfd005346486a9ba3684afecf1910a781c29425
3
+ metadata.gz: 1256eb4ef3acf8b61a7754d8ea30ce644f7fc081132a3d9f107a40deca79ed0a
4
+ data.tar.gz: f65aa78f287f521a729723823f04339153d0a80cf8291e82e4a1c791cc57745e
5
5
  SHA512:
6
- metadata.gz: 1b028968f083cf0dfd4cc36bcd4d0812adf50243c7ba78cf281291a4ddc71f181fb5980f6a51959d65d378242aeecbdbb75904a803b5341b06413ccbfd46938b
7
- data.tar.gz: a085f1d993bef88178ef654086bc2fcbfd0739b91f4f4c5089816beb5da14060bb8f3050e4fa4924bfb6bd61ce63624e3fb98bc6c05258daf38b19e845b69900
6
+ metadata.gz: 1669bdf575ac48ed4abb7523383d2a8ae39a6f52fd6b08143b3ef48bd89698e9492f9f16ffff1538329a35989ec57bde2db1b77e2d67e1cafc89bd33896788e4
7
+ data.tar.gz: 1cda2c4017dccf2c9e9d9c935d48c95d6aeeda0c471f662d5618b723b54d08d921bae78849e3aa5fd7783716402edbc8d8726a5638c83992e318db45be4c2a27
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## [v0.1.3](https://github.com/metabahn/corerb/releases/tag/2021-09-28)
2
+
3
+ *released on 2021-09-28*
4
+
5
+ * `fix` [#84](https://github.com/metabahn/corerb/pull/84) Define core-local as a dependency of core-state ([bryanp](https://github.com/bryanp))
6
+
7
+ ## [v0.1.1](https://github.com/metabahn/corerb/releases/tag/2021-07-15)
8
+
9
+ *released on 2021-07-15*
10
+
11
+ * `fix` [#62](https://github.com/metabahn/corerb/pull/62) Correctly isolate class-level state on access ([bryanp](https://github.com/bryanp))
12
+ * `fix` [#55](https://github.com/metabahn/corerb/pull/55) Correctly forward arguments to super initialize ([bryanp](https://github.com/bryanp))
13
+
14
+ ## [v0.1.0](https://github.com/metabahn/corerb/releases/tag/2021-07-07)
15
+
16
+ *released on 2021-07-07*
17
+
18
+ * `fix` [#48](https://github.com/metabahn/corerb/pull/48) Correctly copy defined state when a stateful object is copied ([bryanp](https://github.com/bryanp))
19
+ * `chg` [#44](https://github.com/metabahn/corerb/pull/44) Drop Ruby 2.6 support from core-state ([bryanp](https://github.com/bryanp))
20
+
1
21
  ## [v0.0.0](https://github.com/metabahn/corerb/releases/tag/2021-05-22)
2
22
 
3
23
  *released on 2021-05-22*
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Core
4
4
  module State
5
- VERSION = "0.0.0"
5
+ VERSION = "0.1.3"
6
6
 
7
7
  def self.version
8
8
  VERSION
data/lib/is/stateful.rb CHANGED
@@ -1,24 +1,94 @@
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
 
17
- applied do
87
+ applies do
18
88
  prepend_defined_state_modules
19
89
  end
20
90
 
21
- extends :class do
91
+ extends :definition do
22
92
  # [public] Defines state by name.
23
93
  #
24
94
  # flags - Changes how the state is defined. Possible values include:
@@ -62,9 +132,9 @@ module Is
62
132
  if state[:class]
63
133
  defined_state_class_module.module_eval <<~CODE, __FILE__, __LINE__ + 1
64
134
  #{prefix}def #{method_name}
65
- unless defined_state_isolations.include?(#{ivar_name})
135
+ unless defined_state_isolations.include?(#{ivar_name.inspect})
66
136
  #{ivar_name} = #{ivar_name}.copy
67
- defined_state_isolations << #{ivar_name}
137
+ defined_state_isolations << #{ivar_name.inspect}
68
138
  end
69
139
 
70
140
  #{ivar_name}
@@ -75,9 +145,9 @@ module Is
75
145
  if state[:instance]
76
146
  defined_state_instance_module.module_eval <<~CODE, __FILE__, __LINE__ + 1
77
147
  #{prefix}def #{method_name}
78
- unless defined_state_isolations.include?(#{ivar_name})
148
+ unless defined_state_isolations.include?(#{ivar_name.inspect})
79
149
  #{ivar_name} = #{ivar_name}.copy
80
- defined_state_isolations << #{ivar_name}
150
+ defined_state_isolations << #{ivar_name.inspect}
81
151
  end
82
152
 
83
153
  #{ivar_name}
@@ -167,8 +237,8 @@ module Is
167
237
  end
168
238
  end
169
239
 
170
- extends :instance, prepend: true do
171
- def initialize(*)
240
+ extends :implementation, prepend: true do
241
+ def initialize(...)
172
242
  self.class.send(:defined_state).each_pair do |name, state|
173
243
  if state[:instance]
174
244
  instance_variable_set(name, state[:value])
@@ -177,9 +247,15 @@ module Is
177
247
 
178
248
  super
179
249
  end
250
+
251
+ def initialize_copy(...)
252
+ @__defined_state_isolations = []
253
+
254
+ super
255
+ end
180
256
  end
181
257
 
182
- extends :class, :instance do
258
+ extends :definition, :implementation do
183
259
  # [public] Safely mutates state by name, yielding a copy of the current value to the block.
184
260
  #
185
261
  def mutate_state(name, &block)
@@ -190,5 +266,25 @@ module Is
190
266
  @__defined_state_isolations ||= []
191
267
  end
192
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
193
289
  end
194
290
  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.0.0
4
+ version: 0.1.3
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-05-22 00:00:00.000000000 Z
11
+ date: 2021-09-29 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.0'
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.0'
40
+ version: '0.1'
41
41
  description: Easily manage object state.
42
42
  email: bryan@metabahn.com
43
43
  executables: []
@@ -61,14 +61,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
61
  requirements:
62
62
  - - ">="
63
63
  - !ruby/object:Gem::Version
64
- version: 2.6.7
64
+ version: '2.7'
65
65
  required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - ">="
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.