eac_ruby_utils 0.98.0 → 0.101.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: 8e589ad673e489c92596bc3ad46ec06db307af584835cae664a96b163a9e1534
4
- data.tar.gz: 6a8f0a202d5fd79decc0a3e416b4b83a559f36ff2258510b2a92db2d5ea8c653
3
+ metadata.gz: 53696f879b967bfd596c78de749bae9cf4faf9cc0e783cd94ef93f8801228aa4
4
+ data.tar.gz: 20f707f22860ae13a5b8f7032d91ef37a6d431d2d073e0070d2263053bed0076
5
5
  SHA512:
6
- metadata.gz: '0184ca1df801cbbc7bdd241e15e628dbc42fa692695500ce0b77c11384d131712587834d36ddc6399926c3513548c6d59d1be53d9777306d12714f9b2d63d7af'
7
- data.tar.gz: 36991e903d0f00b8cac236477e8775915cb03dfb2a521308fba707dce3142b4ca0b305b4da34c04c9932de26ae7074ba22ce510b493621b97af815732ec7c678
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
@@ -29,8 +29,16 @@ module EacRubyUtils
29
29
  @values.values.map(&:value)
30
30
  end
31
31
 
32
+ def values_with_blank
33
+ [blank_value.value] + values
34
+ end
35
+
32
36
  def options
33
- @values.values.map { |v| [v.label, v.value] }
37
+ @values.values.map(&:to_option)
38
+ end
39
+
40
+ def options_with_blank
41
+ [blank_value.to_option] + options
34
42
  end
35
43
 
36
44
  def method_missing(name, *args, &block)
@@ -30,6 +30,11 @@ module EacRubyUtils
30
30
  translate('description')
31
31
  end
32
32
 
33
+ # @return [Array] A two-item array in format `[label, value]`.
34
+ def to_option
35
+ [label, value]
36
+ end
37
+
33
38
  def translation_required?
34
39
  @translation_required
35
40
  end
@@ -1,18 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Object
4
- def pretty_debug
4
+ def pretty_debug(options = {})
5
+ print_debug_title(options)
6
+
5
7
  STDERR.write(pretty_inspect)
6
8
 
7
9
  self
8
10
  end
9
11
 
10
- def print_debug
12
+ def print_debug(options = {})
13
+ print_debug_title(options)
11
14
  STDERR.write(to_debug + "\n")
12
15
 
13
16
  self
14
17
  end
15
18
 
19
+ def print_debug_title(title)
20
+ if title.is_a?(::Hash)
21
+ return unless title[:title]
22
+
23
+ title = title[:title]
24
+ end
25
+ char = '='
26
+ STDERR.write(char * (4 + title.length) + "\n")
27
+ STDERR.write("#{char} #{title} #{char}\n")
28
+ STDERR.write(char * (4 + title.length) + "\n")
29
+ end
30
+
16
31
  def to_debug
17
32
  "|#{::Object.instance_method(:to_s).bind(self).call}|#{self}|"
18
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.98.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.98.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-04 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