statinize 0.2.4 → 0.3.0

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: b3dda46ee65052ce2579b2b223c659c3c76d68d85df008400d14432aeaf53fb1
4
- data.tar.gz: bd60810a6e386bdad9d9628dc9f1cb2f0cf2b1e04df41f6fc1762109c7909081
3
+ metadata.gz: 667a2ecdfba95106c743143e47a3373c1c5dab105f82b1f69b3384c84e2aeb4a
4
+ data.tar.gz: f1005a83f8ae7a816f1b9bea2f8cdca1812e4bcd7a32bed2b928a8338fe68682
5
5
  SHA512:
6
- metadata.gz: d3ca8f71c4efa6b8298fffc4ff94fa45193d7aa0ef60bfb6128ca539683c8500086362e520c6e8937dfd82d7896475d90854b01a947a38519d08105a4c048cb3
7
- data.tar.gz: d2d6b696b3195cd9b75cfe8b35ce7eb149965ed89c3d79c2f3737e575cb5bcd2a2be80e6e5061c712e4f68a57aa9d71fcdf90a4c5c36a2e7d7f4069d07a2826b
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,12 +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
- symbolized = kwargs.transform_keys(&:to_sym)
11
- .merge(options.transform_keys(&:to_sym))
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?
12
25
 
13
26
  if private_methods(false).include? :initialize
14
27
  super(*args, **kwargs, &block)
@@ -47,9 +60,9 @@ module Statinize
47
60
 
48
61
  private
49
62
 
50
- def check_defined!(kwargs)
63
+ def check_defined!(options)
51
64
  statinizer.attributes.map(&:name).each do |attr|
52
- 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)
53
66
  end
54
67
 
55
68
  raise UndefinedAttributeError, "Not all attributes defined in statinize block are defined in initialize"
@@ -58,7 +71,7 @@ module Statinize
58
71
 
59
72
  module ClassMethods
60
73
  def statinize(&block)
61
- @statinizer = Statinizer.new(self)
74
+ @statinizer = Statinizer.new(self) unless @statinizer
62
75
 
63
76
  statinizer.instance_eval(&block)
64
77
 
@@ -68,6 +81,11 @@ module Statinize
68
81
  def statinizer
69
82
  @statinizer
70
83
  end
84
+
85
+ def inherited(klass)
86
+ super(klass)
87
+ klass.include(Statinize::Statinizable)
88
+ end
71
89
  end
72
90
  end
73
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.4
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-03 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.