Class: UrlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/restful_acl/url_parser.rb

Overview

This class takes a User and block of text containing a URL and deduces the requested action and any object that that action will be taken upon.

Author

Matt Darby ([email protected])

Copyright

Copyright© 2009 Matt Darby

License

MIT

Constant Summary collapse

TypesOfURLs =
[
  {:name => "parent_with_specific_child", :controller_bit => 3, :object_id_bit => 4,   :regex => /\/(\w+)\/(\d+)[\w-]*\/(\w+)\/(\d+)[\w-]*$/},
  {:name => "parent_with_edit_child",     :controller_bit => 3, :object_id_bit => 4,   :regex => /\/(\w+)\/(\d+)[\w-]*\/(\w+)\/(\d+)[\w-]*\/edit$/},
  {:name => "parent_with_child_index",    :controller_bit => 3, :object_id_bit => nil, :regex => /\/(\w+)\/(\d+)\/\b(?!edit\b)(\w+)$/},
  {:name => "parent_with_new_child",      :controller_bit => 3, :object_id_bit => nil, :regex => /\/(\w+)\/(\d+)[\w-]*\/(\w+)\/new$/},
  {:name => "edit_singleton_child",       :controller_bit => 3, :object_id_bit => nil, :regex => /\/(\w+)\/(\d+)[\w-]*\/(\w+)\/edit$/},
  {:name => "new_singleton_child",        :controller_bit => 3, :object_id_bit => nil, :regex => /\/(\w+)\/(\d+)[\w-]*\/(\w+)\/new$/},
  {:name => "edit_parent",                :controller_bit => 1, :object_id_bit => 2,   :regex => /\/(\w+)\/(\d+)[\w-]*\/edit$/},
  {:name => "new_parent",                 :controller_bit => 1, :object_id_bit => nil, :regex => /\/(\w+)\/new$/},
  {:name => "specific_parent",            :controller_bit => 1, :object_id_bit => 2,   :regex => /\/(\w+)\/(\d+)[\w-]*$/},
  {:name => "parent_index",               :controller_bit => 1, :object_id_bit => nil, :regex => /\/(\w+)$/}
]
URL =
/href="(.*?)"/
NewURL =
/\/new$/
EditURL =
/\/edit$/
ObjectURL =
/\/(\d+)[\w-]*$/
DestroyURL =
/data-method="delete"/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, &block) ⇒ UrlParser

Returns a new instance of UrlParser.



50
51
52
53
54
# File 'lib/restful_acl/url_parser.rb', line 50

def initialize(user, &block)
  @text = yield
  @user = user
  @url  = requested_url
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



29
30
31
# File 'lib/restful_acl/url_parser.rb', line 29

def text
  @text
end

#urlObject

Returns the value of attribute url.



29
30
31
# File 'lib/restful_acl/url_parser.rb', line 29

def url
  @url
end

#userObject

Returns the value of attribute user.



29
30
31
# File 'lib/restful_acl/url_parser.rb', line 29

def user
  @user
end

Instance Method Details

#options_hashObject

Parse a URL and return a hash suitable for usage with RESTful_ACL

  • :controller_name => The requested action’s controller’s name,

  • :object_id => The requested ID of the object in question (nil when Index, New, Create actions),

  • :action => The requested RESTful action (index, show, etc.),

  • :uri => The requested URL,

  • :user => The current user (used for context in RESTful_ACL)



62
63
64
# File 'lib/restful_acl/url_parser.rb', line 62

def options_hash
  invoke_url_type_method(deduce_url_type)
end