statinize 0.3.1 → 0.4.2

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: '0098c847e6bc5a635a49088e2743a4f229378acf5b69b92f1fd9318c1fe3066c'
4
- data.tar.gz: 5422215479267d866b48b06786bc7794582289f3c13957b6825c70e749743ed7
3
+ metadata.gz: 9aab9a6ffd9f3b34b5275c2af057e44177f7881ce11fcea7ec4404a7cb933bad
4
+ data.tar.gz: 8f71ae54ea17777a0440acdb966c6bc7dd59688184fe4af9b98c31d318fdcae5
5
5
  SHA512:
6
- metadata.gz: a9a8442f442999cee53a0f7f105dbe695258877d13d77ffdff22e721f2f1a70746ae68fd415b81f4418dc0d8063d2182c9d46390cea6e5cbb04a53a0a59fc148
7
- data.tar.gz: 5a6bd4518f967b2cdf3826017749ee4920460b1634320dc8618f230e7299f5829b3efd3c50334c71b5d8be583d7db30bf1e921b7a5fcec84c7f9f09c98b15a2b
6
+ metadata.gz: 05b41cf02e7a4612e1ccc4e839df19733d33ee9e9e5d70296c0e5ce81a2bd5af51de2c0572b7791b284736538dd3e12a10a55f7c4a66872b3369ee628fd377df
7
+ data.tar.gz: 697f7d67836e0057bbb8a9697e611fe026bb91276a02fea2367ec4f494a113d1ae2187b3cebc597070c540160773124e5d8cc192cc2cab213a8729713fca7053
@@ -2,26 +2,28 @@ module Statinize
2
2
  class Attribute
3
3
  include Comparable
4
4
 
5
- attr_reader :klass, :name
6
- attr_accessor :options
5
+ attr_reader :klass, :name, :default, :arg_name
6
+ attr_accessor :options_collection, :options
7
7
 
8
- def initialize(klass, name, opts)
8
+ def initialize(klass, name, options)
9
9
  @klass = klass
10
10
  @name = name
11
- @options = OptionsCollection.new
12
- @options << opts.clone.extend(Options) unless opts.empty?
11
+ @options = options
12
+ @options_collection = OptionsCollection.new # TODO: think of a better way
13
+ @options_collection << options.clone.extend(Options) unless options.empty?
14
+
15
+ @default = options[:default] if options.key?(:default)
16
+ @arg_name = name
17
+ @name = options[:name] || name
13
18
  end
14
19
 
15
- def self.create(klass, name, opts = {})
16
- new(klass, name, opts).create
20
+ def self.create(klass, name, options = {})
21
+ new(klass, name, options).create
17
22
  end
18
23
 
19
24
  def create
20
- if attribute?
21
- statinizer.attributes.find { |a| a.name == name }.options = options
22
- else
23
- statinizer.add_attribute(self)
24
- end
25
+ attribute? ? override : add_attribute
26
+
25
27
  klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
26
28
  def #{name}
27
29
  @#{name}
@@ -37,7 +39,7 @@ module Statinize
37
39
  end
38
40
 
39
41
  def add_options(opts)
40
- options << opts.extend(Options)
42
+ options_collection << opts.extend(Options)
41
43
  end
42
44
 
43
45
  def <=>(other)
@@ -48,6 +50,16 @@ module Statinize
48
50
 
49
51
  private
50
52
 
53
+ def override
54
+ attribute = statinizer.attributes.find { |a| a.name == name }
55
+ attribute.options_collection = options_collection
56
+ attribute.options = options
57
+ end
58
+
59
+ def add_attribute
60
+ statinizer.add_attribute(self)
61
+ end
62
+
51
63
  def statinizer
52
64
  klass.statinizer
53
65
  end
@@ -1,11 +1,7 @@
1
1
  module Statinize
2
- class Configuration
3
- include Singleton
2
+ module Config
3
+ extend self
4
4
 
5
5
  attr_accessor :force
6
-
7
- def self.configure
8
- yield instance if block_given?
9
- end
10
6
  end
11
7
  end
@@ -18,17 +18,17 @@ module Statinize
18
18
  end
19
19
 
20
20
  module PrependedMethods
21
- def initialize(options = {}, *args, **kwargs, &block)
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?
21
+ def initialize(options = {}, *args, &block)
22
+ symbolized = options.transform_keys(&:to_sym)
23
+
24
+ instantiate_defaults
25
25
 
26
26
  if private_methods(false).include? :initialize
27
- super(*args, **kwargs, &block)
27
+ super(options, *args, &block)
28
28
  check_defined!(symbolized)
29
29
  else
30
- statinizer.attributes.map(&:name).each do |attr|
31
- instance_variable_set("@#{attr}", symbolized[attr]) if symbolized.key?(attr)
30
+ statinizer.attributes.each do |attr|
31
+ instance_variable_set("@#{attr.name}", symbolized[attr.arg_name]) if symbolized.key?(attr.arg_name)
32
32
  end
33
33
  end
34
34
 
@@ -62,11 +62,17 @@ module Statinize
62
62
 
63
63
  def check_defined!(options)
64
64
  statinizer.attributes.map(&:name).each do |attr|
65
- undefined_attrs << attr if public_send(attr) != options[attr] || !options.key?(attr)
65
+ undefined_attrs << attr unless options.key?(attr)
66
66
  end
67
67
 
68
68
  raise UndefinedAttributeError, "Not all attributes defined in statinize block are defined in initialize"
69
69
  end
70
+
71
+ def instantiate_defaults
72
+ statinizer.attributes.select { |a| a.options.key?(:default) }.each do |attribute|
73
+ public_send("#{attribute.name}=", attribute.default)
74
+ end
75
+ end
70
76
  end
71
77
 
72
78
  module ClassMethods
@@ -4,10 +4,6 @@ module Statinize
4
4
 
5
5
  attr_reader :klass, :before_callbacks
6
6
 
7
- def self.configure(&block)
8
- Configuration.configure(&block)
9
- end
10
-
11
7
  def initialize(klass)
12
8
  @klass = klass
13
9
  @force = config.force
@@ -15,7 +11,7 @@ module Statinize
15
11
  end
16
12
 
17
13
  def config
18
- @config ||= Configuration.instance
14
+ @config ||= Config
19
15
  end
20
16
 
21
17
  def force?
@@ -46,7 +42,7 @@ module Statinize
46
42
 
47
43
  def merge_options(**options)
48
44
  attributes.each do |attribute|
49
- attribute.options.each do |option|
45
+ attribute.options_collection.each do |option|
50
46
  option.merge!(options)
51
47
  end
52
48
  end
@@ -57,14 +53,14 @@ module Statinize
57
53
  attribute attr.name
58
54
  attributes
59
55
  .find { _1.name == attr.name }
60
- .options = attr.options.clone
56
+ .options_collection = attr.options_collection.clone
61
57
  end
62
58
  end
63
59
 
64
60
  protected
65
61
 
66
62
  def all_validators_defined?
67
- attributes.map { |attr| attr.options.all_validators_defined? }.all? { !!_1 }
63
+ attributes.map { |attr| attr.options_collection.all_validators_defined? }.all? { !!_1 }
68
64
  end
69
65
  end
70
66
  end
@@ -37,7 +37,7 @@ module Statinize
37
37
 
38
38
  def fill_errors
39
39
  attributes.each do |attr|
40
- attr.options.each do |option|
40
+ attr.options_collection.each do |option|
41
41
  next unless option.should_validate?(instance)
42
42
 
43
43
  attr_value = instance.public_send(attr.name)
@@ -1,6 +1,6 @@
1
1
  module Statinize
2
2
  class Validator
3
- NOT_VALIDATORS = %i[force cast if unless]
3
+ NOT_VALIDATORS = %i[force cast if unless default name]
4
4
 
5
5
  attr_accessor :attr_value, :validator_value
6
6
 
data/lib/statinize.rb CHANGED
@@ -1,20 +1,27 @@
1
1
  require "set"
2
2
  require "pry"
3
- require "singleton"
4
3
  require "bigdecimal/util"
5
- require_relative "statinize/statinizable"
6
- require_relative "statinize/dsl"
7
- require_relative "statinize/statinizer"
8
- require_relative "statinize/configuration"
9
- require_relative "statinize/attribute"
10
- require_relative "statinize/attribute/options"
11
- require_relative "statinize/attribute/options/conditions"
12
- require_relative "statinize/attribute/options_collection"
13
- require_relative "statinize/validator"
14
- require_relative "statinize/validation"
15
- require_relative "statinize/validators/type_validator"
16
- require_relative "statinize/validators/presence_validator"
17
- require_relative "statinize/validators/inclusion_validator"
18
- require_relative "statinize/validators/nil_validator"
19
- require_relative "statinize/caster"
20
- require_relative "statinize/errors"
4
+ require "statinize/statinizable"
5
+ require "statinize/dsl"
6
+ require "statinize/statinizer"
7
+ require "statinize/configuration"
8
+ require "statinize/attribute"
9
+ require "statinize/attribute/options"
10
+ require "statinize/attribute/options/conditions"
11
+ require "statinize/attribute/options_collection"
12
+ require "statinize/validator"
13
+ require "statinize/validation"
14
+ require "statinize/validators/type_validator"
15
+ require "statinize/validators/presence_validator"
16
+ require "statinize/validators/inclusion_validator"
17
+ require "statinize/validators/nil_validator"
18
+ require "statinize/caster"
19
+ require "statinize/errors"
20
+
21
+ module Statinize
22
+ def self.included(klass)
23
+ klass.include(Statinize::Statinizable)
24
+ end
25
+
26
+ def self.configure = yield Config
27
+ end
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.3.1
4
+ version: 0.4.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-09-25 00:00:00.000000000 Z
11
+ date: 2022-11-04 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.