Class: Sapp::Path::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sapp/path/base.rb

Overview

An Object that builds a path structure for matching. Is used during the addition of routes.

Direct Known Subclasses

Request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options: {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
# File 'lib/sapp/path/base.rb', line 8

def initialize path, options: {}
  @original   = set_original options, path
  @options    = options
  @keys       = Hash.new
  @paths      = Hash.new
  @stream     = Array.new
  @counter    = 0
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



6
7
8
# File 'lib/sapp/path/base.rb', line 6

def controller
  @controller
end

#keysObject (readonly)

Returns the value of attribute keys.



6
7
8
# File 'lib/sapp/path/base.rb', line 6

def keys
  @keys
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/sapp/path/base.rb', line 6

def options
  @options
end

#originalObject (readonly)

Returns the value of attribute original.



6
7
8
# File 'lib/sapp/path/base.rb', line 6

def original
  @original
end

#pathsObject (readonly)

Returns the value of attribute paths.



6
7
8
# File 'lib/sapp/path/base.rb', line 6

def paths
  @paths
end

#streamObject (readonly)

Returns the value of attribute stream.



6
7
8
# File 'lib/sapp/path/base.rb', line 6

def stream
  @stream
end

Instance Method Details

#concat_namespace_and_path(path, namespaces) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/sapp/path/base.rb', line 41

def concat_namespace_and_path path, namespaces
  if namespaces? namespaces
    x = namespace_to_path namespaces[0]
    y = nested?(namespaces) ? namespace_to_path(namespaces[1]) : ""

    x + path + y
  else
    path
  end
end

#counterObject



114
115
116
# File 'lib/sapp/path/base.rb', line 114

def counter
  @counter
end

#create_pathObject



104
105
106
107
108
109
110
111
112
# File 'lib/sapp/path/base.rb', line 104

def create_path
  {
     controller: @controller,
     stream: stream,
     keys: keys,
     methods: paths,
     original: original,
  }
end

#extract_keys(key) ⇒ Object



90
91
92
93
94
95
# File 'lib/sapp/path/base.rb', line 90

def extract_keys key
  if key.match(/\A:/)
    stream << 0
    keys[counter] = key
  end
end

#extract_keys_and_pathsObject



82
83
84
85
86
87
88
# File 'lib/sapp/path/base.rb', line 82

def extract_keys_and_paths
  setup_extraction.each do |values|
    extract_keys values
    extract_paths values
    count
  end
end

#extract_paths(path) ⇒ Object



97
98
99
100
101
102
# File 'lib/sapp/path/base.rb', line 97

def extract_paths path
  unless path.match(/\A:/)
    stream << 1
    paths[counter] = path
  end
end

#namespace_to_path(namespaces) ⇒ Object



52
53
54
# File 'lib/sapp/path/base.rb', line 52

def namespace_to_path namespaces
  '/' + namespaces.join('/') 
end

#namespacesObject



68
69
70
# File 'lib/sapp/path/base.rb', line 68

def namespaces
  options[:namespaces]
end

#namespaces?(namespaces) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sapp/path/base.rb', line 64

def namespaces? namespaces
  namespaces && namespaces.any?
end

#nested?(namespaces) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sapp/path/base.rb', line 60

def nested? namespaces
  namespaces[1]
end

#nested_deeply?(namespaces) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/sapp/path/base.rb', line 37

def nested_deeply? namespaces
  namespaces? namespaces && namespaces.count > 2
end

#options?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sapp/path/base.rb', line 56

def options?
  options.any?
end

#parseObject



17
18
19
20
21
22
23
# File 'lib/sapp/path/base.rb', line 17

def parse
  extract_keys_and_paths
  set_controller
  path = create_path

  options? ? path.merge(options) : path
end

#set_original(options, path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sapp/path/base.rb', line 25

def set_original options, path
  namespaces = options[:namespaces]

  begin
    if nested_deeply? namespaces 
      raise ArgumentError, "Routes nested too deeply"
    else
      concat_namespace_and_path path, namespaces
    end
  end
end

#setup_extractionObject



72
73
74
75
76
77
78
79
80
# File 'lib/sapp/path/base.rb', line 72

def setup_extraction
  if original == '/'
    [original]
 else
    path = original.split('/')
    path.delete("")
    path
  end
end