Class: Web::Action
Overview
:nodoc:
Constant Summary collapse
- @@all_actions =
[]
Instance Attribute Summary collapse
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Class Method Summary collapse
Instance Method Summary collapse
- #applies?(cgi) ⇒ Boolean
-
#initialize(patterns = {}, &function) ⇒ Action
constructor
A new instance of Action.
- #old_applies?(cgi) ⇒ Boolean
- #run(cgi = Web::CGI.create) ⇒ Object
Constructor Details
#initialize(patterns = {}, &function) ⇒ Action
Returns a new instance of Action.
4 5 6 7 8 |
# File 'lib/web/action.rb', line 4 def initialize patterns={}, &function @patterns = patterns @function = function || lambda{} @@all_actions.push self end |
Instance Attribute Details
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
3 4 5 |
# File 'lib/web/action.rb', line 3 def patterns @patterns end |
Class Method Details
.blank_action ⇒ Object
64 65 66 67 68 |
# File 'lib/web/action.rb', line 64 def Action.blank_action blank = Action.new @@all_actions.delete blank blank end |
.pick(cgi) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/web/action.rb', line 52 def Action.pick( cgi ) @@all_actions.find_all{ |action| action.applies? cgi }.sort{|a, b| a.applies?(cgi) <=> b.applies?(cgi) }.last || @@all_actions.find{ |action| action.patterns == {} } || blank_action # (raise Web::Exception.new( "Could not locate action for #{ cgi.multiple_params.inspect }" )) end |
Instance Method Details
#applies?(cgi) ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/web/action.rb', line 28 def applies? cgi relevance = 0; unmatched_terms = patterns.size; patterns.each{ |key, pattern| if pattern.kind_of? Regexp if cgi.multiple_params[key].find{ |value| value =~ pattern } unmatched_terms = unmatched_terms - 1 relevance += 1 end else if cgi.multiple_params[key].find{ |value| value == pattern } unmatched_terms = unmatched_terms - 1 relevance += 10 end end } if unmatched_terms == 0 relevance else false end end |
#old_applies?(cgi) ⇒ Boolean
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/web/action.rb', line 14 def old_applies? cgi matches = false patterns.each{ |key, pattern| cgi.multiple_params[key].each{ |value| if pattern.kind_of? Regexp matches = true if value =~ pattern else matches = true if value == pattern end } } matches end |