eac_ruby_utils 0.59.0 → 0.60.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: 548ad31ed6381e0a51802ea856dfa885fdaef16080305285698872baa9dc9b24
4
- data.tar.gz: ee4ddee8e898704ef248d82296d7cb63093a3ff8a72e929b6815c854e8e66e70
3
+ metadata.gz: 70fe181d734d1e97b9467b44302604bb7b2a61ef3102e08aeddd1a9dd9a5d6be
4
+ data.tar.gz: d04e61f84fd5c9fa22bca11be939732c23e352eb019ba7a2260c5ef3012b339d
5
5
  SHA512:
6
- metadata.gz: 2dcbd2e28932d4a287e94798511973b1ec0803838294f2eba2d5fb7ea5e7399ee5092d2bde598e5f403195c3832bdc57a4de2a9ba7514ac4f3c9ff5f23523243
7
- data.tar.gz: 8a3259581c69448604dcc452f7ede39ef6ba1eaf9081d609c837e16377aa11bdd0bba9100256226334b7863149a5d93ad43d4410b4d7b660ce61f6498850612c
6
+ metadata.gz: ce2f14b7c8cc05f34337212a54cf4ae4d33a44fb1cfb83c1365e5c86a08e4d4bd3395c29bc4cf4a09969d25760c0340094939c804956b4a5e586caeffdfaf4e7
7
+ data.tar.gz: 8b243bf7a7de4cadadbd3497df8ecf8ac97fbc16bbc084d9459e12f72244963c878043eaeb95c180cee10b6ead01c044530732196cdaf13a55997431f74e2816
@@ -1,15 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/string/inflections'
4
- require 'eac_ruby_utils/options_consumer'
3
+ require 'eac_ruby_utils/require_sub'
5
4
 
6
5
  module EacRubyUtils
7
6
  # Provide a option by constant, method or options object.
8
7
  module SettingsProvider
8
+ ::EacRubyUtils.require_sub __FILE__, base: self
9
+
9
10
  def setting_constant_name(key, fullname = false)
10
- name = key.to_s.underscore.upcase
11
- name = "#{self.class.name}::#{name}" if fullname
12
- name
11
+ setting_value_instance(key).constant_name(fullname)
13
12
  end
14
13
 
15
14
  def setting_search_order
@@ -25,41 +24,23 @@ module EacRubyUtils
25
24
  end
26
25
 
27
26
  def setting_value(key, options = {})
28
- options = parse_setting_value_options(options)
29
- options.order.each do |method|
30
- value = send("setting_value_by_#{method}", key)
31
- return value if value
32
- end
33
- return nil unless options.required
34
-
35
- raise "Setting \"#{key}\" not found. Supply in #{settings_object_name}, implement a " \
36
- "\"#{key}\" method or declare a #{setting_constant_name(key, true)} constant."
27
+ setting_value_instance(key, options).value
37
28
  end
38
29
 
39
30
  def setting_value_by_constant(key)
40
- constant_name = setting_constant_name(key)
41
- begin
42
- self.class.const_get(constant_name)
43
- rescue NameError
44
- nil
45
- end
31
+ setting_value_instance(key).value_by_constant
46
32
  end
47
33
 
48
34
  def setting_value_by_method(key)
49
- respond_to?(key) ? send(key) : nil
35
+ setting_value_instance(key).value_by_method
50
36
  end
51
37
 
52
38
  def setting_value_by_settings_object(key)
53
- settings_object[key.to_s] || settings_object[key.to_sym]
39
+ setting_value_instance(key).value_by_settings_object
54
40
  end
55
41
 
56
- private
57
-
58
- def parse_setting_value_options(options)
59
- r = ::EacRubyUtils::OptionsConsumer.new(options).consume_all(:required, :order, ostruct: true)
60
- r.required = true if r.required.nil?
61
- r.order = setting_search_order if r.order.nil?
62
- r
42
+ def setting_value_instance(key, options = {})
43
+ ::EacRubyUtils::SettingsProvider::SettingValue.new(self, key, options)
63
44
  end
64
45
  end
65
46
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'eac_ruby_utils/listable'
5
+ require 'eac_ruby_utils/simple_cache'
6
+ require 'eac_ruby_utils/struct'
7
+
8
+ module EacRubyUtils
9
+ module SettingsProvider
10
+ class SettingValue
11
+ include ::EacRubyUtils::Listable
12
+ include ::EacRubyUtils::SimpleCache
13
+
14
+ attr_reader :source, :key, :options
15
+ lists.add_symbol :option, :default, :order, :required
16
+
17
+ def initialize(source, key, options)
18
+ @source = source
19
+ @key = key
20
+ @options = options
21
+ end
22
+
23
+ def constant_name(fullname = false)
24
+ name = key.to_s.underscore.upcase
25
+ name = "#{source.class.name}::#{name}" if fullname
26
+ name
27
+ end
28
+
29
+ def value
30
+ parsed_options.order.each do |method|
31
+ value = send("value_by_#{method}")
32
+ return value if value
33
+ end
34
+ return parsed_options.default if parsed_options.respond_to?(OPTION_DEFAULT)
35
+ return nil unless parsed_options.required
36
+
37
+ raise_key_not_found
38
+ end
39
+
40
+ def value_by_constant
41
+ source.class.const_get(constant_name)
42
+ rescue NameError
43
+ nil
44
+ end
45
+
46
+ def value_by_method
47
+ source.respond_to?(key, true) ? source.send(key) : nil
48
+ end
49
+
50
+ def value_by_settings_object
51
+ source.settings_object[key.to_s] || source.settings_object[key.to_sym]
52
+ end
53
+
54
+ private
55
+
56
+ def parsed_options_uncached
57
+ r = self.class.lists.option.hash_keys_validate!(options.symbolize_keys)
58
+ r[:required] = true unless r.key?(OPTION_REQUIRED)
59
+ r[:order] = source.setting_search_order if r[OPTION_ORDER].nil?
60
+ ::EacRubyUtils::Struct.new(r)
61
+ end
62
+
63
+ def raise_key_not_found
64
+ raise "Setting \"#{key}\" not found. Supply in #{source.settings_object_name}, implement "\
65
+ "a \"#{key}\" method or declare a #{constant_name(true)} constant."
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.59.0'
4
+ VERSION = '0.60.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.59.0
4
+ version: 0.60.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: 2021-02-21 00:00:00.000000000 Z
11
+ date: 2021-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -242,6 +242,7 @@ files:
242
242
  - lib/eac_ruby_utils/ruby/command.rb
243
243
  - lib/eac_ruby_utils/ruby/on_clean_environment.rb
244
244
  - lib/eac_ruby_utils/settings_provider.rb
245
+ - lib/eac_ruby_utils/settings_provider/setting_value.rb
245
246
  - lib/eac_ruby_utils/simple_cache.rb
246
247
  - lib/eac_ruby_utils/struct.rb
247
248
  - lib/eac_ruby_utils/templates.rb