Class: VRT::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/vrt/map.rb

Constant Summary collapse

DEPTH_MAP =
{
  'category' => 1,
  'subcategory' => 2,
  'variant' => 3
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version = nil) ⇒ Map

Returns a new instance of Map.



11
12
13
14
15
16
# File 'lib/vrt/map.rb', line 11

def initialize(version = nil)
  @version = version || VRT.current_version
  @structure = build_structure
  @found_nodes = {}
  @lineages = {}
end

Instance Attribute Details

#structureObject (readonly)

Returns the value of attribute structure.



9
10
11
# File 'lib/vrt/map.rb', line 9

def structure
  @structure
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/vrt/map.rb', line 9

def version
  @version
end

Instance Method Details

#categoriesObject

Returns list of top level categories in the shape:

{ value: category_id, label: category_name }


33
34
35
36
37
38
# File 'lib/vrt/map.rb', line 33

def categories
  structure.keys.map do |key|
    node = find_node(key.to_s, max_depth: 'category')
    { value: node.id, label: node.name }
  end
end

#find_node(string, max_depth: 'variant') ⇒ Object



18
19
20
# File 'lib/vrt/map.rb', line 18

def find_node(string, max_depth: 'variant')
  @found_nodes[string + max_depth] ||= walk_node_tree(string, max_depth: max_depth)
end

#get_lineage(string, max_depth: 'variant') ⇒ Object



27
28
29
# File 'lib/vrt/map.rb', line 27

def get_lineage(string, max_depth: 'variant')
  @lineages[string] ||= construct_lineage(string, max_depth)
end

#valid?(node) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/vrt/map.rb', line 22

def valid?(node)
  return false unless node =~ /[[:lower]]/
  node == 'other' || find_node(node)
end