Class: Web::Action
Overview
:nodoc:
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 9 |
# File 'lib/web/action.rb', line 4 def initialize patterns={}, &function @patterns = patterns @function = function || lambda{} Thread.current[:actions] ||= [ ] Thread.current[: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
65 66 67 68 69 |
# File 'lib/web/action.rb', line 65 def Action.blank_action blank = Action.new Thread::current[:actions].delete blank blank end |
.pick(cgi) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/web/action.rb', line 52 def Action.pick( cgi ) Thread.current[:actions] ||= [ ] Thread.current[:actions].find_all do |action| action.applies? cgi end.sort do |a, b| a.applies?(cgi) <=> b.applies?(cgi) end.last || Thread.current[:actions].find do |action| action.patterns == {} end || blank_action # (raise Web::Exception.new( "Could not locate action for #{ cgi.multiple_params.inspect }" )) end |
Instance Method Details
#applies?(cgi) ⇒ Boolean
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/web/action.rb', line 29 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
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/web/action.rb', line 15 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 |
#run(cgi = Web::CGI.create) ⇒ Object
11 12 13 |
# File 'lib/web/action.rb', line 11 def run cgi=Web::CGI.create @function.call( cgi ) end |