eco-helpers 0.9.1 → 0.9.2

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: c1c15ec5f243e3ddbf220b7177458666205cbd03aa2cd7b42ae49c75b7309c41
4
- data.tar.gz: 3f46b25d6aff9988339d4e331356b9cf9e168a944ec7f7ae7ac171d660413499
3
+ metadata.gz: f3aec55ff6424177729ff863f6652dfe53fecd2df84111036d336e216f9df441
4
+ data.tar.gz: 86faa0568a8d3bc393cbbe4db62bb367230035de12230739c68891d551ae16b7
5
5
  SHA512:
6
- metadata.gz: 5fa870fa6491d12d458696895882b63895bf3dc5e2ca86ce58dd6ad476ec63e012b4a58809244f47ac09e674139dfcfe6e68fc32e052dbd248c145d0f9edb5c6
7
- data.tar.gz: 692681ee1f899a6d6509289c99b291a379c20daa6b34155a6faa903e43badddca393644dc10dc07e00eaa4f96e0b6f83da0b31bc8764c70713b5c3f824a686fb
6
+ metadata.gz: 49f68f024a1e129502ec505b79b945feec5f40a99dfa8c7ebb45ea9699c889efffaad23b1e0c08302032a92cc117a0fb9ecf404cb68cdf6d576deb283d63118e
7
+ data.tar.gz: 64777d1d3c04e878538f70b41fd0fc67ec4a951c1d59ade2300a96174c006ff68d1a2c12c9c661b57bc75af5e71e8d690c1e8239664373b426328d4e247a674d
@@ -14,6 +14,10 @@ module Eco
14
14
  @caches_init = false
15
15
  end
16
16
 
17
+ def to_json
18
+ to_a.to_json
19
+ end
20
+
17
21
  def [](id_or_ext)
18
22
  id(id_or_ext) || external_id(id_or_ext)
19
23
  end
@@ -1,8 +1,8 @@
1
1
  module Eco
2
2
  module API
3
3
  class Session < Common::Session::BaseSession
4
- attr_reader :batch
5
- attr_reader :job_groups
4
+ attr_reader :batch, :job_groups
5
+ attr_reader :usecases
6
6
  attr_accessor :schema
7
7
 
8
8
  def initialize(init = {})
@@ -18,7 +18,7 @@ module Eco
18
18
  @task = Task.new(enviro)
19
19
  @job_groups = JobGroups.new(enviro)
20
20
 
21
- @use_cases = Eco::API::UseCases::DefaultCases.new.merge(config.usecases)
21
+ @usecases = Eco::API::UseCases::DefaultCases.new.merge(config.usecases)
22
22
 
23
23
  self.schema = config.people.default_schema || schemas.first
24
24
  @policy_groups = config.policy_groups
@@ -142,9 +142,9 @@ module Eco
142
142
 
143
143
  def process_case(name, type: nil, **params)
144
144
  args = { session: self }.merge(params)
145
- fatal("Undefined usecase '#{name}' of type '#{type.to_s}'") if !@use_cases.defined?(name, type: type)
145
+ fatal("Undefined usecase '#{name}' of type '#{type.to_s}'") if !usecases.defined?(name, type: type)
146
146
  logger.debug("Session: going to process '#{name}' defined case")
147
- @use_cases.case(name, type: type).launch(**args)
147
+ usecases.case(name, type: type).launch(**args)
148
148
  end
149
149
 
150
150
  def job_group(name, order: :last)
@@ -23,6 +23,13 @@ module Eco
23
23
  @jobs.key?(name)
24
24
  end
25
25
 
26
+ def job(name, type: nil, sets: nil, usecase: nil, &block)
27
+ new(name, type: type, sets: sets, usecase: usecase, &block) unless exists?(name)
28
+ self[name].tap do |job|
29
+ block.call(job) if block
30
+ end
31
+ end
32
+
26
33
  def new(name, type:, sets:, usecase: nil)
27
34
  fatal "Can't create job named '#{name}' because it already exists." if exists?(name)
28
35
 
@@ -176,7 +176,7 @@ module Eco
176
176
  private
177
177
 
178
178
  def person_ref(entry)
179
- "(id: '#{get_attr(entry, :id)}')'#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
179
+ "(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
180
180
  end
181
181
 
182
182
  def get_attr(entry, attr)
@@ -188,6 +188,10 @@ module Eco
188
188
  @schemas = Eco::API::Organization::PersonSchemas.new(schs)
189
189
  end
190
190
 
191
+ def session
192
+ @session ||= Eco::API::Session.new(self)
193
+ end
194
+
191
195
  # PEOPLE
192
196
  def discarded_people_file=(value)
193
197
  people.discarded_file = value
@@ -12,6 +12,10 @@ module Eco
12
12
  apis.keys.length > 0
13
13
  end
14
14
 
15
+ def enviros
16
+ apis.keys
17
+ end
18
+
15
19
  def defined?(name)
16
20
  apis.key?(name)
17
21
  end
@@ -14,7 +14,11 @@ module Eco
14
14
  begin
15
15
  cnf[key] = self[key].clone(config: cnf)
16
16
  rescue ArgumentError
17
- cnf[key] = self[key].clone
17
+ begin
18
+ cnf[key] = self[key].clone
19
+ rescue TypeError
20
+ cnf[key] = self[key]
21
+ end
18
22
  end
19
23
  end
20
24
  end
@@ -15,6 +15,25 @@ module Eco
15
15
  Eco::API::Organization::People.new(people)
16
16
  end
17
17
 
18
+ def search(data, silent: true)
19
+ # to scope people to be fresh data got via api
20
+ session.logger.info("going to api get entries...")
21
+ status = session.batch.search(data, silent: silent)
22
+ people = Eco::API::Organization::People.new(status.people)
23
+ # get the supervisors
24
+ session.logger.info("going to api get supervisors...")
25
+ supers = data.each_with_object([]) do |entry, sup|
26
+ if entry.respond_to?(:supervisor_id) && !entry.supervisor_id.to_s.strip.empty?
27
+ spr = {"id" => entry.supervisor_id}
28
+ sup.push(spr) unless sup.include?(spr)
29
+ end
30
+ end
31
+ status = session.batch.search(supers, silent: silent)
32
+ people = people.merge(status.people)
33
+ session.logger.info("could get #{people.length} people (out of #{data.length} entries)")
34
+ people
35
+ end
36
+
18
37
  def load_people(filename = enviro.config.people.cache, modifier: [:newest, :api])
19
38
  modifier = [modifier].flatten
20
39
  people = []
@@ -7,15 +7,15 @@ module Eco
7
7
  def process
8
8
  @cases.define("delete", type: :transform) do |people, session, options, usecase|
9
9
  delete = session.job_group("main").new("delete", usecase: usecase, type: :delete, sets: [:core, :details, :account])
10
- people.each_with_index do |person|
10
+ people.each_with_index do |person, i|
11
11
  delete.add(person)
12
12
  end
13
13
  end
14
14
 
15
- @cases.define("delete", type: :sync) do |entries, people, session|
15
+ @cases.define("delete", type: :sync) do |entries, people, session, options, usecase|
16
16
  delete = session.job_group("main").new("delete", usecase: usecase, type: :delete, sets: [:core, :details, :account])
17
17
  strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))
18
- entries.each_with_index do |entry|
18
+ entries.each_with_index do |entry, i|
19
19
  if person = people.find(entry, strict: strict_search)
20
20
  delete.add(person)
21
21
  else
@@ -41,7 +41,9 @@ module Eco
41
41
  person.account&.send_invites = options[:send_invites] if options.key?(:send_invites)
42
42
  end
43
43
 
44
- person.details = entry.details unless options.dig(:exclude, :details)
44
+ unless options.dig(:exclude, :details)
45
+ person.details = entry.details
46
+ end
45
47
 
46
48
  creation.add(person) if create
47
49
  update.add(person) unless create
@@ -11,7 +11,6 @@ module Eco
11
11
  users = people.users
12
12
  session.logger.warn("There are no people with account amoung your #{people.length} people") unless users.length > 0
13
13
  users.each do |person|
14
- print "."
15
14
  person.account.permissions_custom = session.new_preset(person)
16
15
  job.add(person)
17
16
  end
@@ -10,7 +10,7 @@ module Eco
10
10
  transform: [:people, :session],
11
11
  export: [:people, :session, :options]
12
12
  }
13
-
13
+
14
14
  class << self
15
15
  def valid_type?(type)
16
16
  TYPES.include?(type)
@@ -29,12 +29,18 @@ module Eco
29
29
  raise "Undefined usecase type #{type}, when creating '#{name}'. Please, use any of #{TYPES}" unless self.class.valid_type?(type)
30
30
 
31
31
  self.root = root
32
- @case = block
32
+ @callback = block
33
33
  @name = name
34
34
  @type = type
35
35
  @times_launched = 0
36
36
  end
37
37
 
38
+ def chainer
39
+ # TODO: root is a UseGroup that will not point to this new case.
40
+ # => Moreover, the name and type will be the same as self
41
+ Eco::API::UseCases::UseCaseChain.new(usecase: self, root: @root)
42
+ end
43
+
38
44
  def root=(value)
39
45
  raise "Root should be a UseGroup. Given: #{value}" if !value.is_a?(UseGroup)
40
46
  @root = value
@@ -44,7 +50,7 @@ module Eco
44
50
  @options = options
45
51
  data = UseCaseIO.new(usecase: self, input: input, people: people, session: session, options: options)
46
52
 
47
- data.output = @case.call(data.params)
53
+ data.output = @callback.call(data.params)
48
54
  @times_launched += 1
49
55
 
50
56
  data_model = {
@@ -54,6 +60,12 @@ module Eco
54
60
  }
55
61
  end
56
62
 
63
+ protected
64
+
65
+ def callback
66
+ @callback
67
+ end
68
+
57
69
  end
58
70
 
59
71
  end
@@ -5,7 +5,14 @@ module Eco
5
5
  MAX_CHAINS = 70
6
6
  @@num_chains = 0
7
7
 
8
- def initialize(name, type:, root:, &block)
8
+ def initialize(name = nil, type: nil, root:, usecase: nil, &block)
9
+ if usecase
10
+ raise "Expected Eco::API::UseCases::UseCase. Given #{usecase.class}" if !usecase.is_a?(Eco::API::UseCases::UseCase)
11
+ type = usecase.type
12
+ name = usecase.name
13
+ block = usecase.callback unless block
14
+ end
15
+
9
16
  super(name, type: type, root: root, &block)
10
17
  @chains = []
11
18
  @resolved_chains = nil
@@ -64,9 +71,9 @@ module Eco
64
71
 
65
72
  use_group = use_group || @root
66
73
  @resolved_chains = @chains.map do |usecase|
67
- usecase = usecase.call(use_group) if usecase.respond_to? :call
74
+ usecase = usecase.call(use_group) if usecase.respond_to?(:call)
68
75
  raise "A UseCase can only be chained with another UseCase" if usecase && !usecase.is_a?(UseCase)
69
- usecase.resolved_chains(use_group)
76
+ usecase.resolved_chains(use_group) if usecase.respond_to?(:resolved_chains)
70
77
  usecase
71
78
  end
72
79
  end
@@ -83,6 +90,7 @@ module Eco
83
90
  # chained cases use same params as parent case (out of simplicity)
84
91
  data_chain = dm[self][:io].chain(usecase: usecase)
85
92
  keyed_data = data_chain.params(keyed: true)
93
+ keyed_data.delete(:usecase)
86
94
  dm[self][:chains].merge(usecase.launch(keyed_data))
87
95
  end
88
96
  end
@@ -63,10 +63,10 @@ module Eco
63
63
  def chain(usecase:)
64
64
  raise "It should be a UseCase. Given: #{usecase}" if !usecase.is_a?(UseCase)
65
65
 
66
- kargs = params(keyed: true)
66
+ aux_io = self.class.new(input: input, people: people, session: session, options: options, usecase: usecase)
67
+ kargs = aux_io.params(keyed: true)
67
68
 
68
- # TODO: review chaining framework (redirection should depend on self.type as well)
69
- case usecase.type
69
+ case self.type
70
70
  when :import
71
71
  kargs[:input] = output
72
72
  when :filter
@@ -74,7 +74,6 @@ module Eco
74
74
  when :transform, :sync, :export
75
75
  # no redirections => should it redirect the input?
76
76
  end
77
-
78
77
  self.class.new(kargs)
79
78
  end
80
79
 
data/lib/eco/assets.rb CHANGED
@@ -9,25 +9,29 @@ module Eco
9
9
  @active_config = :default
10
10
  end
11
11
 
12
- def session(key: active_config)
13
- sessions[key] ||= Eco::API::Session.new(config(key: key))
14
- sessions[key].tap do |session|
12
+ def session(key: active_config, update_active: true)
13
+ config(key: key, update_active: update_active).session.tap do |session|
15
14
  yield(session) if block_given?
16
15
  end
16
+ #sessions[key] ||= Eco::API::Session.new(config(key: key, update_active: update_active))
17
+ #sessions[key].tap do |session|
18
+ # yield(session) if block_given?
19
+ #end
17
20
  end
18
21
 
19
- def sessions
20
- @sessions ||= {}
21
- end
22
+ #def sessions
23
+ # @sessions ||= {}
24
+ #end
22
25
 
23
26
 
24
- def config(key: active_config)
27
+ def config(key: active_config, update_active: true)
25
28
  configs[:default] ||= Eco::API::Session::Config.new(key)
26
29
  unless configs.key?(key)
27
- @active_config = key
30
+ @active_config = key unless !update_active
28
31
  configs[key] = configs[:default].clone(key)
29
32
  end
30
33
  configs[key].tap do |config|
34
+ config.active_api(key) if config.apis.defined?(key)
31
35
  yield(config) if block_given?
32
36
  end
33
37
  end
@@ -100,8 +100,6 @@ module Eco
100
100
 
101
101
  def remove(attr, value, modifier = Language::MatchModifier.new)
102
102
  self < exclude(attr, value, modifier)
103
- on_change
104
- self
105
103
  end
106
104
 
107
105
  def attr(attr, value = true, modifier = Language::MatchModifier.new)
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
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.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura