statinize 0.3.2 → 0.4.3

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: 8076207399abda2e6f39e33f71e78a318819bbdb9cd7ae759d6a896f62cf49ce
4
- data.tar.gz: 53378fe3b67039c26472294ac3061c2d9a2dbd7801a77d61602f6804e05c389e
3
+ metadata.gz: b3184b7bdea5121e7355c91ad65b4929f9e3a13fcd6f6849a5bf971bb4f3ad3a
4
+ data.tar.gz: 6ab5f9e27b4a3f337dfb6fbffd018d847a25bf743df3788824f219668ea58650
5
5
  SHA512:
6
- metadata.gz: a6919c84d1fb55b306fc37663152b438c23b199dc952d68e4ee8618d9f2987be236583f7bf7035a9c1e1397edfcd70b58e09a99d71ef4e87d9d3a210cd893c8f
7
- data.tar.gz: 6f6d9af898f648a285f377bed8f5be59ab9045f67296dfd7ed4bc0807a544f8bc62aef377c585d1f2ddd258d9b5012a6c1eff95c1a0c5fe36071bf1f951b321f
6
+ metadata.gz: 134c65ae58d093141b00c284367304951e5d7da83ba07a6dc0ababc35fd5fff4020fd6d6c7ce794bdb69095fa2bb6dcf53d3fdeb387c19e79c34e814c5a292c0
7
+ data.tar.gz: bbf362bb6cf6f87b190c4042f6897ad1b7c2ccf74ead7c6646dbc8bdf8e6d829e6503ca45806d306c6ccccd80b6b040b5a962e5ee9d5114a396bc7aa5e3959a1
@@ -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
data/lib/statinize/dsl.rb CHANGED
@@ -16,15 +16,22 @@ module Statinize
16
16
  end
17
17
 
18
18
  def with(**options, &block)
19
+ # create new statinizer instance
19
20
  instance = self.class.new(klass)
20
21
  instance.force(force)
21
22
 
23
+ # execute block in the context of that statinizer
24
+ # while it's attached to the klass
25
+ # then rewind and attach the original statinizer(self)
26
+ # back to the klass
22
27
  klass.instance_variable_set(:@statinizer, instance)
23
28
  instance.instance_exec(&block)
24
29
  klass.instance_variable_set(:@statinizer, self)
25
30
 
31
+ # merge the newly created statinizer with the options
26
32
  instance.merge_options(**options)
27
33
 
34
+ # populate self with the instance's attributes
28
35
  populate(instance.attributes)
29
36
  end
30
37
 
@@ -18,15 +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)
21
+ def initialize(options = {}, *args, &block)
22
+ symbolized = options.transform_keys(&:to_sym)
23
+
24
+ instantiate_defaults
23
25
 
24
26
  if private_methods(false).include? :initialize
25
- super(*args, **kwargs, &block)
27
+ super(options, *args, &block)
26
28
  check_defined!(symbolized)
27
29
  else
28
- statinizer.attributes.map(&:name).each do |attr|
29
- 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)
30
32
  end
31
33
  end
32
34
 
@@ -60,11 +62,17 @@ module Statinize
60
62
 
61
63
  def check_defined!(options)
62
64
  statinizer.attributes.map(&:name).each do |attr|
63
- undefined_attrs << attr if public_send(attr) != options[attr] || !options.key?(attr)
65
+ undefined_attrs << attr unless options.key?(attr)
64
66
  end
65
67
 
66
68
  raise UndefinedAttributeError, "Not all attributes defined in statinize block are defined in initialize"
67
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
68
76
  end
69
77
 
70
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
@@ -54,17 +50,18 @@ module Statinize
54
50
 
55
51
  def populate(attrs)
56
52
  attrs.each do |attr|
57
- attribute attr.name
58
- attributes
59
- .find { _1.name == attr.name }
60
- .options = attr.options.clone
53
+ attribute attr.arg_name, name: attr.name, default: attr.default
54
+ attributes.find { _1.name == attr.name }.tap do |attr_to_populate|
55
+ attr_to_populate.options_collection = attr.options_collection.clone
56
+ attr_to_populate.options = attr.options.clone
57
+ end
61
58
  end
62
59
  end
63
60
 
64
61
  protected
65
62
 
66
63
  def all_validators_defined?
67
- attributes.map { |attr| attr.options.all_validators_defined? }.all? { !!_1 }
64
+ attributes.map { |attr| attr.options_collection.all_validators_defined? }.all? { !!_1 }
68
65
  end
69
66
  end
70
67
  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.2
4
+ version: 0.4.3
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-26 00:00:00.000000000 Z
11
+ date: 2022-11-05 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.