eac_ruby_utils 0.100.0 → 0.101.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: e6f852b958cf7ed8f6f05d3860d738de716c1c5a7456917b4705abf4ab78b546
4
- data.tar.gz: ea80e22f5fec3c6d5958a544a348790a01491490dc0f75ac98d8d59b88955122
3
+ metadata.gz: 53696f879b967bfd596c78de749bae9cf4faf9cc0e783cd94ef93f8801228aa4
4
+ data.tar.gz: 20f707f22860ae13a5b8f7032d91ef37a6d431d2d073e0070d2263053bed0076
5
5
  SHA512:
6
- metadata.gz: 9b9be95919c93a7b3800d726bb52055c9ecfe92b422601af67cc7ad50bbd0a6602d0d92d120759ad408661201040512545286fc376832031f594e46ea1d7d121
7
- data.tar.gz: 50674addc08d99c21ffcd2160a8beeb0418f49bd1bd6b2a815544179495b8fce3b4e33fb98074ab530a8def5052db2022048d758943afff3ade8855a3d6bc512
6
+ metadata.gz: 5c0de088366c5722e63312031f81695cc023d3fd0846ad343d3f7c987ca4fc679249fc729fb715f46844cff1aee4cce9833471fb712f2d967c417d7d48067299
7
+ data.tar.gz: eb0ab7f25211706f098ac5d2c73be11319f7034b5574997d5ded5b6acd10c3da0daa5f4333f682cfc74bdb34a2f01f48000c093c88ee6f65a883d3609becb17b
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/common_constructor/instance_initialize'
4
+ require 'eac_ruby_utils/common_constructor/super_args'
5
+
6
+ module EacRubyUtils
7
+ class CommonConstructor
8
+ class ClassAccessors
9
+ attr_reader :common_constructor, :klass
10
+
11
+ def initialize(common_constructor, klass)
12
+ @common_constructor = common_constructor
13
+ @klass = klass
14
+ end
15
+
16
+ def args
17
+ common_constructor.all_args
18
+ end
19
+
20
+ def perform
21
+ setup_class_attr_readers
22
+ setup_class_attr_writers
23
+ end
24
+
25
+ def setup_class_attr_readers
26
+ klass.send(:attr_reader, *args)
27
+ klass.send(:public, *args) if args.any?
28
+ end
29
+
30
+ def setup_class_attr_writers
31
+ klass.send(:attr_writer, *args)
32
+ klass.send(:private, *args.map { |a| "#{a}=" }) if args.any?
33
+ end
34
+ end
35
+ end
36
+ end
@@ -13,17 +13,27 @@ module EacRubyUtils
13
13
  @klass = klass
14
14
  end
15
15
 
16
- def run
16
+ def define_initialize_callbacks
17
+ klass.include(::ActiveSupport::Callbacks)
18
+ klass.define_callbacks :initialize
19
+ end
20
+
21
+ def define_initialize_method
17
22
  class_initialize = self
18
- klass.send(:define_method, :initialize) do |*args|
23
+ klass.send(:define_method, :initialize) do |*args, &block|
19
24
  ::EacRubyUtils::CommonConstructor::InstanceInitialize.new(
20
- class_initialize.common_constructor, args, self
21
- ).run
25
+ class_initialize.common_constructor, args, self, block
26
+ ).perform
22
27
  super(*::EacRubyUtils::CommonConstructor::SuperArgs.new(
23
28
  class_initialize, args, self
24
29
  ).result)
25
30
  end
26
31
  end
32
+
33
+ def perform
34
+ define_initialize_callbacks
35
+ define_initialize_method
36
+ end
27
37
  end
28
38
  end
29
39
  end
@@ -3,18 +3,20 @@
3
3
  module EacRubyUtils
4
4
  class CommonConstructor
5
5
  class InstanceInitialize
6
- attr_reader :common_constructor, :args, :object
6
+ attr_reader :common_constructor, :args, :object, :block
7
7
 
8
- def initialize(common_constructor, args, object)
8
+ def initialize(common_constructor, args, object, block)
9
9
  @common_constructor = common_constructor
10
10
  @args = args
11
11
  @object = object
12
+ @block = block
12
13
  end
13
14
 
14
- def run
15
+ def perform
15
16
  validate_args_count
16
17
  object.run_callbacks :initialize do
17
18
  object_attributes_set
19
+ object_block_set
18
20
  object_after_callback
19
21
  end
20
22
  end
@@ -36,12 +38,21 @@ module EacRubyUtils
36
38
  object.instance_eval(&common_constructor.after_set_block)
37
39
  end
38
40
 
41
+ def object_attribute_set(attr_name, attr_value)
42
+ object.send("#{attr_name}=", attr_value)
43
+ end
44
+
39
45
  def object_attributes_set
40
46
  common_constructor.args.each do |arg_name|
41
- object.send("#{arg_name}=", arg_value(arg_name))
47
+ object_attribute_set(arg_name, arg_value(arg_name))
42
48
  end
43
49
  end
44
50
 
51
+ def object_block_set
52
+ block_arg = common_constructor.block_arg
53
+ object_attribute_set(block_arg, block) if block_arg
54
+ end
55
+
45
56
  def validate_args_count
46
57
  return if common_constructor.args_count.include?(args.count)
47
58
 
@@ -2,12 +2,13 @@
2
2
 
3
3
  require 'active_support/callbacks'
4
4
  require 'eac_ruby_utils/arguments_consumer'
5
+ require 'eac_ruby_utils/common_constructor/class_accessors'
5
6
  require 'eac_ruby_utils/common_constructor/class_initialize'
6
7
  require 'ostruct'
7
8
 
8
9
  module EacRubyUtils
9
10
  class CommonConstructor
10
- attr_reader :args, :options, :after_set_block
11
+ attr_reader :all_args, :options, :after_set_block
11
12
 
12
13
  class << self
13
14
  def parse_args_options(args)
@@ -37,11 +38,15 @@ module EacRubyUtils
37
38
 
38
39
  def initialize(*args, &after_set_block)
39
40
  args_processed = self.class.parse_args_options(args)
40
- @args = args_processed.args
41
+ @all_args = args_processed.args
41
42
  @options = args_processed.options
42
43
  @after_set_block = after_set_block
43
44
  end
44
45
 
46
+ def args
47
+ block_arg? ? all_args[0..-2] : all_args
48
+ end
49
+
45
50
  def args_count
46
51
  (args_count_min..args_count_max)
47
52
  end
@@ -54,32 +59,32 @@ module EacRubyUtils
54
59
  args.count
55
60
  end
56
61
 
62
+ def block_arg
63
+ block_arg? ? all_args.last : nil
64
+ end
65
+
66
+ def block_arg?
67
+ options[:block_arg] || false
68
+ end
69
+
57
70
  def default_values
58
71
  options[:default] || []
59
72
  end
60
73
 
61
74
  def setup_class(klass)
62
- setup_class_attr_readers(klass)
63
- setup_class_attr_writers(klass)
75
+ setup_class_accessors(klass)
76
+
64
77
  setup_class_initialize(klass)
65
78
 
66
79
  klass
67
80
  end
68
81
 
69
- def setup_class_attr_readers(klass)
70
- klass.send(:attr_reader, *args)
71
- klass.send(:public, *args) if args.any?
72
- end
73
-
74
- def setup_class_attr_writers(klass)
75
- klass.send(:attr_writer, *args)
76
- klass.send(:private, *args.map { |a| "#{a}=" }) if args.any?
82
+ def setup_class_accessors(klass)
83
+ ::EacRubyUtils::CommonConstructor::ClassAccessors.new(self, klass).perform
77
84
  end
78
85
 
79
86
  def setup_class_initialize(klass)
80
- klass.include(::ActiveSupport::Callbacks)
81
- klass.define_callbacks :initialize
82
- ::EacRubyUtils::CommonConstructor::ClassInitialize.new(self, klass).run
87
+ ::EacRubyUtils::CommonConstructor::ClassInitialize.new(self, klass).perform
83
88
  end
84
89
 
85
90
  def super_args
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.100.0'
4
+ VERSION = '0.101.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_ruby_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.100.0
4
+ version: 0.101.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-14 00:00:00.000000000 Z
11
+ date: 2022-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -121,6 +121,7 @@ files:
121
121
  - lib/eac_ruby_utils/common_concern/class_setup.rb
122
122
  - lib/eac_ruby_utils/common_concern/module_setup.rb
123
123
  - lib/eac_ruby_utils/common_constructor.rb
124
+ - lib/eac_ruby_utils/common_constructor/class_accessors.rb
124
125
  - lib/eac_ruby_utils/common_constructor/class_initialize.rb
125
126
  - lib/eac_ruby_utils/common_constructor/instance_initialize.rb
126
127
  - lib/eac_ruby_utils/common_constructor/super_args.rb