statinize 0.4.2 → 0.4.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9aab9a6ffd9f3b34b5275c2af057e44177f7881ce11fcea7ec4404a7cb933bad
4
- data.tar.gz: 8f71ae54ea17777a0440acdb966c6bc7dd59688184fe4af9b98c31d318fdcae5
3
+ metadata.gz: a2a7fe61fb4cc14c6a74ff3847b650a365688269d1e0ef34d7f1d061cd17a7a9
4
+ data.tar.gz: 9c8fe51192997a3e22bc15d392a1a7df7489e080dc6c6c8db6e615d40807961d
5
5
  SHA512:
6
- metadata.gz: 05b41cf02e7a4612e1ccc4e839df19733d33ee9e9e5d70296c0e5ce81a2bd5af51de2c0572b7791b284736538dd3e12a10a55f7c4a66872b3369ee628fd377df
7
- data.tar.gz: 697f7d67836e0057bbb8a9697e611fe026bb91276a02fea2367ec4f494a113d1ae2187b3cebc597070c540160773124e5d8cc192cc2cab213a8729713fca7053
6
+ metadata.gz: e81d152b1d3e596147d5d39ca4afdc701345cd6310b667823c824c3c88331081e4cbb3913914d48fade0b496e1b2ea5bdc32beafa6514ffdc54d679523dce1c4
7
+ data.tar.gz: ae3a96d472a35a3ccd04c6b52b62cbd1aedf66676006335442e3ef5a4c9f44a75b448dfd7e66a99fb6c3fd6b194ec178a2a6ad7254a96092588b04ddcda1b043
@@ -0,0 +1,53 @@
1
+ # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/object/deep_dup.rb
2
+
3
+ class Object
4
+ # Returns a deep copy of object if it's duplicable. If it's
5
+ # not duplicable, returns +self+.
6
+ #
7
+ # object = Object.new
8
+ # dup = object.deep_dup
9
+ # dup.instance_variable_set(:@a, 1)
10
+ #
11
+ # object.instance_variable_defined?(:@a) # => false
12
+ # dup.instance_variable_defined?(:@a) # => true
13
+ def deep_dup
14
+ duplicable? ? dup : self
15
+ end
16
+ end
17
+
18
+ class Array
19
+ # Returns a deep copy of array.
20
+ #
21
+ # array = [1, [2, 3]]
22
+ # dup = array.deep_dup
23
+ # dup[1][2] = 4
24
+ #
25
+ # array[1][2] # => nil
26
+ # dup[1][2] # => 4
27
+ def deep_dup
28
+ map(&:deep_dup)
29
+ end
30
+ end
31
+
32
+ class Hash
33
+ # Returns a deep copy of hash.
34
+ #
35
+ # hash = { a: { b: 'b' } }
36
+ # dup = hash.deep_dup
37
+ # dup[:a][:c] = 'c'
38
+ #
39
+ # hash[:a][:c] # => nil
40
+ # dup[:a][:c] # => "c"
41
+ def deep_dup
42
+ hash = dup
43
+ each_pair do |key, value|
44
+ if ::String === key || ::Symbol === key
45
+ hash[key] = value.deep_dup
46
+ else
47
+ hash.delete(key)
48
+ hash[key.deep_dup] = value.deep_dup
49
+ end
50
+ end
51
+ hash
52
+ end
53
+ end
@@ -0,0 +1,42 @@
1
+ # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/object/duplicable.rb
2
+
3
+ class Object
4
+ # Can you safely dup this object?
5
+ #
6
+ # False for method objects;
7
+ # true otherwise.
8
+ def duplicable?
9
+ true
10
+ end
11
+ end
12
+
13
+ class Method
14
+ # Methods are not duplicable:
15
+ #
16
+ # method(:puts).duplicable? # => false
17
+ # method(:puts).dup # => TypeError: allocator undefined for Method
18
+ def duplicable?
19
+ false
20
+ end
21
+ end
22
+
23
+ class UnboundMethod
24
+ # Unbound methods are not duplicable:
25
+ #
26
+ # method(:puts).unbind.duplicable? # => false
27
+ # method(:puts).unbind.dup # => TypeError: allocator undefined for UnboundMethod
28
+ def duplicable?
29
+ false
30
+ end
31
+ end
32
+
33
+ require "singleton"
34
+
35
+ module Singleton
36
+ # Singleton instances are not duplicable:
37
+ #
38
+ # Class.new.include(Singleton).instance.dup # TypeError (can't dup instance of singleton
39
+ def duplicable?
40
+ false
41
+ end
42
+ end
data/lib/core_ext.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "core_ext/deep_dup"
2
+ require "core_ext/duplicable"
data/lib/statinize/dsl.rb CHANGED
@@ -16,15 +16,22 @@ module Statinize
16
16
  end
17
17
 
18
18
  def with(**options, &block)
19
+ # create new statinizer instance
19
20
  instance = self.class.new(klass)
20
21
  instance.force(force)
21
22
 
23
+ # execute block in the context of that statinizer
24
+ # while it's attached to the klass
25
+ # then rewind and attach the original statinizer(self)
26
+ # back to the klass
22
27
  klass.instance_variable_set(:@statinizer, instance)
23
28
  instance.instance_exec(&block)
24
29
  klass.instance_variable_set(:@statinizer, self)
25
30
 
31
+ # merge the newly created statinizer with the options
26
32
  instance.merge_options(**options)
27
33
 
34
+ # populate self with the instance's attributes
28
35
  populate(instance.attributes)
29
36
  end
30
37
 
@@ -70,7 +70,7 @@ module Statinize
70
70
 
71
71
  def instantiate_defaults
72
72
  statinizer.attributes.select { |a| a.options.key?(:default) }.each do |attribute|
73
- public_send("#{attribute.name}=", attribute.default)
73
+ public_send("#{attribute.name}=", attribute.default.deep_dup)
74
74
  end
75
75
  end
76
76
  end
@@ -50,10 +50,11 @@ module Statinize
50
50
 
51
51
  def populate(attrs)
52
52
  attrs.each do |attr|
53
- attribute attr.name
54
- attributes
55
- .find { _1.name == attr.name }
56
- .options_collection = attr.options_collection.clone
53
+ attribute attr.arg_name, name: attr.name, default: attr.default
54
+ attributes.find { _1.name == attr.name }.tap do |attr_to_populate|
55
+ attr_to_populate.options_collection = attr.options_collection.clone
56
+ attr_to_populate.options = attr.options.clone
57
+ end
57
58
  end
58
59
  end
59
60
 
data/lib/statinize.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "set"
2
2
  require "pry"
3
3
  require "bigdecimal/util"
4
+ require "core_ext"
4
5
  require "statinize/statinizable"
5
6
  require "statinize/dsl"
6
7
  require "statinize/statinizer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statinize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Barseek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-04 00:00:00.000000000 Z
11
+ date: 2022-11-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Statinization gem. Allows for creation of attributes for a class with
14
14
  a given type.
@@ -17,6 +17,9 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/core_ext.rb
21
+ - lib/core_ext/deep_dup.rb
22
+ - lib/core_ext/duplicable.rb
20
23
  - lib/statinize.rb
21
24
  - lib/statinize/attribute.rb
22
25
  - lib/statinize/attribute/options.rb