Class: Eco::API::Session::Config::TagTree

Inherits:
BaseConfig show all
Defined in:
lib/eco/api/session/config/tagtree.rb

Instance Attribute Summary

Attributes inherited from BaseConfig

#config

Instance Method Summary collapse

Methods inherited from BaseConfig

#apis, attr_key, #clone, #file_manager, #initialize

Methods inherited from Hash

#deep_merge, #deep_merge!

Constructor Details

This class inherits a constructor from Eco::API::Session::Config::BaseConfig

Instance Method Details

#live_tree(id: nil, enviro: nil, include_archived: false, **kargs, &block) ⇒ Object

Among all the locations structures it selects the one with more location nodes If id is provided, it only retrieves this locations structure.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/eco/api/session/config/tagtree.rb', line 24

def live_tree(id: nil, enviro: nil, include_archived: false, **kargs, &block)
  return @live_tree if instance_variable_defined?(:@live_tree) && @live_tree.enviro == enviro
  if id
    args       = {id: id, enviro: enviro, include_archived: include_archived}.merge(kargs)
    @live_tree = live_tree_get(**args, &block)
  else
    # note that `include_archived` nodes is NOT the same as including archived structures
    # => In `live_tree` the paramter refers to nodes
    trees      = live_trees(enviro: enviro, &block)
    @live_tree = trees.reject do |tree|
      tree.empty?
    end.max do |a,b|
      a.count <=> b.count
    end
  end.tap do |tree|
    if tree
      msg = "Using LIVE LOCATIONS Structure: '#{tree.name}' (#{tree.count} nodes)"
      session_logger.info(msg)
    end
  end
end

#live_tree_get(id: nil, enviro: nil, include_archived: false, **kargs, &block) ⇒ Object

Note:

it does not memoize

Gets a single locations structure



48
49
50
51
52
53
54
55
56
57
# File 'lib/eco/api/session/config/tagtree.rb', line 48

def live_tree_get(id: nil, enviro: nil, include_archived: false, **kargs, &block)
  return nil unless apis.active_api.version_available?(:graphql)
  return nil unless graphql = apis.api(version: :graphql)
  #kargs = { includeArchived: include_archived }.merge(kargs).slice(:includeArchived)
  # For now, this endpoint only accepts `id` as a parameter. It is pending to
  # expose further parameters via query
  return nil unless tree = graphql.currentOrganization.locationStructure(id: id, &block)
  args = { enviro: enviro, id: tree.id, name: tree.name }
  Eco::API::Organization::TagTree.new(tree.treeify, **args)
end

#live_trees(enviro: nil, include_archived: false, **kargs, &block) ⇒ Object

Retrieves all the location structures of the organisation



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/eco/api/session/config/tagtree.rb', line 60

def live_trees(enviro: nil, include_archived: false, **kargs, &block)
  [].tap do |eco_trees|
    next unless apis.active_api.version_available?(:graphql)
    next unless graphql = apis.api(version: :graphql)
    kargs = {
      includeArchived:    include_archived,
      includeUnpublished: false
    }.merge(kargs).slice(:includeArchived, :includeUnpublished)

    next unless trees = graphql.currentOrganization.locationStructures(**kargs, &block)
    trees.each do |tree|
      args     = { enviro: enviro, id: tree.id, name: tree.name }
      eco_tree = Eco::API::Organization::TagTree.new(tree.treeify, **args)
      eco_trees.push(eco_tree)
    end
  end
end

#scope_tree(enviro: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/eco/api/session/config/tagtree.rb', line 8

def scope_tree(enviro: nil)
  return @tagtree if instance_variable_defined?(:@tagtree) && @tagtree.enviro == enviro
  if tree_file = self.file
    if (tree = file_manager.load_json(tree_file)) && !tree.empty?
      @tagtree  = Eco::API::Organization::TagTree.new(tree, enviro: enviro)
    end
  end
  @tagtree ||= live_tree(enviro: enviro).tap do |tree|
    unless tree && !tree.empty?
      raise "Could not find a locations structure."
    end
  end
end