Module: Eco::API::UseCases::GraphQL::Helpers::Location::Base::TreeTracking

Includes:
Base::CaseEnv
Included in:
Eco::API::UseCases::GraphQL::Helpers::Location::Base
Defined in:
lib/eco/api/usecases/graphql/helpers/location/base/tree_tracking.rb

Constant Summary collapse

TAGTREE_BACKUP =
'cache/tagtree.json'.freeze

Instance Attribute Summary collapse

Attributes included from Base::ErrorHandling

#exception, #exiting

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Instance Attribute Details

#current_treeObject

Returns the value of attribute current_tree.



8
9
10
# File 'lib/eco/api/usecases/graphql/helpers/location/base/tree_tracking.rb', line 8

def current_tree
  @current_tree
end

#previous_treeObject

Returns the value of attribute previous_tree.



9
10
11
# File 'lib/eco/api/usecases/graphql/helpers/location/base/tree_tracking.rb', line 9

def previous_tree
  @previous_tree
end

Instance Method Details

#backup_tree(tree = current_tree || live_tree) ⇒ Boolean

Returns whether or not the backup was created.

Parameters:

Returns:

  • (Boolean)

    whether or not the backup was created



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eco/api/usecases/graphql/helpers/location/base/tree_tracking.rb', line 52

def backup_tree(tree = current_tree || live_tree)
  return false if simulate?
  case tree
  when Eco::API::Organization::TagTree
    tree = tree.source
  when Hash, Array
    # that's alright
  else
    log(:error) {
      "Can't back up tagtree. Expecting TagTree, Hash or Array. Given: #{tree.class}"
    }
    return false
  end

  file = session.file_manager.save_json(tree, self.class::TAGTREE_BACKUP, :timestamp)
  log(:debug) { "Backed-up tagtree saved locally to #{file}." }
  true
end

#track_current_tree(tree) ⇒ Eco::API::Organization::TagTree

Note:

it also does a backup

At any moment we want to know how the live tree is

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eco/api/usecases/graphql/helpers/location/base/tree_tracking.rb', line 32

def track_current_tree(tree)
  return if simulate?
  return if tree.nil?

  latest_tree = tree if tree.is_a?(Eco::API::Organization::TagTree)
  if tree.respond_to?(:treeify) && track_current_tree?
    latest_tree ||= Eco::API::Organization::TagTree.new(tree.treeify, id: tree.id, name: tree.name)
  end

  latest_tree.tap do
    next unless latest_tree.is_a?(Eco::API::Organization::TagTree)
    next if latest_tree.empty?

    # a backup happens:
    self.current_tree = latest_tree
  end
end

#track_current_tree?Boolean

Allows to override if the @current_tree should be kept to the latest live version

Returns:

  • (Boolean)


13
14
15
# File 'lib/eco/api/usecases/graphql/helpers/location/base/tree_tracking.rb', line 13

def track_current_tree?
  true
end