eac_ruby_utils 0.22.0 → 0.23.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: b861a9c3b748b6aeb1e66d0ddbdecd38f5fecf08f14ca42a31a486ed7d7ae9b4
4
- data.tar.gz: be84a8533d1a4fea360b9e250799724acc7f3ef3e5f4d99e9bd567dda73d5b11
3
+ metadata.gz: 50c460eb97537968f851b321ff60f2cd09a59311fc5355ec178a4df1423fb1ef
4
+ data.tar.gz: e1930099e96f86521138af568396cc90a561a6902fe830921c4b2e91f274ef5f
5
5
  SHA512:
6
- metadata.gz: 29ab7e2c0c8d9aea6bf0ff222c1c0fad81fd077d5b06315186bf152e00d77b32ca121adb1c2542f8c26cb03803705caed99237046aea118ee4286e89de288bf6
7
- data.tar.gz: 479e0d22d8ff085defebb333916ec103a681f5940a32df4ea42f892fe51b62a3bd6baac495a2ff6d6827f1e3707e846e69fd91a783c68cd13369c78e7bc25161
6
+ metadata.gz: f9ba27537563ede2af48698e8c2aead81872fbe00752f7f177d66af8a0ad003216061f1b07054ad02f4a0d57582331597b2e32118b8b44a8bf97db10b927ef7c
7
+ data.tar.gz: 4543eb2929914244b81e5bad736ccb3821f0fb963f240a7163c47b0a111936ee3a3b4f0eabe98289dcd68f634f9d60778e2fbc2360a0e40805bd8b85d125b17a
@@ -22,14 +22,6 @@ module EacRubyUtils
22
22
  Parser.new(self, args).data
23
23
  end
24
24
 
25
- def initialize2(args, positional, _default_options)
26
- @positional = positional.dup
27
- @positional.freeze
28
- @args = args
29
-
30
- @options_found = false
31
- end
32
-
33
25
  class Parser
34
26
  attr_reader :data, :arguments_consumer
35
27
 
@@ -74,7 +66,7 @@ module EacRubyUtils
74
66
  end
75
67
 
76
68
  def invalid_argument(arg, message)
77
- raise InvalidArgumentError, self, arg, message
69
+ raise InvalidArgumentError.new(self, arg, message)
78
70
  end
79
71
  end
80
72
 
@@ -1,13 +1,60 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/callbacks'
4
+ require 'eac_ruby_utils/arguments_consumer'
5
+ require 'ostruct'
4
6
 
5
7
  module EacRubyUtils
6
8
  class CommonConstructor
7
- attr_reader :args, :options
9
+ attr_reader :args, :options, :after_set_block
8
10
 
9
- def initialize(*args)
10
- @args = args
11
+ class << self
12
+ def parse_args_options(args)
13
+ result = ::OpenStruct.new(args: [], options: {})
14
+ options_reached = false
15
+ args.each do |arg|
16
+ raise "Options reached and there is more arguments (Arguments: #{args})" if
17
+ options_reached
18
+
19
+ options_reached = parse_arg_options_process_arg(result, arg)
20
+ end
21
+ result
22
+ end
23
+
24
+ private
25
+
26
+ def parse_arg_options_process_arg(result, arg)
27
+ if arg.is_a?(::Hash)
28
+ result.options = arg
29
+ true
30
+ else
31
+ result.args << arg
32
+ false
33
+ end
34
+ end
35
+ end
36
+
37
+ def initialize(*args, &after_set_block)
38
+ args_processed = self.class.parse_args_options(args)
39
+ @args = args_processed.args
40
+ @options = args_processed.options
41
+ @after_set_block = after_set_block
42
+ end
43
+
44
+ def args_count
45
+ (args_count_min..args_count_max)
46
+ end
47
+
48
+ def args_count_min
49
+ args.count - default_values.count
50
+ end
51
+
52
+ def args_count_max
53
+ args.count
54
+ end
55
+
56
+ def default_values
57
+ options[:default] || []
11
58
  end
12
59
 
13
60
  def setup_class(klass)
@@ -27,28 +74,60 @@ module EacRubyUtils
27
74
  end
28
75
 
29
76
  def setup_class_initialize(klass)
77
+ common_constructor = self
30
78
  klass.include(::ActiveSupport::Callbacks)
31
79
  klass.define_callbacks :initialize
32
- klass.class_eval initialize_method_code, __FILE__, __LINE__
80
+ klass.send(:define_method, :initialize) do |*args|
81
+ Initialize.new(common_constructor, args, self).run
82
+ end
33
83
  end
34
84
 
35
- def initialize_method_code
36
- b = "def initialize(#{initialize_method_args_code})\n"
37
- b += " run_callbacks :initialize do\n"
38
- initialize_method_args.each do |arg|
39
- b += " self.#{arg} = #{arg}\n"
85
+ class Initialize
86
+ attr_reader :common_constructor, :args, :object
87
+
88
+ def initialize(common_constructor, args, object)
89
+ @common_constructor = common_constructor
90
+ @args = args
91
+ @object = object
40
92
  end
41
- b += " end\n"
42
- b += "end\n"
43
- b
44
- end
45
93
 
46
- def initialize_method_args_code
47
- initialize_method_args.join(', ')
48
- end
94
+ def run
95
+ validate_args_count
96
+ object.run_callbacks :initialize do
97
+ object_attributes_set
98
+ object_after_callback
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def arg_value(arg_name)
105
+ arg_index = common_constructor.args.index(arg_name)
106
+ if arg_index < args.count
107
+ args[arg_index]
108
+ else
109
+ common_constructor.default_values[arg_index - common_constructor.args_count_min]
110
+ end
111
+ end
49
112
 
50
- def initialize_method_args
51
- args
113
+ def object_after_callback
114
+ return unless common_constructor.after_set_block
115
+
116
+ object.instance_eval(&common_constructor.after_set_block)
117
+ end
118
+
119
+ def object_attributes_set
120
+ common_constructor.args.each do |arg_name|
121
+ object.send("#{arg_name}=", arg_value(arg_name))
122
+ end
123
+ end
124
+
125
+ def validate_args_count
126
+ return if common_constructor.args_count.include?(args.count)
127
+
128
+ raise ArgumentError, "#{object.class}.initialize: wrong number of arguments" \
129
+ " (given #{args.count}, expected #{common_constructor.args_count})"
130
+ end
52
131
  end
53
132
  end
54
133
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.22.0'
4
+ VERSION = '0.23.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.22.0
4
+ version: 0.23.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: 2020-03-06 00:00:00.000000000 Z
11
+ date: 2020-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport