eac_ruby_utils 0.43.0 → 0.46.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: 17c2233b153a30506d6d62a3e8d89e35b9233d9ddb7043a4641a52ee214c2382
4
- data.tar.gz: 7369e5d33ca13dd1827371d8fb257bf3ddab56bcd0a12b997eb6835349a94d61
3
+ metadata.gz: 2d83be9e72d46a6f6c42134ef7f19c403a428ba6ddbdf9ba49e67b0c653d391e
4
+ data.tar.gz: f89cde0244962f5f0ec23d0fa03c59f285968cf07f7e67565d259e31e5416802
5
5
  SHA512:
6
- metadata.gz: afc7ada9f7bd0f0c260d058db8d755d6778fa8d76e4dd549e8a0b7d6b6aac755ff18229da447bb2d8a6cac831b8d4a9c7585755fe16ed03ef6214653ee7b3f85
7
- data.tar.gz: 4f227e60bcf60d4598a15cb4b8748696a602d3490d282e0ae2b4ca5546eb8161e4b011f22ab8647b39409a6bf5fb8055f78f4b6373c41e57c6351821274bfe1d
6
+ metadata.gz: c8843e2fd8ffc283f92e92948bbc511feafc6f14e2f96e0b10aa7dcfa8382c38c86bed6806536cc5f0bd679f723e712ad0d9370cc26277b421fc00b1c41f206f
7
+ data.tar.gz: c16518b0ffe008079584f31becedb049001546c07bae0470325e842c3137e6b58ae46b918d409dc0bc56c4e1d26d543e75f5abac3ce353fbcf89bae6cde7bb83
@@ -21,10 +21,10 @@ module EacRubyUtils
21
21
 
22
22
  class Setup
23
23
  include ::EacRubyUtils::SimpleCache
24
- attr_reader :a_module, :common_constructor
24
+ attr_reader :a_module, :common_concern
25
25
 
26
- def initialize(common_constructor, a_module)
27
- @common_constructor = common_constructor
26
+ def initialize(common_concern, a_module)
27
+ @common_concern = common_concern
28
28
  @a_module = a_module
29
29
  end
30
30
 
@@ -47,7 +47,7 @@ module EacRubyUtils
47
47
  end
48
48
 
49
49
  def setup_after_callback(base)
50
- common_constructor.after_callback.if_present do |v|
50
+ common_concern.after_callback.if_present do |v|
51
51
  base.instance_eval(&v)
52
52
  end
53
53
  end
@@ -14,6 +14,12 @@ module EacRubyUtils
14
14
  class DocoptRunner
15
15
  include ::EacRubyUtils::Contextualizable
16
16
 
17
+ class << self
18
+ def create(settings = {})
19
+ new(settings)
20
+ end
21
+ end
22
+
17
23
  attr_reader :settings
18
24
 
19
25
  def initialize(settings = {})
@@ -7,7 +7,7 @@ module EacRubyUtils
7
7
 
8
8
  class << self
9
9
  def run(options = {})
10
- new(options).send(:run)
10
+ create(options).send(:run)
11
11
  rescue Docopt::Exit => e
12
12
  STDERR.write(e.message + "\n")
13
13
  ::Kernel.exit(DOCOPT_ERROR_EXIT_CODE) # rubocop:disable Rails/Exit
@@ -25,6 +25,8 @@ module EacRubyUtils
25
25
  end
26
26
 
27
27
  module SubcommandsSupport
28
+ EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME = :extra_available_subcommands
29
+
28
30
  def check_subcommands_arg
29
31
  if subcommand_arg_as_list?
30
32
  singleton_class.include(SubcommandsSupport::SubcommandArgAsList)
@@ -43,7 +45,7 @@ module EacRubyUtils
43
45
  end
44
46
 
45
47
  def subcommand
46
- @subcommand ||= subcommand_class_name(subcommand_name).constantize.new(
48
+ @subcommand ||= subcommand_class_name(subcommand_name).constantize.create(
47
49
  argv: subcommand_args,
48
50
  program_name: subcommand_program,
49
51
  parent: self
@@ -77,7 +79,11 @@ module EacRubyUtils
77
79
  end
78
80
 
79
81
  def available_subcommands
80
- (setting_value(:subcommands, false) || auto_available_subcommands).sort
82
+ r = ::Set.new(setting_value(:subcommands, false) || auto_available_subcommands)
83
+ if respond_to?(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME, true)
84
+ r += send(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME)
85
+ end
86
+ r.sort
81
87
  end
82
88
 
83
89
  def auto_available_subcommands
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ class Inflector
5
+ class << self
6
+ VARIABLE_NAME_PATTERN = /[_a-z][_a-z0-9]*/i.freeze
7
+
8
+ def variableize(string)
9
+ r = string.gsub(/[^_a-z0-9]/i, '_').gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
10
+ m = VARIABLE_NAME_PATTERN.match(r)
11
+ return r if m
12
+
13
+ raise ::ArgumentError, "Invalid variable name \"#{r}\" was generated " \
14
+ "from string \"#{string}\""
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'eac_ruby_utils/inflector'
4
+
3
5
  module EacRubyUtils
4
6
  module Listable
5
7
  class Value
@@ -16,8 +18,7 @@ module EacRubyUtils
16
18
  end
17
19
 
18
20
  def constant_name
19
- "#{@list.item}_#{@key}".gsub(/[^a-z0-9_]/, '_').gsub(/_+/, '_')
20
- .gsub(/(?:\A_|_\z)/, '').upcase
21
+ ::EacRubyUtils::Inflector.variableize("#{@list.item}_#{@key}").upcase
21
22
  end
22
23
 
23
24
  def label
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/object/blank'
4
+
5
+ class Object
6
+ # @return +block.call(self.method_name)+ if +self+ responds to +method_name+, +default_value+
7
+ # otherwise.
8
+ def if_respond(method_name, default_value = nil)
9
+ return default_value unless respond_to?(method_name)
10
+
11
+ value = send(method_name)
12
+
13
+ block_given? ? yield(value) : value
14
+ end
15
+
16
+ # @return +yield+ if +self+ is blank.
17
+ def if_blank
18
+ return yield if blank? && block_given?
19
+
20
+ self
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub __FILE__
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/inflector'
4
+
5
+ class String
6
+ def variableize
7
+ ::EacRubyUtils::Inflector.variableize(self)
8
+ end
9
+ end
@@ -21,8 +21,12 @@ module EacRubyUtils
21
21
  end
22
22
  end
23
23
 
24
- def reset_cache
25
- @cache_keys = nil
24
+ def reset_cache(*keys)
25
+ if keys.any?
26
+ keys.each { |key| cache_keys.delete(key) }
27
+ else
28
+ @cache_keys = nil
29
+ end
26
30
  end
27
31
 
28
32
  private
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require 'active_support/core_ext/object/blank'
5
+
6
+ module EacRubyUtils
7
+ class Struct
8
+ def initialize(initial_data = {})
9
+ self.data = ::ActiveSupport::HashWithIndifferentAccess.new(initial_data)
10
+ end
11
+
12
+ def [](key)
13
+ key, bool = parse_key(key)
14
+ bool ? self[key].present? : data[key]
15
+ end
16
+
17
+ def fetch(key)
18
+ key, bool = parse_key(key)
19
+ bool ? fetch(key).present? : data.fetch(key)
20
+ end
21
+
22
+ def method_missing(method_name, *arguments, &block)
23
+ property_method?(method_name) ? fetch(method_name) : super
24
+ end
25
+
26
+ def respond_to_missing?(method_name, include_private = false)
27
+ property_method?(method_name) || super
28
+ end
29
+
30
+ def to_h
31
+ data.dup
32
+ end
33
+
34
+ delegate :to_s, to: :data
35
+
36
+ private
37
+
38
+ attr_accessor :data
39
+
40
+ def parse_key(key)
41
+ m = /\A(.+)\?\z/.match(key.to_s)
42
+ [(m ? m[1] : key.to_s).to_sym, m ? true : false]
43
+ end
44
+
45
+ def property_method?(key)
46
+ property_methods.include?(key.to_sym)
47
+ end
48
+
49
+ def property_methods
50
+ data.keys.flat_map { |k| [k.to_sym, "#{k}?".to_sym] }
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.43.0'
4
+ VERSION = '0.46.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.43.0
4
+ version: 0.46.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-07-25 00:00:00.000000000 Z
11
+ date: 2020-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -161,6 +161,7 @@ files:
161
161
  - lib/eac_ruby_utils/immutable/common_accessor.rb
162
162
  - lib/eac_ruby_utils/immutable/hash_accessor.rb
163
163
  - lib/eac_ruby_utils/immutable/instance_methods.rb
164
+ - lib/eac_ruby_utils/inflector.rb
164
165
  - lib/eac_ruby_utils/listable.rb
165
166
  - lib/eac_ruby_utils/listable/class_methods.rb
166
167
  - lib/eac_ruby_utils/listable/instance_methods.rb
@@ -192,12 +193,15 @@ files:
192
193
  - lib/eac_ruby_utils/patches/object.rb
193
194
  - lib/eac_ruby_utils/patches/object/asserts.rb
194
195
  - lib/eac_ruby_utils/patches/object/if_present.rb
196
+ - lib/eac_ruby_utils/patches/object/if_respond.rb
195
197
  - lib/eac_ruby_utils/patches/object/template.rb
196
198
  - lib/eac_ruby_utils/patches/object/to_pathname.rb
197
199
  - lib/eac_ruby_utils/patches/pathname.rb
198
200
  - lib/eac_ruby_utils/patches/pathname/basename_sub.rb
199
201
  - lib/eac_ruby_utils/patches/regexp.rb
200
202
  - lib/eac_ruby_utils/patches/regexp/if_match.rb
203
+ - lib/eac_ruby_utils/patches/string.rb
204
+ - lib/eac_ruby_utils/patches/string/inflector.rb
201
205
  - lib/eac_ruby_utils/patches/time.rb
202
206
  - lib/eac_ruby_utils/patches/time/default_time_zone_set.rb
203
207
  - lib/eac_ruby_utils/patches/time/local_time_zone.rb
@@ -211,6 +215,7 @@ files:
211
215
  - lib/eac_ruby_utils/ruby/on_clean_environment.rb
212
216
  - lib/eac_ruby_utils/settings_provider.rb
213
217
  - lib/eac_ruby_utils/simple_cache.rb
218
+ - lib/eac_ruby_utils/struct.rb
214
219
  - lib/eac_ruby_utils/templates.rb
215
220
  - lib/eac_ruby_utils/templates/directory.rb
216
221
  - lib/eac_ruby_utils/templates/file.rb
@@ -242,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
242
247
  - !ruby/object:Gem::Version
243
248
  version: '0'
244
249
  requirements: []
245
- rubygems_version: 3.0.6
250
+ rubygems_version: 3.0.8
246
251
  signing_key:
247
252
  specification_version: 4
248
253
  summary: Utilities for E.A.C.'s Ruby projects.