Class: Eco::API::UseCases::DefaultCases::SetDefaultTagCase

Inherits:
Common::Loaders::UseCase show all
Defined in:
lib/eco/api/usecases/backup/set_default_tag_case.rb,
lib/eco/api/usecases/default_cases/set_default_tag_case.rb

Instance Method Summary collapse

Methods inherited from Common::Loaders::UseCase

#initialize, type, #type

Methods inherited from Common::BaseLoader

<=>, created_at, #initialize, #name, name_only_once!, set_created_at!

Methods included from Common::ClassHelpers

#class_resolver, #descendants, #descendants?, #new_class, #resolve_class, #to_constant

Constructor Details

This class inherits a constructor from Eco::API::Common::Loaders::UseCase

Instance Method Details

#main(people, session, options, usecase) ⇒ Object

take the deepest tag (the one that is further down in the tree) different options (several nodes at the same depth): => take the common node between them (i.e. you have Hamilton and Auckland -> take New Zealand) => if there's no common node between them, take the first (unless they are at top level of the tree)



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/eco/api/usecases/default_cases/set_default_tag_case.rb', line 9

def main(people, session, options, usecase)
  micro    = session.micro
  tagtree_present!(session)
  users    = users_present!(people, session)
  update   = session.new_job("main", "update", :update, usecase, :account)

  users.each do |person|
    micro.refresh_default_tag(nil, person, options)
    update.add(person)
  end
end

#processObject

take the deepest tag (the one that is further down in the tree) different options (several nodes at the same depth): => take the common node between them (i.e. you have Hamilton and Auckland -> take New Zealand) => if there's no common node between them, take the first (unless they are at top level of the tree)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/eco/api/usecases/backup/set_default_tag_case.rb', line 11

def process
  @cases.define("set-default-tag", type: :transform) do |people, session, options, usecase|
    if !session.tagtree
      msg = "There is no tagtree definition in the configuration files\n" +
            "For this usecase to work out you need to define it."
      session.logger.fatal(msg)
      raise msg
    end

    # IMPORTANT: this two lines ensure that only people to be updated is selected
    all_people = people
    people = people.

    if people.length <= 0
      msg = "There are no people with account... aborting script"
      session.logger.info(msg)
      raise msg
    end

    update   = session.job_group("main").new("update", usecase: usecase, type: :update, sets: :account)

    people.each_with_index  do |person, i|
      if session.tagtree
        person..default_tag = session.tagtree.default_tag(*person.filter_tags)
      else
        tags = person.filter_tags || []
        person..default_tag = tags.first unless tags.length > 1
      end
      update.add(person)
    end

  end
end