statinize 0.2.5 → 0.3.0

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: 2af1c947ea86cfe13c9a10dfe90ba2b23fb63c1a0e2da61fdd2a67e38dee20b9
4
- data.tar.gz: 815b39101a2b9153a378a44122ca9d7899e7b837384ea0dfff22bbb2b6448bd0
3
+ metadata.gz: 667a2ecdfba95106c743143e47a3373c1c5dab105f82b1f69b3384c84e2aeb4a
4
+ data.tar.gz: f1005a83f8ae7a816f1b9bea2f8cdca1812e4bcd7a32bed2b928a8338fe68682
5
5
  SHA512:
6
- metadata.gz: ccbb70e5eee7ddac8a40a3c1dc3d0e97438e3ea7ce725e14f900df86afaa6452bb9db27694538f23a1774946165b30815178bb63a0c98de2885117b8ea1f6cd5
7
- data.tar.gz: 0c2a602dcdef6ec5ac69baa36158019ecb0cdd9cbb4313c6a041467b69cfb9c22d78873804b8117fffbaf1094e279842f7ab3fd2ba68dcc2cc9d95bc4946c3c8
6
+ metadata.gz: 0b888eaccd5ae0e23cc8a303abdaf8207fcc0f1bcd618bbe1985d71e3fc1b77c2b4e65e5a98bdcd04468e7977a37c69a8af732b1d1035c856f7fe0561b5359a7
7
+ data.tar.gz: bd422a1a7ddb0b917c895ebc8078b071b07bc1b35cb2090c2750a451df119adc668ba9e727d1ad11b534f44a37a58772ddc7f6837e996970d6990d75b7d38f9a
@@ -17,7 +17,11 @@ module Statinize
17
17
  end
18
18
 
19
19
  def create
20
- statinizer.add_attribute(self) unless attribute?
20
+ if attribute?
21
+ statinizer.attributes.find { |a| a.name == name }.options = options
22
+ else
23
+ statinizer.add_attribute(self)
24
+ end
21
25
  klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
22
26
  def #{name}
23
27
  @#{name}
data/lib/statinize/dsl.rb CHANGED
@@ -37,24 +37,5 @@ module Statinize
37
37
  def force(force = nil)
38
38
  force.nil? ? @force : @force = force
39
39
  end
40
-
41
- protected
42
-
43
- def merge_options(**options)
44
- attributes.each do |attribute|
45
- attribute.options.each do |option|
46
- option.merge!(options)
47
- end
48
- end
49
- end
50
-
51
- def populate(attrs)
52
- attrs.each do |attr|
53
- attribute attr.name
54
- attributes
55
- .find { _1.name == attr.name }
56
- .options = attr.options.clone
57
- end
58
- end
59
40
  end
60
41
  end
@@ -5,7 +5,9 @@ module Statinize
5
5
 
6
6
  class InvalidConditionError < StandardError; end
7
7
 
8
- class UndefinedAttribute < StandardError; end
8
+ class UndefinedAttributeError < StandardError; end
9
+
10
+ class UnknownAttributeError < StandardError; end
9
11
 
10
12
  class Errors < Hash
11
13
  def nice
@@ -3,11 +3,25 @@ module Statinize
3
3
  def self.included(klass)
4
4
  klass.extend(ClassMethods)
5
5
  klass.prepend(PrependedMethods)
6
+
7
+ statinized_ancestors = klass.ancestors
8
+ .reject { |a| a == klass || a == Statinize::Statinizable }
9
+ .select { |a| a.ancestors.include? Statinize::Statinizable }
10
+
11
+ if statinized_ancestors.any?
12
+ klass.instance_variable_set("@statinizer", Statinizer.new(klass))
13
+
14
+ statinized_ancestors.each do |ancestor|
15
+ klass.statinizer.populate(ancestor.statinizer.attributes)
16
+ end
17
+ end
6
18
  end
7
19
 
8
20
  module PrependedMethods
9
21
  def initialize(options = {}, *args, **kwargs, &block)
10
22
  symbolized = kwargs.merge(options).transform_keys(&:to_sym)
23
+ extra_attributes = symbolized.keys.map(&:to_sym) - statinizer.attributes.map(&:name).map(&:to_sym)
24
+ raise UnknownAttributeError, "Attributes #{extra_attributes.join(", ")} are unknown" if extra_attributes.any?
11
25
 
12
26
  if private_methods(false).include? :initialize
13
27
  super(*args, **kwargs, &block)
@@ -46,9 +60,9 @@ module Statinize
46
60
 
47
61
  private
48
62
 
49
- def check_defined!(kwargs)
63
+ def check_defined!(options)
50
64
  statinizer.attributes.map(&:name).each do |attr|
51
- undefined_attrs << attr if public_send(attr) != kwargs[attr] || !kwargs.key?(attr)
65
+ undefined_attrs << attr if public_send(attr) != options[attr] || !options.key?(attr)
52
66
  end
53
67
 
54
68
  raise UndefinedAttributeError, "Not all attributes defined in statinize block are defined in initialize"
@@ -57,7 +71,7 @@ module Statinize
57
71
 
58
72
  module ClassMethods
59
73
  def statinize(&block)
60
- @statinizer = Statinizer.new(self)
74
+ @statinizer = Statinizer.new(self) unless @statinizer
61
75
 
62
76
  statinizer.instance_eval(&block)
63
77
 
@@ -67,6 +81,11 @@ module Statinize
67
81
  def statinizer
68
82
  @statinizer
69
83
  end
84
+
85
+ def inherited(klass)
86
+ super(klass)
87
+ klass.include(Statinize::Statinizable)
88
+ end
70
89
  end
71
90
  end
72
91
  end
@@ -23,7 +23,7 @@ module Statinize
23
23
  end
24
24
 
25
25
  def add_attribute(attribute)
26
- attributes.add(attribute) unless attribute? attribute
26
+ attributes.add(attribute)
27
27
  end
28
28
 
29
29
  def attributes
@@ -44,6 +44,23 @@ module Statinize
44
44
  raise NoSuchValidatorError unless all_validators_defined?
45
45
  end
46
46
 
47
+ def merge_options(**options)
48
+ attributes.each do |attribute|
49
+ attribute.options.each do |option|
50
+ option.merge!(options)
51
+ end
52
+ end
53
+ end
54
+
55
+ def populate(attrs)
56
+ attrs.each do |attr|
57
+ attribute attr.name
58
+ attributes
59
+ .find { _1.name == attr.name }
60
+ .options = attr.options.clone
61
+ end
62
+ end
63
+
47
64
  protected
48
65
 
49
66
  def all_validators_defined?
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.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Barseek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-05 00:00:00.000000000 Z
11
+ date: 2022-09-25 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.