statinize 0.4.3 → 0.4.5

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: b3184b7bdea5121e7355c91ad65b4929f9e3a13fcd6f6849a5bf971bb4f3ad3a
4
- data.tar.gz: 6ab5f9e27b4a3f337dfb6fbffd018d847a25bf743df3788824f219668ea58650
3
+ metadata.gz: dd5a1c15473a68cad29248c698c58fb190a37e432bfeb41d8622594904e79060
4
+ data.tar.gz: 7120d733e7389966c5378b924beb2a638eb2bfc283437020cd5bdf8601608fd5
5
5
  SHA512:
6
- metadata.gz: 134c65ae58d093141b00c284367304951e5d7da83ba07a6dc0ababc35fd5fff4020fd6d6c7ce794bdb69095fa2bb6dcf53d3fdeb387c19e79c34e814c5a292c0
7
- data.tar.gz: bbf362bb6cf6f87b190c4042f6897ad1b7c2ccf74ead7c6646dbc8bdf8e6d829e6503ca45806d306c6ccccd80b6b040b5a962e5ee9d5114a396bc7aa5e3959a1
6
+ metadata.gz: c67c423f48b274dd35b0ca582ea90e7081fb732ecf1252e97e5242ba2dfbbfc2434c93484eb2752d291ab19eeaaaf28b878925e2bd30a082709d2b64449932a5
7
+ data.tar.gz: af01616f0a9a41c3799c3384285ea741dee5301f88beb5d2b6afc5c762f0219b85d16c5eb5c2a6d0994bd72034db0d0e74ebf8548cec68df08f0fd796b66fad2
@@ -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"
@@ -2,7 +2,7 @@ module Statinize
2
2
  class Attribute
3
3
  include Comparable
4
4
 
5
- attr_reader :klass, :name, :default, :arg_name
5
+ attr_reader :klass, :name, :default, :default_exec, :arg_name
6
6
  attr_accessor :options_collection, :options
7
7
 
8
8
  def initialize(klass, name, options)
@@ -13,6 +13,7 @@ module Statinize
13
13
  @options_collection << options.clone.extend(Options) unless options.empty?
14
14
 
15
15
  @default = options[:default] if options.key?(:default)
16
+ @default_exec = options[:default_exec] if options.key?(:default_exec)
16
17
  @arg_name = name
17
18
  @name = options[:name] || name
18
19
  end
@@ -70,7 +70,11 @@ 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
+ end
75
+
76
+ statinizer.attributes.select { |a| a.options.key?(:default_exec) }.each do |attribute|
77
+ public_send("#{attribute.name}=", attribute.default_exec.call)
74
78
  end
75
79
  end
76
80
  end
@@ -50,7 +50,7 @@ module Statinize
50
50
 
51
51
  def populate(attrs)
52
52
  attrs.each do |attr|
53
- attribute attr.arg_name, name: attr.name, default: attr.default
53
+ attribute attr.arg_name, name: attr.name, default: attr.default, default_exec: attr.default_exec
54
54
  attributes.find { _1.name == attr.name }.tap do |attr_to_populate|
55
55
  attr_to_populate.options_collection = attr.options_collection.clone
56
56
  attr_to_populate.options = attr.options.clone
@@ -43,7 +43,7 @@ module Statinize
43
43
  attr_value = instance.public_send(attr.name)
44
44
 
45
45
  option.validators.each do |validator_class, validator_value|
46
- validator_instance = validator_class.new(attr_value, validator_value)
46
+ validator_instance = validator_class.new(attr_value, validator_value, instance)
47
47
 
48
48
  if option[:type] && option.should_cast? && attr_value.nil? && (option[:presence] || option[:nil] == false)
49
49
  cast(attr, option)
@@ -1,12 +1,13 @@
1
1
  module Statinize
2
2
  class Validator
3
- NOT_VALIDATORS = %i[force cast if unless default name]
3
+ NOT_VALIDATORS = %i[force cast if unless default default_exec name]
4
4
 
5
- attr_accessor :attr_value, :validator_value
5
+ attr_accessor :attr_value, :validator_value, :instance
6
6
 
7
- def initialize(attr_value, validator_value)
7
+ def initialize(attr_value, validator_value, instance)
8
8
  @attr_value = attr_value
9
9
  @validator_value = validator_value
10
+ @instance = instance
10
11
  end
11
12
 
12
13
  def valid?
@@ -1,7 +1,11 @@
1
1
  module Statinize
2
2
  class TypeValidator < Validator
3
3
  def valid?
4
- attr_value.is_a?(validator_value) || attr_value.nil?
4
+ if validator_value.is_a? Class
5
+ attr_value.is_a?(validator_value)
6
+ elsif validator_value.is_a? Proc
7
+ attr_value.is_a? instance.instance_exec(&validator_value)
8
+ end || attr_value.nil?
5
9
  end
6
10
 
7
11
  def error
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.3
4
+ version: 0.4.5
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-05 00:00:00.000000000 Z
11
+ date: 2022-11-24 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