Sha256: d6047e75e76b5ffaa85aee8b97472702db9087a0bf9d77c5987987b46eb6208e

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module Eco
  module API
    class Session
      class Config
        class TagTree < BaseConfig
          attr_key :file

          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)
          end

          # Among all the locations structures it selects the one with more location nodes
          def live_tree(enviro: nil)
            return @live_tree if instance_variable_defined?(:@live_tree) && @live_tree.enviro == enviro
            trees = live_trees(enviro: enviro)
            @live_tree = trees.reject do |tree|
              tree.empty?
            end.max {|a,b| a.count <=> b.count}
          end

          # Retrieves all the location structures of the organisation
          def live_trees(enviro: nil)
            [].tap do |trees|
              next unless apis.active_api.version_available?(:graphql)
              next unless graphql = apis.api(version: :graphql)
              kargs = {
                includeArchived: false,
                includeUnpublished: false
              }
              next unless trees = graphql.currentOrganization.locationsStructures(**kargs)
              trees.each do |tree|
                args = {  enviro: enviro, id: tree.id, name: tree.name}
                eco_tree = Eco::API::Organization::TagTree.new(tree.treeify, **args)
                trees.push(eco_tree)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eco-helpers-2.2.1 lib/eco/api/session/config/tagtree.rb