eac_ruby_utils 0.56.0 → 0.57.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: 9359fc40aed41fdc0eb2b7570bff084403a0a1aec9803d3581e2cc1b27f5318c
4
- data.tar.gz: '0749a1a5ec6ec7e31701f8cf18af86a8fd0a8cf6f3e7d62df1d41a568e91394c'
3
+ metadata.gz: e9a5ba0cb4987def93f005feb8c7d109afe4f5e202939d9a5db9e64eb8d08194
4
+ data.tar.gz: 9c7632467e434195994afdf69ada8ea8820e5df639709e79cae7cb8543169129
5
5
  SHA512:
6
- metadata.gz: c87bcbfdbc771869f7654eb28e2cf77e48e0ef70e2981be895c228332608c2aa05f90fd4cadcee926749870da742fa6f9061ad4c1424bdeba3cc5eb06fbabaf0
7
- data.tar.gz: df6e1db72f930687085bde679b38d141edc7cea9de95d9126cb2d87d155d402ccce0581dad5e8abf1bbd5831633dec6c896b2c143cb354ab5c8c44b05401d7cb
6
+ metadata.gz: 92cfc25fcb83ec39aff6136acd20fc30a5a9eacb493765ba034f6fa9291e022c931b55d80350fefe605a126a7f9c61b75e6e2b78e6d24b0b627912fde7a4c0fc
7
+ data.tar.gz: bedc1cd2d75146ca942894d9105e4c6ce02283996c7b1192cd26f93024895feb3a3b02c5dcab0e2f50d0ef5c273c3cdd494d576f55078aa1d5c27712a1679a0a
@@ -17,15 +17,25 @@ module EacRubyUtils
17
17
  end
18
18
 
19
19
  def run
20
- setup = self
21
20
  a_module.extend(::ActiveSupport::Concern)
22
- a_module.included do
23
- ::EacRubyUtils::CommonConcern::ClassSetup.new(setup, self, :include).run
24
- end
25
- a_module.prepended do
26
- ::EacRubyUtils::CommonConcern::ClassSetup.new(setup, self, :prepend).run
21
+ include_or_prepend(:included, :include)
22
+ include_or_prepend(:prepended, :prepend)
23
+ end
24
+
25
+ private
26
+
27
+ def include_or_prepend(module_method, class_setup_method)
28
+ setup = self
29
+ a_module.send(module_method, *a_module_method_args(module_method)) do
30
+ ::EacRubyUtils::CommonConcern::ClassSetup.new(setup, self, class_setup_method).run
27
31
  end
28
32
  end
33
+
34
+ def a_module_method_args(module_method)
35
+ method_arity = a_module.method(module_method).arity
36
+ method_arity = -method_arity - 1 if method_arity.negative?
37
+ method_arity.times.map { |_n| a_module }
38
+ end
29
39
  end
30
40
  end
31
41
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'active_support/callbacks'
4
4
  require 'eac_ruby_utils/arguments_consumer'
5
+ require 'eac_ruby_utils/common_constructor/class_initialize'
5
6
  require 'ostruct'
6
7
 
7
8
  module EacRubyUtils
@@ -76,111 +77,13 @@ module EacRubyUtils
76
77
  end
77
78
 
78
79
  def setup_class_initialize(klass)
79
- common_constructor = self
80
80
  klass.include(::ActiveSupport::Callbacks)
81
81
  klass.define_callbacks :initialize
82
- klass.send(:define_method, :initialize) do |*args|
83
- Initialize.new(common_constructor, args, self).run
84
- super(*SuperArgs.new(common_constructor, args, self).result)
85
- end
82
+ ::EacRubyUtils::CommonConstructor::ClassInitialize.new(self, klass).run
86
83
  end
87
84
 
88
85
  def super_args
89
86
  options[:super_args]
90
87
  end
91
-
92
- class Initialize
93
- attr_reader :common_constructor, :args, :object
94
-
95
- def initialize(common_constructor, args, object)
96
- @common_constructor = common_constructor
97
- @args = args
98
- @object = object
99
- end
100
-
101
- def run
102
- validate_args_count
103
- object.run_callbacks :initialize do
104
- object_attributes_set
105
- object_after_callback
106
- end
107
- end
108
-
109
- private
110
-
111
- def arg_value(arg_name)
112
- arg_index = common_constructor.args.index(arg_name)
113
- if arg_index < args.count
114
- args[arg_index]
115
- else
116
- common_constructor.default_values[arg_index - common_constructor.args_count_min]
117
- end
118
- end
119
-
120
- def object_after_callback
121
- return unless common_constructor.after_set_block
122
-
123
- object.instance_eval(&common_constructor.after_set_block)
124
- end
125
-
126
- def object_attributes_set
127
- common_constructor.args.each do |arg_name|
128
- object.send("#{arg_name}=", arg_value(arg_name))
129
- end
130
- end
131
-
132
- def validate_args_count
133
- return if common_constructor.args_count.include?(args.count)
134
-
135
- raise ArgumentError, "#{object.class}.initialize: wrong number of arguments" \
136
- " (given #{args.count}, expected #{common_constructor.args_count})"
137
- end
138
- end
139
-
140
- class SuperArgs
141
- attr_reader :common_constructor, :args, :object
142
-
143
- def initialize(common_constructor, args, object)
144
- @common_constructor = common_constructor
145
- @args = args
146
- @object = object
147
- end
148
-
149
- def auto_result
150
- r = []
151
- sub_args.each do |name, value|
152
- i = super_arg_index(name)
153
- r[i] = value if i
154
- end
155
- r
156
- end
157
-
158
- def result
159
- result_from_options || auto_result
160
- end
161
-
162
- def result_from_options
163
- return unless common_constructor.super_args
164
-
165
- object.instance_exec(&common_constructor.super_args)
166
- end
167
-
168
- def sub_args
169
- common_constructor.args.each_with_index.map do |name, index|
170
- [name, args[index]]
171
- end.to_h
172
- end
173
-
174
- def super_arg_index(name)
175
- super_method.parameters.each_with_index do |arg, index|
176
- return index if arg[1] == name
177
- end
178
- nil
179
- end
180
-
181
- def super_method
182
- object.class.superclass ? object.class.superclass.instance_method(:initialize) : nil
183
- end
184
- end
185
88
  end
186
89
  end
@@ -0,0 +1,29 @@
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 ClassInitialize
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 run
17
+ class_initialize = self
18
+ klass.send(:define_method, :initialize) do |*args|
19
+ ::EacRubyUtils::CommonConstructor::InstanceInitialize.new(
20
+ class_initialize.common_constructor, args, self
21
+ ).run
22
+ super(*::EacRubyUtils::CommonConstructor::SuperArgs.new(
23
+ class_initialize, args, self
24
+ ).result)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class CommonConstructor
5
+ class InstanceInitialize
6
+ attr_reader :common_constructor, :args, :object
7
+
8
+ def initialize(common_constructor, args, object)
9
+ @common_constructor = common_constructor
10
+ @args = args
11
+ @object = object
12
+ end
13
+
14
+ def run
15
+ validate_args_count
16
+ object.run_callbacks :initialize do
17
+ object_attributes_set
18
+ object_after_callback
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def arg_value(arg_name)
25
+ arg_index = common_constructor.args.index(arg_name)
26
+ if arg_index < args.count
27
+ args[arg_index]
28
+ else
29
+ common_constructor.default_values[arg_index - common_constructor.args_count_min]
30
+ end
31
+ end
32
+
33
+ def object_after_callback
34
+ return unless common_constructor.after_set_block
35
+
36
+ object.instance_eval(&common_constructor.after_set_block)
37
+ end
38
+
39
+ def object_attributes_set
40
+ common_constructor.args.each do |arg_name|
41
+ object.send("#{arg_name}=", arg_value(arg_name))
42
+ end
43
+ end
44
+
45
+ def validate_args_count
46
+ return if common_constructor.args_count.include?(args.count)
47
+
48
+ raise ArgumentError, "#{object.class}.initialize: wrong number of arguments" \
49
+ " (given #{args.count}, expected #{common_constructor.args_count})"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/module/delegation'
4
+
5
+ module EacRubyUtils
6
+ class CommonConstructor
7
+ class SuperArgs
8
+ attr_reader :class_initialize, :args, :object
9
+ delegate :common_constructor, to: :class_initialize
10
+
11
+ def initialize(class_initialize, args, object)
12
+ @class_initialize = class_initialize
13
+ @args = args
14
+ @object = object
15
+ end
16
+
17
+ def auto_result
18
+ r = []
19
+ sub_args.each do |name, value|
20
+ i = super_arg_index(name)
21
+ r[i] = value if i
22
+ end
23
+ r
24
+ end
25
+
26
+ def result
27
+ result_from_options || auto_result
28
+ end
29
+
30
+ def result_from_options
31
+ return unless common_constructor.super_args
32
+
33
+ object.instance_exec(&common_constructor.super_args)
34
+ end
35
+
36
+ def sub_args
37
+ common_constructor.args.each_with_index.map do |name, index|
38
+ [name, args[index]]
39
+ end.to_h
40
+ end
41
+
42
+ def super_arg_index(name)
43
+ super_method.parameters.each_with_index do |arg, index|
44
+ return index if arg[1] == name
45
+ end
46
+ nil
47
+ end
48
+
49
+ def super_method
50
+ class_initialize.klass.superclass&.instance_method(:initialize)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -38,7 +38,7 @@ module EacRubyUtils
38
38
  def run_with_subcommand
39
39
  if subcommand_name
40
40
  check_valid_subcommand
41
- subcommand.run
41
+ subcommand_run
42
42
  else
43
43
  run_without_subcommand
44
44
  end
@@ -52,6 +52,15 @@ module EacRubyUtils
52
52
  )
53
53
  end
54
54
 
55
+ def subcommand_run
56
+ if !subcommand.is_a?(::EacRubyUtils::Console::DocoptRunner) &&
57
+ subcommand.respond_to?(:run_run)
58
+ subcommand.run_run
59
+ else
60
+ subcommand.run
61
+ end
62
+ end
63
+
55
64
  def target_doc
56
65
  super.gsub(SUBCOMMANDS_MACRO,
57
66
  "#{target_doc_subcommand_arg} [#{SUBCOMMAND_ARGS_ARG}...]") +
@@ -57,8 +57,6 @@ module EacRubyUtils
57
57
  user_check_file(path)
58
58
  elsif path.directory?
59
59
  inner_check_directory(path, level)
60
- else
61
- raise "Unknown filesystem object: #{path}"
62
60
  end
63
61
  end
64
62
 
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support/inflector'
4
+
3
5
  module EacRubyUtils
4
6
  class Inflector
5
7
  class << self
6
8
  VARIABLE_NAME_PATTERN = /[_a-z][_a-z0-9]*/i.freeze
7
9
 
8
10
  def variableize(string)
9
- r = string.gsub(/[^_a-z0-9]/i, '_').gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
11
+ r = ::ActiveSupport::Inflector.transliterate(string).gsub(/[^_a-z0-9]/i, '_')
12
+ .gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
10
13
  m = VARIABLE_NAME_PATTERN.match(r)
11
14
  return r if m
12
15
 
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'active_support/values/time_zone'
5
+
6
+ module EacRubyUtils
7
+ module LocalTimeZone
8
+ DEBIAN_CONFIG_PATH = '/etc/timezone'
9
+
10
+ class << self
11
+ TIMEDATECTL_TIMEZONE_LINE_PATTERN = %r{\s*Time zone:\s*(\S+/\S+)\s}.freeze
12
+
13
+ def auto
14
+ %w[tz_env debian_config offset].lazy.map { |s| send("by_#{s}") }.find(&:present?)
15
+ end
16
+
17
+ def by_debian_config
18
+ path = ::Pathname.new(DEBIAN_CONFIG_PATH)
19
+ path.exist? ? path.read.strip.presence : nil
20
+ end
21
+
22
+ def by_offset
23
+ ::ActiveSupport::TimeZone[::Time.now.getlocal.gmt_offset].name
24
+ end
25
+
26
+ def by_timedatectl
27
+ executable = ::EacRubyUtils::Envs.local.executable('timedatectl', '--version')
28
+ return nil unless executable.exist?
29
+
30
+ TIMEDATECTL_TIMEZONE_LINE_PATTERN.if_match(executable.command.execute!) { |m| m[1] }
31
+ end
32
+
33
+ def by_tz_env
34
+ ENV['TZ'].presence
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'eac_ruby_utils/templates/searcher'
5
+
6
+ class Module
7
+ def template
8
+ @template ||= ::EacRubyUtils::Templates::Searcher.default.template(name.underscore)
9
+ end
10
+ end
@@ -1,15 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string/inflections'
4
- require 'eac_ruby_utils/templates/searcher'
3
+ require 'eac_ruby_utils/patches/module/template'
5
4
 
6
5
  class Object
7
- class << self
8
- def template
9
- @template ||= ::EacRubyUtils::Templates::Searcher.default.template(name.underscore)
10
- end
11
- end
12
-
13
6
  def template
14
7
  self.class.template
15
8
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'eac_ruby_utils/patches/time/local_time_zone'
3
+ require 'active_support/core_ext/time/zones'
4
+ require 'eac_ruby_utils/local_time_zone'
4
5
 
5
- ::Time.zone = ::Time.local_time_zone
6
+ ::Time.zone = ::EacRubyUtils::LocalTimeZone.auto
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.56.0'
4
+ VERSION = '0.57.2'
5
5
  end
@@ -14,10 +14,18 @@ module EacRubyUtils
14
14
  ::YAML.dump(sanitize(object))
15
15
  end
16
16
 
17
+ def dump_file(path, object)
18
+ ::File.write(path.to_s, dump(object))
19
+ end
20
+
17
21
  def load(string)
18
22
  ::YAML.safe_load(string, permitted_classes)
19
23
  end
20
24
 
25
+ def load_file(path)
26
+ load(::File.read(path.to_s))
27
+ end
28
+
21
29
  def permitted_classes
22
30
  DEFAULT_PERMITTED_CLASSES
23
31
  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.56.0
4
+ version: 0.57.2
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-12-23 00:00:00.000000000 Z
11
+ date: 2021-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -117,6 +117,9 @@ files:
117
117
  - lib/eac_ruby_utils/common_concern/class_setup.rb
118
118
  - lib/eac_ruby_utils/common_concern/module_setup.rb
119
119
  - lib/eac_ruby_utils/common_constructor.rb
120
+ - lib/eac_ruby_utils/common_constructor/class_initialize.rb
121
+ - lib/eac_ruby_utils/common_constructor/instance_initialize.rb
122
+ - lib/eac_ruby_utils/common_constructor/super_args.rb
120
123
  - lib/eac_ruby_utils/configs.rb
121
124
  - lib/eac_ruby_utils/configs/base.rb
122
125
  - lib/eac_ruby_utils/configs/file.rb
@@ -183,6 +186,7 @@ files:
183
186
  - lib/eac_ruby_utils/listable/string_list.rb
184
187
  - lib/eac_ruby_utils/listable/symbol_list.rb
185
188
  - lib/eac_ruby_utils/listable/value.rb
189
+ - lib/eac_ruby_utils/local_time_zone.rb
186
190
  - lib/eac_ruby_utils/on_clean_ruby_environment.rb
187
191
  - lib/eac_ruby_utils/options_consumer.rb
188
192
  - lib/eac_ruby_utils/patch.rb
@@ -206,6 +210,7 @@ files:
206
210
  - lib/eac_ruby_utils/patches/module/patch.rb
207
211
  - lib/eac_ruby_utils/patches/module/require_sub.rb
208
212
  - lib/eac_ruby_utils/patches/module/simple_cache.rb
213
+ - lib/eac_ruby_utils/patches/module/template.rb
209
214
  - lib/eac_ruby_utils/patches/object.rb
210
215
  - lib/eac_ruby_utils/patches/object/asserts.rb
211
216
  - lib/eac_ruby_utils/patches/object/debug.rb
@@ -221,7 +226,6 @@ files:
221
226
  - lib/eac_ruby_utils/patches/string/inflector.rb
222
227
  - lib/eac_ruby_utils/patches/time.rb
223
228
  - lib/eac_ruby_utils/patches/time/default_time_zone_set.rb
224
- - lib/eac_ruby_utils/patches/time/local_time_zone.rb
225
229
  - lib/eac_ruby_utils/paths_hash.rb
226
230
  - lib/eac_ruby_utils/paths_hash/entry_key_error.rb
227
231
  - lib/eac_ruby_utils/paths_hash/node.rb
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_support/core_ext/time/zones'
4
- require 'eac_ruby_utils/envs'
5
-
6
- class Time
7
- class << self
8
- TIMEDATECTL_TIMEZONE_LINE_PATTERN = %r{\s*Time zone:\s*(\S+/\S+)\s}.freeze
9
-
10
- def local_time_zone
11
- local_time_zone_by_timedatectl || local_time_zone_by_offset
12
- end
13
-
14
- def local_time_zone_by_timedatectl
15
- executable = ::EacRubyUtils::Envs.local.executable('timedatectl', '--version')
16
- return nil unless executable.exist?
17
-
18
- TIMEDATECTL_TIMEZONE_LINE_PATTERN.if_match(executable.command.execute!) { |m| m[1] }
19
- end
20
-
21
- def local_time_zone_by_offset
22
- ::ActiveSupport::TimeZone[::Time.now.getlocal.gmt_offset].name
23
- end
24
- end
25
- end