navigate()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 17
rs 9.55
1
# frozen_string_literal: true
2
3
# DB Loader
4
def views
5
  # /data/views.yml
6
  load_yaml('views')
7
end
8
9
def navigate(ruta)
10
  # Redirected view collection
11
  redirected_views = {
12
    hechizos: %w[aire agua fuego tierra],
13
    sagradas: %w[arena hielo sombra sangre],
14
    plegarias: %w[plegarias execraciones],
15
    heroes: %w[reservistas ausentes licenciados]
16
  }
17
18
  # Detects if ruta exists as key in redirected_views (returns nil if not)
19
  new_route = redirected_views.detect do |key, values|
20
    break key.to_s if Array(values).include?(ruta)
21
  end
22
23
  # Route preview loader
24
  new_route || ruta
25
end
26
27
# Behaviour if defined, or from DB-views by default.
28
def view(ruta)
29
  # Return the matching route ('navigate') from DB
30
  v = views.find { |vista| vista['ruta'] == navigate(ruta) }
31
  v = v.nil? ? view('error') : v # TODO : 404 error...
32
  Vista.new(v)
33
end
34
35
# View meta info for component and layouts
36
def viewinfo(ruta)
37
  {
38
    title: view(ruta)['ruta'],
39
    bc: breadcrumb(ruta),
40
    layout: view(ruta).template
41
  }
42
end
43
44
# Refined DATA hash-like
45
def preview(ruta)
46
  erb :template, locals: viewinfo(ruta)
47
end
48
49
# Only will be displayed if true (returns array)
50
def breadcrumb(ruta)
51
  bc = []
52
  if view(ruta)
53
    if view(ruta).template
54
      if view(ruta).template['main'].split('/').count > 1
55
        bc = view(ruta).template['main'].split('/')
56
      end
57
    end
58
  end
59
  bc # empty arrays should be treated in ERB
60
end
61