statinize 0.1.1 → 0.2.2

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: b8e1c828e69c088b99e338e49626e98faf5dc89a62a86545aa5e4041ce962647
4
- data.tar.gz: '094d296e476cd29e3c850542d71119eb2f48ac28fc366ecf3deebabb79f9db24'
3
+ metadata.gz: 1df88422c35ca62b3fe9b3efab22633e9f72a0e59690db7a005ae2cb9ad8cb4f
4
+ data.tar.gz: d72c1c0fdae71d168d082ee6e368970bdc81748cfa7ea293ec970065cd6232ef
5
5
  SHA512:
6
- metadata.gz: 40df9591e11075cd22f8c72b6c48ba31a0904ea679d5039cdb4f4508665fc9e02d443a3cb406352323a7be00048537248314914ff1a1523c736dde3dddfaa776
7
- data.tar.gz: f594086d4366967f456a21fb03f43b5aa83eccbf01865466b90be8130d9fcaac0925f369be2f0375af684d1fc2f4c1533c92f1f24d8acce3fce9f102271841ed
6
+ metadata.gz: 21b6c73e849260241b4a2301bdbdf1663ceff7279b666564761818618728058be4e5e67dd9d6d11a1f7adc382468d8b5e7fdfcfaf67acbb3b294879a415d7a28
7
+ data.tar.gz: c984be3998b0e6a4ee7e0c4c57263a88f9c1c9698fc91487a499980353c5be9b6a72c2f94ba87cf95d543b9ef5418303e32b2b3edd8299163660cd4c629bae33
@@ -8,7 +8,7 @@ module Statinize
8
8
  @klass = klass
9
9
  @name = name
10
10
  @options = OptionsCollection.new
11
- @options << opts.clone.extend(Options)
11
+ @options << opts.clone.extend(Options) unless opts.empty?
12
12
  end
13
13
 
14
14
  def self.create(klass, name, opts)
@@ -17,7 +17,17 @@ module Statinize
17
17
 
18
18
  def create
19
19
  statinizer.add_attribute(self) unless attribute?
20
- klass.send(:attr_accessor, name)
20
+ klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
21
+ def #{name}
22
+ @#{name}
23
+ end
24
+
25
+ def #{name}=(attr)
26
+ @#{name} = attr
27
+ validate if respond_to?(:validate)
28
+ attr
29
+ end
30
+ RUBY_EVAL
21
31
  self
22
32
  end
23
33
 
@@ -5,6 +5,8 @@ module Statinize
5
5
 
6
6
  class InvalidConditionError < StandardError; end
7
7
 
8
+ class UndefinedAttribute < StandardError; end
9
+
8
10
  class Errors < Array
9
11
  def to_s
10
12
  nice_errors = map do |i|
@@ -6,26 +6,45 @@ module Statinize
6
6
  end
7
7
 
8
8
  module PrependedMethods
9
- def initialize(*args, **kwargs)
10
- self.class.statinizer.attributes.map(&:name).each do |attr|
11
- instance_variable_set("@#{attr}", kwargs.delete(attr)) if kwargs.key?(attr)
9
+ def initialize(*args, **kwargs, &block)
10
+ if private_methods(false).include? :initialize
11
+ super(*args, **kwargs, &block)
12
+ check_defined!(kwargs)
13
+ else
14
+ statinizer.attributes.map(&:name).each do |attr|
15
+ instance_variable_set("@#{attr}", kwargs[attr]) if kwargs.key?(attr)
16
+ end
12
17
  end
13
18
 
14
19
  define_validation
15
20
  validate!
16
-
17
- super(*args, **kwargs)
18
21
  end
19
22
 
20
23
  def validation
21
24
  @validation ||= Validation.new(statinizer, self)
22
25
  end
23
26
 
27
+ def attributes
28
+ @attributes = Hash[
29
+ statinizer.attributes.map { |a| [a.name, public_send(a.name)] }
30
+ ]
31
+ end
32
+
24
33
  alias_method :define_validation, :validation
25
34
 
26
35
  def statinizer
27
36
  self.class.statinizer
28
37
  end
38
+
39
+ private
40
+
41
+ def check_defined!(kwargs)
42
+ statinizer.attributes.map(&:name).each do |attr|
43
+ undefined_attrs << attr if public_send(attr) != kwargs[attr] || !kwargs.key?(attr)
44
+ end
45
+
46
+ raise UndefinedAttributeError, "Not all attributes defined in statinize block are defined in initialize"
47
+ end
29
48
  end
30
49
 
31
50
  module ClassMethods
@@ -34,7 +53,7 @@ module Statinize
34
53
 
35
54
  statinizer.instance_eval(&block)
36
55
 
37
- # statinizer.check_validators_exist!
56
+ statinizer.check_validators_exist!
38
57
  end
39
58
 
40
59
  def statinizer
@@ -20,6 +20,22 @@ module Statinize
20
20
  end
21
21
  end
22
22
 
23
+ def with(**options, &block)
24
+ trace = TracePoint.trace(:call) do |tp|
25
+ tp.disable
26
+
27
+ if %i[attribute validate].include? tp.method_id
28
+ tp.binding.local_variable_get(:options).merge!(options)
29
+ end
30
+
31
+ tp.enable
32
+ end
33
+
34
+ trace.enable
35
+ instance_exec(&block)
36
+ trace.disable
37
+ end
38
+
23
39
  def force(force = nil)
24
40
  force.nil? ? @force : @force = force
25
41
  end
@@ -13,8 +13,8 @@ module Statinize
13
13
 
14
14
  def validate
15
15
  @errors = Errors.new
16
- @erroneous_attributes = Set.new
17
- @erroneous_forced_attributes = Set.new
16
+ @erroneous_attributes = Hash.new { |h, k| h[k] = Set.new }
17
+ @erroneous_forced_attributes = Hash.new { |h, k| h[k] = Set.new }
18
18
 
19
19
  fill_errors
20
20
  end
@@ -48,9 +48,13 @@ module Statinize
48
48
  next if validator_instance.valid?
49
49
  next if validator_class == TypeValidator && cast(attr, option)
50
50
 
51
- erroneous_attributes.add(attr)
52
- erroneous_forced_attributes.add(attr) if option[:force]
53
- @errors << { attr.name => validator_instance.error }
51
+ force = option[:force] ||
52
+ (statinizer.force? && option[:force].nil?)
53
+
54
+ if erroneous_attributes[attr.name].add? option
55
+ @errors << { attr.name => validator_instance.error }
56
+ end
57
+ erroneous_forced_attributes[attr.name].add option if force
54
58
  end
55
59
  end
56
60
  end
@@ -65,8 +69,7 @@ module Statinize
65
69
  def should_raise?
66
70
  return false if valid?
67
71
 
68
- statinizer.force? ||
69
- erroneous_attributes.intersect?(erroneous_forced_attributes)
72
+ erroneous_attributes.keys.intersect?(erroneous_forced_attributes.keys)
70
73
  end
71
74
 
72
75
  def attributes
@@ -1,7 +1,7 @@
1
1
  module Statinize
2
2
  class TypeValidator < Validator
3
3
  def valid?
4
- attr_value.is_a?(validator_value) || attr_value.nil?
4
+ attr_value.is_a?(validator_value)
5
5
  end
6
6
 
7
7
  def error
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.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Barseek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-06 00:00:00.000000000 Z
11
+ date: 2022-07-10 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.