Class: Eco::API::Organization::TagTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/eco/api/organization/tag_tree.rb

Overview

Provides helpers to deal with tagtrees.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tagtree = [], name: nil, id: nil, depth: -1,, path: [], parent: nil, _weight: nil, enviro: nil) ⇒ TagTree

Returns a new instance of TagTree.

Examples:

Node format:

{"tag": "NODE NAME", "nodes": subtree}

Tree/subtree format:

[[Node], ...]

Input format example:

tree = [{"tag" => "AUSTRALIA", "nodes" => [
    {"tag" => "SYDNEY", "nodes" => []}
]}]
tree = TagTree.new(tree.to_json)

Parameters:

  • tagtree (String) (defaults to: [])

    representation of the tagtree in json.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/eco/api/organization/tag_tree.rb', line 27

def initialize(tagtree = [], name: nil, id: nil, depth: -1, path: [], parent: nil, _weight: nil, enviro: nil)
  @depth  = depth
  @parent = parent

  case tagtree
  when String
    @source = JSON.parse(tagtree)
  else
    @source = tagtree
  end
  fatal("You are trying to initialize a TagTree with a null tagtree") if !@source
  fatal("Expecting Environment object. Given: #{enviro}") if enviro && !enviro.is_a?(API::Common::Session::Environment)
  @enviro = enviro

  if @source.is_a?(Array)
    @id        = id
    @name      = name
    @row_nodes = @source
  else
    @id        = @source.values_at('tag', 'id').compact.first&.upcase
    @name      = @source['name']
    @archived  = @source.fetch('archived', false)
    @weight    = @source.fetch('weight', _weight)
    @row_nodes = @source['nodes'] || []
  end

  @path  = path || []
  @path.push(@id) unless top?

  @nodes = @row_nodes.map.with_index do |cnode, idx|
    TagTree.new(cnode, depth: depth + 1, path: @path.dup, parent: self, _weight: idx, enviro: @enviro)
  end

  init_hashes
end

Instance Attribute Details

#archivedObject

Returns the value of attribute archived.



8
9
10
# File 'lib/eco/api/organization/tag_tree.rb', line 8

def archived
  @archived
end

#children_countInteger (readonly)

Returns:

  • (Integer)


181
182
183
# File 'lib/eco/api/organization/tag_tree.rb', line 181

def children_count
  @children_count
end

#depthObject (readonly)

Returns the value of attribute depth.



12
13
14
# File 'lib/eco/api/organization/tag_tree.rb', line 12

def depth
  @depth
end

#enviroObject (readonly)

Returns the value of attribute enviro.



13
14
15
# File 'lib/eco/api/organization/tag_tree.rb', line 13

def enviro
  @enviro
end

#idObject Also known as: tag

Returns the value of attribute id.



6
7
8
# File 'lib/eco/api/organization/tag_tree.rb', line 6

def id
  @id
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/eco/api/organization/tag_tree.rb', line 8

def name
  @name
end

#nodesObject (readonly)

Returns the value of attribute nodes.



11
12
13
# File 'lib/eco/api/organization/tag_tree.rb', line 11

def nodes
  @nodes
end

#parentObject (readonly)

Returns the value of attribute parent.



10
11
12
# File 'lib/eco/api/organization/tag_tree.rb', line 10

def parent
  @parent
end

#path(key = nil) ⇒ Array<String> (readonly)

Note:

the path is not relative to the subtree, but absolute to the entire tree.

Finds the path from a node key to its root node in the tree. If key is not specified, returns the path from current node to root.

Parameters:

  • key (String) (defaults to: nil)

    tag to find the path to.

Returns:

  • (Array<String>)


218
219
220
# File 'lib/eco/api/organization/tag_tree.rb', line 218

def path
  @path
end

#weightObject

Returns the value of attribute weight.



8
9
10
# File 'lib/eco/api/organization/tag_tree.rb', line 8

def weight
  @weight
end

Instance Method Details

#all_nodes(&block) ⇒ Array[]

Note:

order is that of the parent to child relationships

All actual nodes of this tree

Returns:

  • (Array[])


84
85
86
87
88
89
90
91
# File 'lib/eco/api/organization/tag_tree.rb', line 84

def all_nodes(&block)
  [].tap do |all_nodes|
    all_nodes.push(self) unless top?
    nodes.each do |node|
      all_nodes.concat(node.all_nodes)
    end
  end
end

#archived?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/eco/api/organization/tag_tree.rb', line 63

def archived?
  @archived
end

#as_jsonArray[Hash]

Returns where Hash is a node {"tag" => TAG, "nodes": Array[Hash]}.

Returns:

  • (Array[Hash])

    where Hash is a node {"tag" => TAG, "nodes": Array[Hash]}



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/eco/api/organization/tag_tree.rb', line 104

def as_json
  nodes_json = nodes.map {|node| node.as_json}
  if top?
    nodes_json
  else
    {
      "id"    => tag,
      "nodes" => nodes_json
    }
  end
end

#countInteger

Returns the number of locations.

Returns:

  • (Integer)

    the number of locations



122
123
124
# File 'lib/eco/api/organization/tag_tree.rb', line 122

def count
  @hash_tags.keys.count
end

#default_tag(*values) ⇒ String

Helper to decide which among the tags will be the default.

  • take the deepest tag (the one that is further down in the tree)
  • if there are 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
    • to the above, take the first also on top level, but only if there's 1 level for the entire tree

Parameters:

  • values (Array<String>)

    list of tags.

Returns:

  • (String)

    default tag.



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/eco/api/organization/tag_tree.rb', line 266

def default_tag(*values)
  values = filter_tags(values)
  nodes = []; ddepth = -1
  values.each do |tag|
    raise("Couldn't find the node of #{tag} in the tag-tree definition") unless cnode = node(tag)

    if cnode.depth > ddepth
      nodes = [cnode]
      ddepth = cnode.depth
    elsif cnode.depth == ddepth
      nodes.push(cnode)
    end
  end

  default_tag = nil
  if nodes.length > 1
    common      = nodes.reduce(self.tags.reverse) {|com, cnode| com & cnode.path.reverse}
    default_tag = common.first if common.length > 0 && ddepth > 0
  end
  default_tag ||= nodes.first&.tag  if (ddepth > 0) || flat?
  default_tag
end

#diff(tagtree, differences: {}, level: 0, **options) ⇒ Array

Returns with the differences.

Returns:

  • (Array)

    with the differences



94
95
96
97
# File 'lib/eco/api/organization/tag_tree.rb', line 94

def diff(tagtree, differences: {}, level: 0, **options)
  require 'hashdiff'
  Hashdiff.diff(self.as_json, tagtree.as_json, **options.slice(:array_path, :similarity, :use_lcs))
end

#dupEco::API::Organization::TagTree



68
69
70
# File 'lib/eco/api/organization/tag_tree.rb', line 68

def dup
  self.class.new(as_json)
end

#each {|node| ... } ⇒ Enumerable<Eco::API::Organization::TagTree>

Iterate through all the nodes of this tree

Yields:

  • (node)

    do some stuff with one of the nodes of the tree

Yield Parameters:

Returns:



76
77
78
79
# File 'lib/eco/api/organization/tag_tree.rb', line 76

def each(&block)
  return to_enum(:each) unless block
  all_nodes.each(&block)
end

#empty?Boolean

Returns true if there are tags in the node, false otherwise.

Returns:

  • (Boolean)

    true if there are tags in the node, false otherwise.



117
118
119
# File 'lib/eco/api/organization/tag_tree.rb', line 117

def empty?
  count <= 1
end

#filter_tags(list) ⇒ Array<String>

Filters tags out that do not belong to the tree

Parameters:

  • list (Array<String>)

    source tags.

Returns:

  • (Array<String>)


208
209
210
211
# File 'lib/eco/api/organization/tag_tree.rb', line 208

def filter_tags(list)
  return [] unless list && list.is_a?(Array)
  list.select {|str| tag?(str)}
end

#flat?Integer

Returns if there's only top level.

Returns:

  • (Integer)

    if there's only top level.



139
140
141
# File 'lib/eco/api/organization/tag_tree.rb', line 139

def flat?
  self.total_depth <= 0
end

#has_children?Boolean

Returns it has subnodes.

Returns:

  • (Boolean)

    it has subnodes



186
187
188
# File 'lib/eco/api/organization/tag_tree.rb', line 186

def has_children?
  children_count > 0
end

#leafsArray<String>

Returns all the tags with no children

Returns:

  • (Array<String>)


174
175
176
177
178
# File 'lib/eco/api/organization/tag_tree.rb', line 174

def leafs
  tags.select do |tag|
    !node(tag).has_children?
  end
end

#node(key) ⇒ TagTree?

Finds a subtree node.

Parameters:

  • key (String)

    parent node of subtree.

Returns:

  • (TagTree, nil)

    if the tag key is a node, returns that node.



200
201
202
203
# File 'lib/eco/api/organization/tag_tree.rb', line 200

def node(key)
  return nil unless tag?(key)
  @hash_tags[key.upcase]
end

#subtag?(key) ⇒ Boolean

Verifies if a tag exists in the subtree(s).

Parameters:

  • key (String)

    tag to verify.

Returns:

  • (Boolean)


168
169
170
# File 'lib/eco/api/organization/tag_tree.rb', line 168

def subtag?(key)
  subtags.include?(key&.upcase)
end

#subtagsArray<String>

Gets all but the upper level tags of the current node tree.

Returns:

  • (Array<String>)


161
162
163
# File 'lib/eco/api/organization/tag_tree.rb', line 161

def subtags
  tags - tags(depth: depth)
end

#tag?(key) ⇒ Boolean

Verifies if a tag exists in the tree.

Parameters:

  • key (String)

    tag to verify.

Returns:

  • (Boolean)


193
194
195
# File 'lib/eco/api/organization/tag_tree.rb', line 193

def tag?(key)
  @hash_tags.key?(key&.upcase)
end

#tags(depth: nil) ⇒ Array<String>

Note:
  • this will include the upper level tag(s) as well
  • to get all but the upper level tag(s) use subtags method instead

Gets all the tags of the current node tree.

Parameters:

  • depth (Integer) (defaults to: nil)

    if empty, returns the list of tag nodes of that level. Otherwise the list of tag nodes of the entire subtree.

Returns:

  • (Array<String>)


149
150
151
152
153
154
155
156
157
# File 'lib/eco/api/organization/tag_tree.rb', line 149

def tags(depth: nil)
  if !depth || depth < 0
    @hash_tags.keys
  else
    @hash_tags.select do |t, n|
      n.depth == depth
    end.keys
  end
end

#top?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/eco/api/organization/tag_tree.rb', line 99

def top?
  depth == -1
end

#total_depthInteger

Returns the highest depth of all the children.

Returns:

  • (Integer)

    the highest depth of all the children.



127
128
129
130
131
132
133
134
135
136
# File 'lib/eco/api/organization/tag_tree.rb', line 127

def total_depth
  @total_depth ||= if has_children?
      deepest_node = nodes.max_by do |node|
        node.total_depth
      end
      deepest_node.total_depth
    else
      depth
    end
end

#user_tags(initial: [], final: [], preserve_custom: true, add_custom: false) ⇒ Array<String>

Helper to assign tags to a person account.

  • It preserves the :initial order, in case the :final tags are the same

Examples:

Usage example:

tree = [{"tag" => "Australia", "nodes" => [
     {"tag" => "SYDNEY", "nodes" => []},
     {"tag" => "MELBOURNE", "nodes" => []}
]}]

tree = TagTree.new(tree.to_json)
original = ["SYDNEY", "RISK"]
final    = ["MELBOURNE", "EVENT"]

tree.user_tags(initial: original, final: final) # out: ["MELBOURNE", "RISK"]
tree.user_tags(initial: original, final: final, preserve_custom: false) # out: ["MELBOURNE"]
tree.user_tags(initial: original, final: final, add_custom: true) # out: ["MELBOURNE", "RISK", "EVENT"]
tree.user_tags(initial: original, final: final, preserve_custom: false, add_custom: true) # out: ["MELBOURNE", "EVENT"]

Parameters:

  • initial (Array<String>) (defaults to: [])

    original tags a person has in their account.

  • final (Array<String>) (defaults to: [])

    target tags the person should have in their account afterwards.

  • preserve_custom (Boolean) (defaults to: true)

    indicates if original tags that are not in the tree should be added/preserved.

  • add_custom (Boolean) (defaults to: false)

    indicates if target tags that are not in the tree should be really added.

Returns:

  • (Array<String>)

    with the treated final tags.



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/eco/api/organization/tag_tree.rb', line 246

def user_tags(initial: [], final: [], preserve_custom: true, add_custom: false)
  initial = [initial].flatten.compact
  final   = [final].flatten.compact
  raise "Expected Array for initial: and final:" unless initial.is_a?(Array) && final.is_a?(Array)
  final    = filter_tags(final) unless add_custom
  custom   = initial - filter_tags(initial)
  final    = final + custom     if preserve_custom
  new_tags = final - initial
  # keep same order as they where
  (initial & final) + new_tags
end