Class: Cuca::URLMap

Inherits:
Object
  • Object
show all
Defined in:
lib/cuca/urlmap.rb,
lib/cuca/urlmap-original.rb

Overview

URLMap

URLMap is used internally to match a URL to a controller file. Call with ds = URLMap.new(‘/path/to/app’, ‘path/from/url’)

URLMap.new(base_path, url)

You can then fetch the following values:

  • script - path to the controller file

  • url - unmodified URL as passed to initializer

  • assigns - hash with variable assigns from the url (magick prefixes)

  • subcall - name of subcall or nil if a normal call was made

  • action - Action name (Note: action.capitalize+“Controller” is your controller class name)

  • base_url - Base URL to the action (e.g.: /user/someone/show is -> /user/someone/)

  • base_path - Unmodified base_path

  • action_path - Path to the action script

  • action_path_full - Full path to action script

  • path_tree - an array of each directory on the way from /path/to/app to the script

    (to look for include_directories in - see Cuca::Config)
    
  • action_module - The module the action should be loaded into (to avoid name conflicts, depends on action_path)

Match on other URL

A widget/controller can make use of the URLMap object to scan on other directories (example to find out if a link url will be withing the same controller).

See match? / submatch? and usubmatch?

Notes

URL’s ending with ‘/’ will be scanned for default index files.

URL’s where last part (action) starts with ‘-’ will be scanned for subcalls

If no script is found or any other error it will raise a RoutingError exception

Example

u = URLMap.new('/home/bones/src/cuca_app/app', 'customer/southwind_lda/show'

u.script 	 => '/home/bones/src/cuca_app/app/customer/__customer/show.rb'
u.action 	 => 'show'
u.base_url    => '/customer/__customer/'
u.assigns 	 => { 'customer' => 'southwind_lda' }
u.action_path => 'customer/southwind_lda/'

Constant Summary collapse

DEF_ACT =
Cuca::App::config['magic_action_prefix'] || '__'
DEF_IDX =
[ 'index', 'default' ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, path_info, default_actions = ['index']) ⇒ URLMap

Returns a new instance of URLMap.



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/cuca/urlmap.rb', line 252

def initialize(base_path, path_info, default_actions = ['index'])
  @path_info = path_info
  @base_path = File.expand_path(base_path)
  @script    = ''
  @subcall   = nil
  @default_actions = default_actions
  @assigns = {}
  @action = ''
  @action_path = ''
  @path_tree = [base_path]
  scan
  self
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



72
73
74
# File 'lib/cuca/urlmap.rb', line 72

def action
  @action
end

#action_moduleObject (readonly)

Returns the value of attribute action_module.



75
76
77
# File 'lib/cuca/urlmap.rb', line 75

def action_module
  @action_module
end

#action_pathObject (readonly)

Returns the value of attribute action_path.



73
74
75
# File 'lib/cuca/urlmap.rb', line 73

def action_path
  @action_path
end

#action_path_fullObject (readonly)

Returns the value of attribute action_path_full.



74
75
76
# File 'lib/cuca/urlmap.rb', line 74

def action_path_full
  @action_path_full
end

#assignsObject (readonly)

Returns the value of attribute assigns.



67
68
69
# File 'lib/cuca/urlmap.rb', line 67

def assigns
  @assigns
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



71
72
73
# File 'lib/cuca/urlmap.rb', line 71

def base_path
  @base_path
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



70
71
72
# File 'lib/cuca/urlmap.rb', line 70

def base_url
  @base_url
end

#path_treeObject (readonly)

Returns the value of attribute path_tree.



76
77
78
# File 'lib/cuca/urlmap.rb', line 76

def path_tree
  @path_tree
end

#scriptObject (readonly)

Returns the value of attribute script.



68
69
70
# File 'lib/cuca/urlmap.rb', line 68

def script
  @script
end

#subcallObject (readonly)

Returns the value of attribute subcall.



69
70
71
# File 'lib/cuca/urlmap.rb', line 69

def subcall
  @subcall
end

#urlObject (readonly)

current url



66
67
68
# File 'lib/cuca/urlmap.rb', line 66

def url
  @url
end

Instance Method Details

#has_script?(script) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/cuca/urlmap.rb', line 247

def has_script?(script)
   return !(@script == '')
end

#match?(script) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
221
# File 'lib/cuca/urlmap.rb', line 213

def match?(script)
  m_script = @script
  p_script = File.expand_path(script)
  
#   $stderr.puts "URLMap::match - #{m_script} - #{p_script}"
  return (m_script == p_script)
  rescue RoutingError
    false
end

#submatch?(some_path) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
233
234
# File 'lib/cuca/urlmap.rb', line 230

def submatch?(some_path)
#    $stderr.puts "Submatch: #{some_path} with #{@script} - #{(@script.length < some_path.length).inspect} #{@script.include?(some_path)}"
   return false if @script.length < some_path.length
   return @script.include?(some_path)
end

#usubmatch?(some_path) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/cuca/urlmap.rb', line 241

def usubmatch?(some_path)
   @path_info.include?(some_path)
end