eco-helpers 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,85 @@
1
+ module Eco
2
+ module Common
3
+ class MetaThor
4
+ class Pipe
5
+ SYM = "!"
6
+ SYMS = [SYM]
7
+
8
+ class << self
9
+ #def rex_pipes
10
+ # alternatives = Regex.escape(SYMS).join("|")
11
+ # /(#{alternatives})/g
12
+ #end
13
+
14
+ def type(str)
15
+ case str
16
+ when SYM
17
+ :standard
18
+ else
19
+ :unkown
20
+ end
21
+ end
22
+
23
+ def pipe?(str)
24
+ SYMS.include?(str)
25
+ end
26
+
27
+ def piped?(args)
28
+ args.any? do |arg|
29
+ pipe?(arg)
30
+ end
31
+ end
32
+
33
+ def split(args, pipe_to_sym: false)
34
+ args = (args && [args].flatten) || []
35
+ if piped?(args)
36
+ args.reduce([[]]) do |groups,arg|
37
+ cg = []
38
+ cg = pipe?(arg) ? cg : groups.last
39
+ cg.push(arg) if arg
40
+
41
+ groups.push(cg) if pipe?(arg)
42
+ groups
43
+ end
44
+ else
45
+ [].push(args)
46
+ end
47
+ end
48
+ end
49
+
50
+ attr_reader :type, :command
51
+
52
+ def initialize(command:)
53
+ @command = command
54
+ args = @command.source_args
55
+ @type = self.class.type(args.shift)
56
+ end
57
+
58
+ def standard?
59
+ @type == :standard
60
+ end
61
+
62
+ def command_group
63
+ command.group
64
+ end
65
+
66
+ def index
67
+ command.index
68
+ end
69
+
70
+ def input_index
71
+ return nil unless index > 0
72
+ case type
73
+ when :standard
74
+ index - 1
75
+ end
76
+ end
77
+
78
+ def input
79
+ command_group[input_index].output if input_index
80
+ end
81
+
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1 @@
1
+ require_relative 'thor/command'
@@ -0,0 +1,36 @@
1
+ class Thor
2
+ class Command
3
+
4
+ # By default, a command invokes a method in the thor class. You can change this
5
+ # implementation to create custom commands.
6
+ # Source: https://github.com/erikhuda/thor/blob/master/lib/thor/command.rb
7
+ # Trace:
8
+ # 1. Base_Class.start
9
+ # 2. Thor_Class.dispatch
10
+ # - (new) Base.initialize -> with options
11
+ # 3. Base_Instance.invoke_command (invocation.rb)
12
+ # 4. Command_Instance.run
13
+ # @note: modified so it integrates input/output for custom Pipe
14
+ def run(instance, args = [])
15
+ arity = nil
16
+
17
+ if private_method?(instance)
18
+ result = instance.class.handle_no_command_error(name)
19
+ elsif public_method?(instance)
20
+ arity = instance.method(name).arity
21
+ result = instance.__send__(name, *args)
22
+ elsif local_method?(instance, :method_missing)
23
+ result = instance.__send__(:method_missing, name.to_sym, *args)
24
+ else
25
+ result = instance.class.handle_no_command_error(name)
26
+ end
27
+
28
+ result
29
+ rescue ArgumentError => e
30
+ handle_argument_error?(instance, e, caller) ? instance.class.handle_argument_error(self, e, args, arity) : (raise e)
31
+ rescue NoMethodError => e
32
+ handle_no_method_error?(instance, e, caller) ? instance.class.handle_no_command_error(name) : (raise e)
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,54 @@
1
+ module Eco
2
+ module Common
3
+ class MetaThor
4
+ class Value
5
+ KEY = "MetaThorValue"
6
+ KEY_OPTION = "MetaThorJSON"
7
+
8
+ class << self
9
+ def to_value(doc)
10
+ case
11
+ when doc.is_a?(Hash)
12
+ case
13
+ when doc.key?(KEY_OPTION)
14
+ to_value(doc[KEY_OPTION])
15
+ when doc.key?(KEY)
16
+ doc[KEY]
17
+ else
18
+ doc
19
+ end
20
+ when doc.is_a?(String)
21
+ to_value(JSON.parse(doc))
22
+ when doc.is_a?(Value)
23
+ value.value
24
+ end
25
+ end
26
+
27
+ def key?(object)
28
+
29
+ end
30
+ end
31
+
32
+ attr_reader :value
33
+
34
+ def initialize(value)
35
+ @doc = {"#{KEY}" => value}
36
+ end
37
+
38
+ def value
39
+ @doc[KEY]
40
+ end
41
+
42
+ def as_input_option
43
+ "--#{INPUT_OPTION}=#{KEY_OPTION}:#{@doc.to_json}"
44
+ end
45
+
46
+ def as_option
47
+ {"#{INPUT_SYM}" => self}
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+ end
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "0.6.4"
2
+ VERSION = "0.6.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eco-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -219,6 +219,7 @@ extra_rdoc_files: []
219
219
  files:
220
220
  - ".gitignore"
221
221
  - ".rspec"
222
+ - ".yardopts"
222
223
  - README.md
223
224
  - eco-helpers.gemspec
224
225
  - lib/eco-helpers.rb
@@ -278,6 +279,7 @@ files:
278
279
  - lib/eco/api/usecases/case_data.rb
279
280
  - lib/eco/api/usecases/default_cases.rb
280
281
  - lib/eco/api/usecases/default_cases/change_email_case.rb
282
+ - lib/eco/api/usecases/default_cases/create_case.rb
281
283
  - lib/eco/api/usecases/default_cases/create_details_case.rb
282
284
  - lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb
283
285
  - lib/eco/api/usecases/default_cases/delete_case.rb
@@ -293,19 +295,29 @@ files:
293
295
  - lib/eco/api/usecases/default_cases/set_supervisor_case.rb
294
296
  - lib/eco/api/usecases/default_cases/switch_supervisor_case.rb
295
297
  - lib/eco/api/usecases/default_cases/to_csv_case.rb
298
+ - lib/eco/api/usecases/default_cases/update_case.rb
296
299
  - lib/eco/api/usecases/default_cases/update_details_case.rb
297
- - lib/eco/api/usecases/default_cases/upsert_account_case.rb
300
+ - lib/eco/api/usecases/default_cases/upsert_case.rb
298
301
  - lib/eco/api/usecases/use_case.rb
299
302
  - lib/eco/api/usecases/use_group.rb
300
303
  - lib/eco/cli.rb
301
- - lib/eco/cli/input.rb
302
- - lib/eco/cli/input_multi.rb
304
+ - lib/eco/cli/api.rb
303
305
  - lib/eco/cli/root.rb
304
306
  - lib/eco/cli/session.rb
305
307
  - lib/eco/cli/session/batch.rb
306
308
  - lib/eco/common.rb
307
309
  - lib/eco/common/base_cli.rb
310
+ - lib/eco/common/base_cli_backup.rb
308
311
  - lib/eco/common/language.rb
312
+ - lib/eco/common/meta_thor.rb
313
+ - lib/eco/common/meta_thor/command_group.rb
314
+ - lib/eco/common/meta_thor/command_unit.rb
315
+ - lib/eco/common/meta_thor/input_backup.rb
316
+ - lib/eco/common/meta_thor/input_multi_backup.rb
317
+ - lib/eco/common/meta_thor/pipe.rb
318
+ - lib/eco/common/meta_thor/thor.rb
319
+ - lib/eco/common/meta_thor/thor/command.rb
320
+ - lib/eco/common/meta_thor/value.rb
309
321
  - lib/eco/data.rb
310
322
  - lib/eco/data/crypto.rb
311
323
  - lib/eco/data/crypto/encryption.rb
@@ -1,35 +0,0 @@
1
- module Eco
2
- module API
3
- module UseCases
4
- class DefaultCases
5
- class UpsertAccountCase < BaseCase
6
-
7
- def process
8
- @cases.define("upsert-account", type: :sync) do |entries, people, session, options|
9
- creation = session.job_group("main").new("create", type: :create, sets: [:core, :account])
10
- update = session.job_group("main").new("update", type: :update, sets: [:core, :account])
11
-
12
- entries.each.with_index do |entry, i|
13
- create = false
14
- unless person = people.find(entry)
15
- create = true
16
- person = session.new_person
17
- entry.set_core(person)
18
- #entry.set_details(person)
19
- end
20
-
21
- entry.set_account(person)
22
- person.account.permissions_custom = session.new_preset(person)
23
- person.account.send_invites = false if options.key?(:send_invites) && !options(:send_invites)
24
-
25
- creation.add(person) if create
26
- update.add(person) unless create
27
- end
28
- end
29
- end
30
-
31
- end
32
- end
33
- end
34
- end
35
- end
data/lib/eco/cli/input.rb DELETED
@@ -1,109 +0,0 @@
1
- module Eco
2
- module CLI
3
- class Input
4
- MODES = [:both, :json, :value]
5
-
6
- attr_reader :mode
7
-
8
- def initialize(value, mode = :both)
9
- @mode = mode
10
- @mode = :both unless MODES.include?(mode)
11
-
12
- @doc = {}
13
- @doc['value'] = {} if self.value?
14
- @doc['json'] = {} if self.json?
15
-
16
- self.value = value
17
- end
18
-
19
- def to_h
20
- @doc
21
- end
22
-
23
- def json?
24
- @mode == :json || @mode = :both
25
- end
26
-
27
- def value?
28
- @mode == :value || @mode = :both
29
- end
30
-
31
- def json
32
- @doc['json'] if self.json?
33
- end
34
-
35
- def parse
36
- JSON.parse(self.json) if self.json?
37
- end
38
-
39
- def json=(value)
40
- @doc['value'] = value if self.value?
41
- @doc['json'] = to_json(value) if self.json?
42
- end
43
-
44
- def value
45
- return @doc['value'] if self.value?
46
- self.parse
47
- end
48
-
49
- def value=(value)
50
- @doc['value'] = value if self.value?
51
- @doc['json'] = to_json(value) if self.json?
52
- end
53
-
54
- def present?(key)
55
- self[key]&.present?
56
- end
57
-
58
- def empty?
59
- return @doc['value'].keys.length == 0 if self.value?
60
- @doc['json'].keys.length == 0
61
- end
62
-
63
- # merging implies that @mode is equalized to the lesser
64
- # => :both.merge(:json) == :json ; :value.merge(:both) == :value mode
65
- # => :json.merge(:value) will raise an exception
66
- def merge(value)
67
- merge_hash(hash(value))
68
- self
69
- end
70
-
71
- private
72
-
73
- def to_json(value)
74
- if value.respond_to?(:as_json)
75
- input.as_json
76
- elsif value.respond_to?(:to_json)
77
- value.to_json
78
- end
79
- end
80
-
81
- def doc_mode(doc)
82
- case doc
83
- when String
84
- doc = JSON.parse(doc)
85
- return nil unless doc.is_a?(Hash)
86
- doc_mode(doc)
87
- when CliInput
88
- doc.mode
89
- when Hash
90
- mod_json = doc.key?('json')
91
- mod_value = doc.key?('value')
92
- mod_json ? (mod_value ? :both : :json) : (mod_value ? :value : nil)
93
- end
94
- end
95
-
96
- def hash(value)
97
- case value
98
- when String
99
- JSON.parse(value)
100
- when CliInput
101
- value.to_h
102
- when Hash
103
- value
104
- end
105
- end
106
-
107
- end
108
- end
109
- end
@@ -1,137 +0,0 @@
1
- module Eco
2
- module CLI
3
- class MultiInput
4
- MODES = [:both, :json, :value]
5
-
6
- attr_reader :mode
7
-
8
- def initialize(doc = {}, mode = :both)
9
- @mode = doc_mode(doc) || mode
10
- @mode = :both unless MODES.include?(@mode)
11
-
12
- @doc = {}
13
- @doc['value'] = {} if self.value?
14
- @doc['json'] = {} if self.json?
15
-
16
- self.merge(doc) unless doc.empty?
17
- end
18
-
19
- def to_h
20
- @doc
21
- end
22
-
23
- def json?
24
- @mode == :json || @mode = :both
25
- end
26
-
27
- def value?
28
- @mode == :value || @mode = :both
29
- end
30
-
31
- def json(key)
32
- @doc.dig('json', key)
33
- end
34
-
35
- def [](key)
36
- return @doc.dig('value', key) if self.value?
37
- JSON.parse(@doc.dig('json', key))
38
- end
39
-
40
- def []=(key, value)
41
- @doc['value'][key] = value if self.value?
42
- @doc['json'][key] = to_json(value) if self.json?
43
- end
44
-
45
- def delete(key)
46
- if self.key?(key)
47
- value = @doc['value'].delete(key) if self.value?
48
- value2 = JSON.parse(@doc['json'].delete(key)) if self.json?
49
- return value if self.value?
50
- value2
51
- end
52
- end
53
-
54
- def key?(key)
55
- return @doc['value'].key?(key) if self.value?
56
- @doc['json'].key?(key)
57
- end
58
-
59
- def present?(key)
60
- self[key]&.present?
61
- end
62
-
63
- def empty?
64
- return @doc['value'].keys.length == 0 if self.value?
65
- @doc['json'].keys.length == 0
66
- end
67
-
68
- # merging implies that @mode is equalized to the lesser
69
- # => :both.merge(:json) == :json ; :value.merge(:both) == :value mode
70
- # => :json.merge(:value) will raise an exception
71
- def merge(value)
72
- merge_hash(hash(value))
73
- self
74
- end
75
-
76
- private
77
-
78
- def to_json(value)
79
- if value.respond_to?(:as_json)
80
- input.as_json
81
- elsif value.respond_to?(:to_json)
82
- value.to_json
83
- end
84
- end
85
-
86
- def doc_mode(doc)
87
- case doc
88
- when String
89
- doc = JSON.parse(doc)
90
- return nil unless doc.is_a?(Hash)
91
- doc_mode(doc)
92
- when CliInput
93
- doc.mode
94
- when Hash
95
- mod_json = doc.key?('json')
96
- mod_value = doc.key?('value')
97
- mod_json ? (mod_value ? :both : :json) : (mod_value ? :value : nil)
98
- end
99
- end
100
-
101
- def hash(value)
102
- case value
103
- when String
104
- JSON.parse(value)
105
- when CliInput
106
- value.to_h
107
- when Hash
108
- value
109
- end
110
- end
111
-
112
- def merge_hash(value)
113
- err_input = "Error: merge requires a correct Hashable value or a #{self.class.name} instance."
114
- err_incompatible = "Error: can't merge value mode with json mode."
115
- raise err_input unless value.is_a?(Hash)
116
- return unless !value.empty?
117
-
118
- raise err_input if ! (mode = doc_mode(value))
119
- raise err_incompatible if ! (new_mode = resolve_mode(mode))
120
- @mode = new_mode
121
-
122
- @doc.delete('json') unless self.json?
123
- @doc.delete('value') unless self.value?
124
-
125
- @doc['json'].merge(value['json']) if self.json?
126
- @doc['value'].merge(value['value']) if self.value?
127
- end
128
-
129
- def resolve_mode(mode)
130
- return mode if @mode == :both
131
- return @mode if mode == :both
132
- return mode if @mode == mode
133
- end
134
-
135
- end
136
- end
137
- end