Module: UlePage::ModelMatch

Extended by:
Capybara::DSL
Defined in:
lib/ule_page/model_match.rb

Class Method Summary collapse

Class Method Details

.get_current_page(special_maps = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ule_page/model_match.rb', line 23

def get_current_page(special_maps = {})
  map = prepare_maps special_maps

  return nil unless current_path

  current = get_model map, current_path

  if current.nil? && current_path.include?('/admin/')
    current =  get_model map, current_path[6, current_path.length]
  end

  current
end

.get_current_page_with_wait(special_maps = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ule_page/model_match.rb', line 9

def get_current_page_with_wait(special_maps = {})
  page.has_css?('.pace-small .pace-inactive') if defined? page

  current = get_current_page special_maps

  if !current_path.nil? && current.nil?
    p "current path is #{current_path}, we can not get the page model, please define the set_urls for the page model."

    raise
  end

  current
end

.get_model(map, path) ⇒ Object



53
54
55
# File 'lib/ule_page/model_match.rb', line 53

def get_model(map, path)
  map[path] || try_regex_match(map, path)
end

.prepare_maps(special_maps) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ule_page/model_match.rb', line 37

def prepare_maps(special_maps)
  if !UlePage.map_initialized || UlePage.special_maps.empty?
    UlePage.special_maps = special_maps
    UlePage.map = {}

    generate_map_by_convention UlePage.map, UlePage.resource_models

    UlePage.map.merge! UlePage.special_maps if UlePage.special_maps.any?
    UlePage.map.merge! UlePage::PageMap.instance.pages

    UlePage.map_initialized = true
  end

  UlePage.map
end

.try_regex_match(map, path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ule_page/model_match.rb', line 57

def try_regex_match(map, path)
  map.each do |k, v|
    pattern = k
    pattern = k.gsub(/(:\w+)/, '\w+') if k.include?(':')

    regex = Regexp.new "^#{pattern}$"
    if regex.match(path)
      return v
    end
  end

  nil
end