eac_ruby_utils 0.34.0 → 0.38.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: 4a1ec180f3a7aef3fae244443ddd7d9650402e4a7f26757068ee318dce2e9cc9
4
- data.tar.gz: bf7cb26680fb44edcf31b169dd8014dc0bb7f2e7ccbfee664a480751a11fc967
3
+ metadata.gz: 5fc852bc0ba01d1706a2f6c6dc7a0a09b1725b3dc3e54741abbf4c1d2022cac0
4
+ data.tar.gz: 288360947e10bbef4f1b4cf100306e8bfcb7b5e146adead41184c96e4c66c2d3
5
5
  SHA512:
6
- metadata.gz: 8c97935d7790e791ffa864420a353abe203751af236dd494a91729a27f7caaaf796430025a3a11e3e32023e8330570cd2bb1c328be2702caaaa7110a62b9a8cf
7
- data.tar.gz: acbb32cb1398e8fb4b36eaf367cf222a7de7d92f2afbca5dd3de3747d77a2e2c9b7ea4ad72e4852e6e79bd69dd336e6a46cd32a67717cf0be00ef60c6fc45866
6
+ metadata.gz: 579594be75a7160855c0b530cd4b5d9bf8f918f065b5e2d4938274d2395dd47f14ee7b827c8461fa6775a7962936ae729d90320124398d3f8220eb0c914243eb
7
+ data.tar.gz: d287cd78c95da6e3d28c628c6059af23b3710d70f150672cbe13c47e80caa2989b3c93b084292e253380ef342128f780cc9b302cc09dfd36d9a0142ebf9a564b
@@ -20,17 +20,11 @@ module EacRubyUtils
20
20
  load
21
21
  end
22
22
 
23
- def clear
24
- file.clear
25
- end
23
+ delegate :clear, to: :file
26
24
 
27
- def save
28
- file.save
29
- end
25
+ delegate :save, to: :file
30
26
 
31
- def load
32
- file.load
33
- end
27
+ delegate :load, to: :file
34
28
 
35
29
  def []=(entry_key, entry_value)
36
30
  write_entry(entry_key, entry_value)
@@ -48,9 +42,7 @@ module EacRubyUtils
48
42
  file.read(entry_key)
49
43
  end
50
44
 
51
- def autosave?
52
- file.autosave?
53
- end
45
+ delegate :autosave?, to: :file
54
46
 
55
47
  private
56
48
 
@@ -85,7 +85,7 @@ module EacRubyUtils
85
85
  def looped_entry_value_from_input(entry_key, options)
86
86
  loop do
87
87
  entry_value = entry_value_from_input(entry_key, options)
88
- next unless entry_value.present?
88
+ next if entry_value.blank?
89
89
  next if options[:validator] && !options[:validator].call(entry_value)
90
90
 
91
91
  return entry_value
@@ -10,7 +10,7 @@ module EacRubyUtils
10
10
  new(options).send(:run)
11
11
  rescue Docopt::Exit => e
12
12
  STDERR.write(e.message + "\n")
13
- ::Kernel.exit(DOCOPT_ERROR_EXIT_CODE)
13
+ ::Kernel.exit(DOCOPT_ERROR_EXIT_CODE) # rubocop:disable Rails/Exit
14
14
  end
15
15
  end
16
16
  end
@@ -26,7 +26,7 @@ module EacRubyUtils
26
26
 
27
27
  def fatal_error(string)
28
28
  puts "ERROR: #{string}".white.on_red
29
- Kernel.exit 1
29
+ Kernel.exit 1 # rubocop:disable Rails/Exit
30
30
  end
31
31
 
32
32
  def title(string)
@@ -54,6 +54,7 @@ module EacRubyUtils
54
54
  end
55
55
 
56
56
  def execute!(options = {})
57
+ options[:exit_outputs] = status_results.merge(options[:exit_outputs].presence || {})
57
58
  er = ExecuteResult.new(execute(options), options)
58
59
  return er.result if er.success?
59
60
 
@@ -16,6 +16,10 @@ module EacRubyUtils
16
16
  duplicate_by_extra_options(envvars: envvars.merge(name => value))
17
17
  end
18
18
 
19
+ def status_result(status_code, result)
20
+ duplicate_by_extra_options(status_results: status_results.merge(status_code => result))
21
+ end
22
+
19
23
  def pipe(other_command)
20
24
  duplicate_by_extra_options(pipe: other_command)
21
25
  end
@@ -28,6 +32,10 @@ module EacRubyUtils
28
32
  extra_options[:envvars] ||= {}.with_indifferent_access
29
33
  end
30
34
 
35
+ def status_results
36
+ extra_options[:status_results] ||= {}.with_indifferent_access
37
+ end
38
+
31
39
  def append_envvars(command)
32
40
  e = envvars.map { |k, v| "#{Shellwords.escape(k)}=#{Shellwords.escape(v)}" }.join(' ')
33
41
  e.present? ? "#{e} #{command}" : command
@@ -12,6 +12,10 @@ module EacRubyUtils
12
12
  apply_constants
13
13
  end
14
14
 
15
+ def each_value(&block)
16
+ values.each(&block)
17
+ end
18
+
15
19
  def values
16
20
  @values.values.map(&:value)
17
21
  end
@@ -4,6 +4,7 @@ require 'active_support/hash_with_indifferent_access'
4
4
  require 'active_support/core_ext/string/inflections'
5
5
  require_relative 'integer_list'
6
6
  require_relative 'string_list'
7
+ require_relative 'symbol_list'
7
8
 
8
9
  module EacRubyUtils
9
10
  module Listable
@@ -14,18 +15,10 @@ module EacRubyUtils
14
15
  @source = source
15
16
  end
16
17
 
17
- def add_integer(item, *labels)
18
- check_acts_as_listable_new_item(item)
19
- acts_as_listable_items[item] = ::EacRubyUtils::Listable::IntegerList.new(
20
- self, item, labels
21
- )
22
- end
23
-
24
- def add_string(item, *labels)
25
- check_acts_as_listable_new_item(item)
26
- acts_as_listable_items[item] = ::EacRubyUtils::Listable::StringList.new(
27
- self, item, labels
28
- )
18
+ %w[integer string symbol].each do |list_type|
19
+ define_method "add_#{list_type}" do |item, *labels|
20
+ add(::EacRubyUtils::Listable.const_get("#{list_type}_list".camelize), item, labels)
21
+ end
29
22
  end
30
23
 
31
24
  def method_missing(name, *args, &block)
@@ -43,6 +36,11 @@ module EacRubyUtils
43
36
 
44
37
  private
45
38
 
39
+ def add(list_class, item, labels)
40
+ check_acts_as_listable_new_item(item)
41
+ acts_as_listable_items[item] = list_class.new(self, item, labels)
42
+ end
43
+
46
44
  def check_acts_as_listable_new_item(item)
47
45
  return unless acts_as_listable_items.key?(item)
48
46
 
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'list'
4
+
5
+ module EacRubyUtils
6
+ module Listable
7
+ class SymbolList < ::EacRubyUtils::Listable::List
8
+ protected
9
+
10
+ def parse_labels(labels)
11
+ if labels.first.is_a?(Hash)
12
+ Hash[labels.first.map { |k, v| [k.to_sym, v.to_sym] }]
13
+ else
14
+ Hash[labels.map { |v| [v.to_sym, v.to_sym] }]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -5,7 +5,7 @@ require 'active_support/core_ext/object/blank'
5
5
  class Object
6
6
  # @return +block.call(self)+ if +self+ is present, +default_value+ otherwise.
7
7
  def if_present(default_value = nil)
8
- return default_value unless present?
8
+ return default_value if blank?
9
9
 
10
10
  block_given? ? yield(self) : self
11
11
  end
@@ -33,9 +33,7 @@ module EacRubyUtils
33
33
  root.write_entry(self.class.parse_entry_key(entry_key), entry_value, [])
34
34
  end
35
35
 
36
- def to_h
37
- root.to_h
38
- end
36
+ delegate :to_h, to: :root
39
37
 
40
38
  private
41
39
 
@@ -10,6 +10,7 @@ module EacRubyUtils
10
10
  class RequireSub
11
11
  BASE_OPTION_KEY = :base
12
12
  INCLUDE_MODULES_OPTION_KEY = :include_modules
13
+ REQUIRE_DEPENDENCY_OPTION_KEY = :require_dependency
13
14
 
14
15
  attr_reader :file, :options
15
16
 
@@ -42,7 +43,11 @@ module EacRubyUtils
42
43
 
43
44
  def require_sub_files
44
45
  Dir["#{File.dirname(file)}/#{::File.basename(file, '.*')}/*.rb"].sort.each do |path|
45
- require path
46
+ if options[REQUIRE_DEPENDENCY_OPTION_KEY]
47
+ require_dependency path
48
+ else
49
+ require path
50
+ end
46
51
  end
47
52
  end
48
53
  end
@@ -10,7 +10,7 @@ module EacRubyUtils
10
10
  attr_reader :path
11
11
 
12
12
  def initialize(path)
13
- @path = path
13
+ @path = path.is_a?(::Pathname) ? path : ::Pathname.new(path.to_s)
14
14
  end
15
15
 
16
16
  def apply(variables_source, directory)
@@ -25,6 +25,12 @@ module EacRubyUtils
25
25
  raise "Child \"#{subpath}\" from \"#{path}\" not found"
26
26
  end
27
27
 
28
+ def children
29
+ path.children.map do |path_child|
30
+ child(path_child.basename.to_path)
31
+ end
32
+ end
33
+
28
34
  private
29
35
 
30
36
  def apply_fs_object(source_relative, target)
@@ -1,32 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/hash/indifferent_access'
4
- require 'eac_ruby_utils/simple_cache'
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'eac_ruby_utils/templates/variable_providers'
5
5
 
6
6
  module EacRubyUtils
7
7
  module Templates
8
8
  class File
9
- include ::EacRubyUtils::SimpleCache
10
-
11
9
  VARIABLE_DELIMITER = ::Regexp.quote('%%')
12
10
  VARIABLE_PATTERN = /#{VARIABLE_DELIMITER}([a-z0-9\._]*)#{VARIABLE_DELIMITER}/i.freeze
13
11
 
14
- attr_reader :path
15
-
16
- def initialize(path)
17
- @path = path
12
+ enable_simple_cache
13
+ common_constructor :path do
14
+ self.path = path.to_pathname
18
15
  end
19
16
 
20
17
  # +variables_provider+ A [Hash] or object which responds to +read_entry(entry_name)+.
21
18
  def apply(variables_source)
22
- variables_provider = build_variables_provider(variables_source)
19
+ variables_provider = ::EacRubyUtils::Templates::VariableProviders.build(variables_source)
23
20
  variables.inject(content) do |a, e|
24
21
  a.gsub(variable_pattern(e), variables_provider.variable_value(e).to_s)
25
22
  end
26
23
  end
27
24
 
28
25
  def apply_to_file(variables_source, output_file_path)
29
- ::File.write(output_file_path, apply(variables_source))
26
+ output_file_path.to_pathname.write(apply(variables_source))
30
27
  end
31
28
 
32
29
  private
@@ -38,64 +35,16 @@ module EacRubyUtils
38
35
  end
39
36
 
40
37
  def content_uncached
41
- ::File.read(path)
38
+ path.read
42
39
  end
43
40
 
44
41
  def sanitize_variable_name(variable_name)
45
42
  variable_name.to_s.downcase
46
43
  end
47
44
 
48
- def build_variables_provider(variables_source)
49
- return HashVariablesProvider.new(variables_source) if variables_source.is_a?(::Hash)
50
- return EntriesReaderVariablesProvider.new(variables_source) if
51
- variables_source.respond_to?(:read_entry)
52
-
53
- raise "Variables provider not found for #{variables_source}"
54
- end
55
-
56
45
  def variable_pattern(name)
57
46
  /#{VARIABLE_DELIMITER}#{::Regexp.quote(name)}#{VARIABLE_DELIMITER}/i
58
47
  end
59
-
60
- class BaseVariablesProvider
61
- attr_reader :source
62
-
63
- def initialize(source)
64
- @source = source
65
- end
66
-
67
- def variable_value(name)
68
- return variable_fetch(name) if variable_exist?(name)
69
-
70
- raise VariableNotFoundError, "Variable \"#{name}\" not found in #{source}"
71
- end
72
- end
73
-
74
- class HashVariablesProvider < BaseVariablesProvider
75
- def initialize(source)
76
- super(source.with_indifferent_access)
77
- end
78
-
79
- def variable_exist?(name)
80
- source.key?(name)
81
- end
82
-
83
- def variable_fetch(name)
84
- source.fetch(name)
85
- end
86
- end
87
-
88
- class EntriesReaderVariablesProvider < BaseVariablesProvider
89
- def variable_exist?(_name)
90
- true
91
- end
92
-
93
- def variable_fetch(name)
94
- source.read_entry(name)
95
- end
96
- end
97
-
98
- class VariableNotFoundError < StandardError; end
99
48
  end
100
49
  end
101
50
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Templates
5
+ class VariableNotFoundError < StandardError; end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRubyUtils
6
+ module Templates
7
+ module VariableProviders
8
+ require_sub __FILE__
9
+
10
+ PROVIDERS = %w[entries_reader hash generic].map do |name|
11
+ "eac_ruby_utils/templates/variable_providers/#{name}".camelize.constantize
12
+ end
13
+
14
+ class << self
15
+ def build(variables_source)
16
+ PROVIDERS.each do |provider|
17
+ return provider.new(variables_source) if provider.accept?(variables_source)
18
+ end
19
+
20
+ raise "Variables provider not found for #{variables_source}"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/templates/variable_not_found_error'
4
+
5
+ module EacRubyUtils
6
+ module Templates
7
+ module VariableProviders
8
+ class Base
9
+ attr_reader :source
10
+
11
+ def initialize(source)
12
+ @source = source
13
+ end
14
+
15
+ def variable_value(name)
16
+ return variable_fetch(name) if variable_exist?(name)
17
+
18
+ raise VariableNotFoundError, "Variable \"#{name}\" not found in #{source}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/templates/variable_providers/base'
4
+
5
+ module EacRubyUtils
6
+ module Templates
7
+ module VariableProviders
8
+ class EntriesReader < ::EacRubyUtils::Templates::VariableProviders::Base
9
+ class << self
10
+ def accept?(variables_source)
11
+ variables_source.respond_to?(:read_entry)
12
+ end
13
+ end
14
+
15
+ def variable_exist?(_name)
16
+ true
17
+ end
18
+
19
+ def variable_fetch(name)
20
+ source.read_entry(name)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/templates/variable_providers/base'
4
+
5
+ module EacRubyUtils
6
+ module Templates
7
+ module VariableProviders
8
+ class Generic < ::EacRubyUtils::Templates::VariableProviders::Base
9
+ class << self
10
+ def accept?(variables_source)
11
+ variables_source.is_a?(::Object)
12
+ end
13
+ end
14
+
15
+ def variable_exist?(name)
16
+ source.respond_to?(name)
17
+ end
18
+
19
+ def variable_fetch(name)
20
+ source.send(name)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/templates/variable_providers/base'
4
+
5
+ module EacRubyUtils
6
+ module Templates
7
+ module VariableProviders
8
+ class Hash < ::EacRubyUtils::Templates::VariableProviders::Base
9
+ class << self
10
+ def accept?(variables_source)
11
+ variables_source.is_a?(::Hash)
12
+ end
13
+ end
14
+
15
+ def initialize(source)
16
+ super(source.with_indifferent_access)
17
+ end
18
+
19
+ def variable_exist?(name)
20
+ source.key?(name)
21
+ end
22
+
23
+ def variable_fetch(name)
24
+ source.fetch(name)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.34.0'
4
+ VERSION = '0.38.0'
5
5
  end
@@ -3,10 +3,93 @@
3
3
  require 'yaml'
4
4
 
5
5
  module EacRubyUtils
6
+ # A safe YAML loader/dumper with common types included.
6
7
  class Yaml
7
8
  class << self
8
- def load_common(string)
9
- ::YAML.safe_load(string, [::Symbol, ::Date])
9
+ DEFAULT_PERMITTED_CLASSES = [::Array, ::Date, ::FalseClass, ::Hash, ::NilClass, ::Numeric,
10
+ ::String, ::Symbol, ::Time, ::TrueClass].freeze
11
+
12
+ def dump(object)
13
+ ::YAML.dump(sanitize(object))
14
+ end
15
+
16
+ def load(string)
17
+ ::YAML.safe_load(string, permitted_classes)
18
+ end
19
+
20
+ def permitted_classes
21
+ DEFAULT_PERMITTED_CLASSES
22
+ end
23
+
24
+ def sanitize(object)
25
+ Sanitizer.new(object).result
26
+ end
27
+
28
+ def yaml?(object)
29
+ return false unless object.is_a?(::String)
30
+ return false unless object.start_with?('---')
31
+
32
+ load(object)
33
+ true
34
+ rescue ::Psych::SyntaxError
35
+ false
36
+ end
37
+
38
+ class Sanitizer
39
+ attr_reader :source
40
+
41
+ RESULT_TYPES = %w[permitted enumerableable hashable].freeze
42
+
43
+ def initialize(source)
44
+ @source = source
45
+ end
46
+
47
+ def result
48
+ RESULT_TYPES.each do |type|
49
+ return send("result_#{type}") if send("result_#{type}?")
50
+ end
51
+
52
+ source.to_s
53
+ end
54
+
55
+ private
56
+
57
+ def result_enumerableable?
58
+ source.respond_to?(:to_a) && !source.is_a?(::Hash)
59
+ end
60
+
61
+ def result_enumerableable
62
+ source.to_a.map { |child| sanitize_value(child) }
63
+ end
64
+
65
+ def result_hashable?
66
+ source.respond_to?(:to_h)
67
+ end
68
+
69
+ def result_hashable
70
+ source.to_h.map { |k, v| [k.to_sym, sanitize_value(v)] }.to_h
71
+ end
72
+
73
+ def result_nil?
74
+ source.nil?
75
+ end
76
+
77
+ def result_nil
78
+ nil
79
+ end
80
+
81
+ def result_permitted?
82
+ (::EacRubyUtils::Yaml.permitted_classes - [::Array, ::Hash])
83
+ .any? { |klass| source.is_a?(klass) }
84
+ end
85
+
86
+ def result_permitted
87
+ source
88
+ end
89
+
90
+ def sanitize_value(value)
91
+ self.class.new(value).result
92
+ end
10
93
  end
11
94
  end
12
95
  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.34.0
4
+ version: 0.38.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-06-01 00:00:00.000000000 Z
11
+ date: 2020-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -155,6 +155,7 @@ files:
155
155
  - lib/eac_ruby_utils/listable/list.rb
156
156
  - lib/eac_ruby_utils/listable/lists.rb
157
157
  - lib/eac_ruby_utils/listable/string_list.rb
158
+ - lib/eac_ruby_utils/listable/symbol_list.rb
158
159
  - lib/eac_ruby_utils/listable/value.rb
159
160
  - lib/eac_ruby_utils/on_clean_ruby_environment.rb
160
161
  - lib/eac_ruby_utils/options_consumer.rb
@@ -200,6 +201,12 @@ files:
200
201
  - lib/eac_ruby_utils/templates/directory.rb
201
202
  - lib/eac_ruby_utils/templates/file.rb
202
203
  - lib/eac_ruby_utils/templates/searcher.rb
204
+ - lib/eac_ruby_utils/templates/variable_not_found_error.rb
205
+ - lib/eac_ruby_utils/templates/variable_providers.rb
206
+ - lib/eac_ruby_utils/templates/variable_providers/base.rb
207
+ - lib/eac_ruby_utils/templates/variable_providers/entries_reader.rb
208
+ - lib/eac_ruby_utils/templates/variable_providers/generic.rb
209
+ - lib/eac_ruby_utils/templates/variable_providers/hash.rb
203
210
  - lib/eac_ruby_utils/version.rb
204
211
  - lib/eac_ruby_utils/yaml.rb
205
212
  homepage: