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

Includes:
Base
Included in:
Command, Samples::Location::Command::Results, Samples::Location::DSL
Defined in:
lib/eco/api/usecases/graphql/helpers/location/base.rb

Constant Summary collapse

TAGTREE_BACKUP =
'cache/tagtree.json'.freeze

Instance Attribute Summary collapse

Attributes included from Base::CaseEnv

#options, #session

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Base

#backup, #exit_error, #graphql

Methods included from Base::CaseEnv

#config, #simulate?

Methods included from Language::AuxiliarLogger

#log

Instance Attribute Details

#current_treeObject

Returns the value of attribute current_tree.



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

def current_tree
  @current_tree
end

#previous_treeObject

Returns the value of attribute previous_tree.



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

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/eco/api/usecases/graphql/helpers/location/base.rb', line 37

def backup_tree(tree = current_tree || live_tree)
  return false if simulate?
  case tree
  when Eco::API::Organization::TagTree
    tagtree = tree.source
  when Hash, Array
    # that's allright
  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)
  logger.debug("Backed up tagtree saved locally to #{file}.")
  true
end

#live_treeObject

Note:

that target_structure_id can retrive the live tree (when id is not defined) By checking if the current_tree changed after calling target_structure_id we prevent unnecessary requests.

Retrieves the live tree only if current_tree hasn't been just retrieved.



79
80
81
82
83
84
# File 'lib/eco/api/usecases/graphql/helpers/location/base.rb', line 79

def live_tree
  tree_init = current_tree
  target_id = target_structure_id
  return current_tree if current_tree != tree_init
  self.current_tree = session_live_tree(id: target_id)
end

#session_live_tree(id: nil) ⇒ Object

Note:

ensures archived nodes are retrieved.

Unique access point to retrieve the live tree



88
89
90
# File 'lib/eco/api/usecases/graphql/helpers/location/base.rb', line 88

def session_live_tree(id: nil)
  session.live_tree(id: id, include_archived: true)
end

#tagtree_idObject



55
56
57
# File 'lib/eco/api/usecases/graphql/helpers/location/base.rb', line 55

def tagtree_id
  %i[target_structure_id tagtree_id structure_id].find {|key| options.dig(:source, key)}
end

#target_structure_idObject

Note:

it is basic that the id is correctly identified.

Scopes the target structure id.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/eco/api/usecases/graphql/helpers/location/base.rb', line 61

def target_structure_id
  @target_structure_id ||= tagtree_id
  @target_structure_id ||= self.class.const_get(:TARGET_STRUCTURE_ID) if self.class.const_defined?(:TARGET_STRUCTURE_ID)
  @target_structure_id ||= current_tree.id                            if current_tree.respond_to?(:id)
  return @target_structure_id if @target_structure_id
  msg  = "Const TARGET_STRUCTURE_ID has not been defined, "
  msg << "nor options(:source, :structure_id). "
  msg << "Infering active locations structure."
  log(:warn) { msg }
  if self.current_tree = session_live_tree
    @target_structure_id = current_tree.id
  end
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:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/eco/api/usecases/graphql/helpers/location/base.rb', line 21

def track_current_tree(tree)
  return if simulate?
  return unless tree
  latest_tree = tree if tree.is_a?(Eco::API::Organization::TagTree)
  if tree.respond_to?(:treeify)
    latest_tree ||= Eco::API::Organization::TagTree.new(tree.treeify, id: tree.id, name: tree.name)
  end
  latest_tree.tap do |_tree|
    next unless latest_tree
    @previous_tree     = @current_tree
    self.current_tree  = latest_tree
  end
end