eac_ruby_utils 0.7.0 → 0.8.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: 11c321836d0bd034a1ebd2c4835cbd97bcb423edf7a96c362a18259c2eeb698f
4
- data.tar.gz: 4601661dd0057f7e2bd1dbe7b5adae2463f8419370848dd33bf48b2f8ab55860
3
+ metadata.gz: '0119c50e5869164d29e5dc579df784a5e3576a613a4ea5c597a2ee6dcda212fe'
4
+ data.tar.gz: c69eea7c7587d93d44e82da0b37a110e2ec5fb77dfac4e83ecf2d028fd10657f
5
5
  SHA512:
6
- metadata.gz: 0eeb4ee5d8f011a9a4607dbb94932092969465a1b530e8712df7b955b553d0d465dfbeb8427a5820e6262609be3f30b2f38f8850579839f4d0b7c3126e9f2979
7
- data.tar.gz: 79e0b183401615aeb3f16d92c4b3505eca0d6569f8fa5b6eeabbe6d7e5853cab7cdad24eac03401e6083cf8d8c2cb7693df6c196e42f554f158de8facfb2cbcf
6
+ metadata.gz: d7890f0ffebd4a457ba27eb4cd5ca5df500d19e74103039c441d4ff5c3a26685c6c5a239652e69b99a7007e4688e2a79c275c78b1b019f1ebbedf8f49a9e0f9a
7
+ data.tar.gz: 4ed10a42a2b890e6995f67931648029e5f94c6de8b7cc9bf5de57f028169a0e7e122289c53f6b115a37879d2f742acb5b38edd686f93aae62614ffefddcd0102
@@ -5,6 +5,7 @@ module EacRubyUtils
5
5
  require 'eac_ruby_utils/console'
6
6
  require 'eac_ruby_utils/contextualizable'
7
7
  require 'eac_ruby_utils/envs'
8
+ require 'eac_ruby_utils/listable'
8
9
  require 'eac_ruby_utils/options_consumer'
9
10
  require 'eac_ruby_utils/patches'
10
11
  require 'eac_ruby_utils/simple_cache'
@@ -32,7 +32,7 @@ module EacRubyUtils
32
32
  end
33
33
 
34
34
  def run_with_subcommand
35
- if options.fetch(SUBCOMMAND_ARG)
35
+ if subcommand_name
36
36
  check_valid_subcommand
37
37
  subcommand.run
38
38
  else
@@ -74,10 +74,6 @@ module EacRubyUtils
74
74
  subcommand_name
75
75
  end
76
76
 
77
- def subcommand_name
78
- options.fetch(SUBCOMMAND_ARG)
79
- end
80
-
81
77
  def available_subcommands
82
78
  (setting_value(:subcommands, false) || auto_available_subcommands).sort
83
79
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ Dir["#{__dir__}/#{::File.basename(__FILE__, '.*')}/*.rb"].each do |partial|
5
+ require partial
6
+ end
7
+
8
+ module EacRubyUtils
9
+ module Listable
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ extend(ClassMethods)
14
+ include(InstanceMethods)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
4
+ require_relative 'lists'
5
+
6
+ module EacRubyUtils
7
+ module Listable
8
+ module ClassMethods
9
+ def lists
10
+ @lists ||= ::EacRubyUtils::Listable::Lists.new(self)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Listable
5
+ module InstanceMethods
6
+ LISTABLE_INSTANCE_VALUE_METHODS = %w[label description].freeze
7
+
8
+ def method_missing(name, *args, &block)
9
+ list, method = parse_method(name)
10
+ list && method ? list.instance_value(self).send(method) : super
11
+ end
12
+
13
+ def respond_to_missing?(name, include_all = false)
14
+ list, method = parse_method(name)
15
+ list && method ? true : super
16
+ end
17
+
18
+ private
19
+
20
+ def parse_method(method)
21
+ self.class.lists.acts_as_listable_items.each do |item, list|
22
+ LISTABLE_INSTANCE_VALUE_METHODS.each do |m|
23
+ return [list, m] if method.to_s == "#{item}_#{m}"
24
+ end
25
+ end
26
+ [nil, nil]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'list'
4
+
5
+ module EacRubyUtils
6
+ module Listable
7
+ class IntegerList < ::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_i, v.to_s] }]
13
+ else
14
+ Hash[labels.each_with_index.map { |v, i| [i + 1, v.to_s] }]
15
+ end
16
+ end
17
+
18
+ def build_value(index, _key)
19
+ index + 1
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Listable
5
+ class List
6
+ attr_reader :item
7
+
8
+ def initialize(lists, item, labels)
9
+ @lists = lists
10
+ @item = item
11
+ @values = build_values(labels)
12
+ apply_constants
13
+ end
14
+
15
+ def values
16
+ @values.values.map(&:value)
17
+ end
18
+
19
+ def options
20
+ @values.values.map { |v| [v.label, v.value] }
21
+ end
22
+
23
+ def method_missing(name, *args, &block)
24
+ list = find_list_by_method(name)
25
+ list ? list : super
26
+ end
27
+
28
+ def respond_to_missing?(name, include_all = false)
29
+ find_list_by_method(name) || super
30
+ end
31
+
32
+ def i18n_key
33
+ "eac_ruby_utils.listable.#{class_i18n_key}.#{item}"
34
+ end
35
+
36
+ def instance_value(instance)
37
+ v = instance.send(item)
38
+ return @values[v] if @values.key?(v)
39
+ raise "List value unkown: #{v} (Source: #{@lists.source}, Item: #{item})"
40
+ end
41
+
42
+ private
43
+
44
+ def class_i18n_key
45
+ @lists.source.name.underscore.to_sym
46
+ end
47
+
48
+ def find_list_by_method(method)
49
+ @values.values.each do |v|
50
+ return v if method.to_s == "value_#{v.key}"
51
+ end
52
+ nil
53
+ end
54
+
55
+ def constants
56
+ labels.each_with_index.map { |v, i| ["#{item.upcase}_#{v.upcase}", values[i]] }
57
+ end
58
+
59
+ def apply_constants
60
+ @values.values.each do |v|
61
+ @lists.source.const_set(v.constant_name, v.value)
62
+ end
63
+ end
64
+
65
+ def build_values(labels)
66
+ vs = {}
67
+ parse_labels(labels).each do |value, key|
68
+ v = Value.new(self, value, key)
69
+ vs[v.value] = v
70
+ end
71
+ vs
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require 'active_support/core_ext/string/inflections'
5
+ require_relative 'integer_list'
6
+ require_relative 'string_list'
7
+
8
+ module EacRubyUtils
9
+ module Listable
10
+ class Lists
11
+ attr_reader :source
12
+
13
+ def initialize(source)
14
+ @source = source
15
+ end
16
+
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
+ )
29
+ end
30
+
31
+ def method_missing(name, *args, &block)
32
+ list = find_list_by_method(name)
33
+ list ? list : super
34
+ end
35
+
36
+ def respond_to_missing?(name, include_all = false)
37
+ find_list_by_method(name) || super
38
+ end
39
+
40
+ def acts_as_listable_items
41
+ @acts_as_listable_items ||= ActiveSupport::HashWithIndifferentAccess.new
42
+ end
43
+
44
+ private
45
+
46
+ def check_acts_as_listable_new_item(item)
47
+ return unless acts_as_listable_items.key?(item)
48
+ raise "Item já adicionado anteriormente: #{item} em #{self} " \
49
+ "(#{acts_as_listable_items.keys})"
50
+ end
51
+
52
+ def find_list_by_method(method)
53
+ acts_as_listable_items.each do |item, list|
54
+ return list if method.to_sym == item.to_sym
55
+ end
56
+ nil
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'list'
4
+
5
+ module EacRubyUtils
6
+ module Listable
7
+ class StringList < ::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_s, v.to_s] }]
13
+ else
14
+ Hash[labels.map { |v| [v.to_s, v.to_s] }]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRubyUtils
4
+ module Listable
5
+ class Value
6
+ attr_reader :value, :key
7
+
8
+ def initialize(list, value, key)
9
+ @list = list
10
+ @value = value
11
+ @key = key
12
+ end
13
+
14
+ def to_s
15
+ "I: #{@list.item}, V: #{@value}, K: #{@key}"
16
+ end
17
+
18
+ def constant_name
19
+ "#{@list.item}_#{@key}".gsub(/[^a-z0-9_]/, '_').gsub(/_+/, '_')
20
+ .gsub(/(?:\A_|_\z)/, '').upcase
21
+ end
22
+
23
+ def label
24
+ translate('label')
25
+ end
26
+
27
+ def description
28
+ translate('description')
29
+ end
30
+
31
+ private
32
+
33
+ def translate(translate_key)
34
+ ::I18n.t("#{@list.i18n_key}.#{@key}.#{translate_key}")
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.7.0'
4
+ VERSION = '0.8.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.7.0
4
+ version: 0.8.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: 2019-07-23 00:00:00.000000000 Z
11
+ date: 2019-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -118,6 +118,14 @@ files:
118
118
  - lib/eac_ruby_utils/envs/local_env.rb
119
119
  - lib/eac_ruby_utils/envs/process.rb
120
120
  - lib/eac_ruby_utils/envs/ssh_env.rb
121
+ - lib/eac_ruby_utils/listable.rb
122
+ - lib/eac_ruby_utils/listable/class_methods.rb
123
+ - lib/eac_ruby_utils/listable/instance_methods.rb
124
+ - lib/eac_ruby_utils/listable/integer_list.rb
125
+ - lib/eac_ruby_utils/listable/list.rb
126
+ - lib/eac_ruby_utils/listable/lists.rb
127
+ - lib/eac_ruby_utils/listable/string_list.rb
128
+ - lib/eac_ruby_utils/listable/value.rb
121
129
  - lib/eac_ruby_utils/options_consumer.rb
122
130
  - lib/eac_ruby_utils/patches.rb
123
131
  - lib/eac_ruby_utils/patches/hash.rb