Class: Web::Action

Inherits:
Object show all
Defined in:
lib/web/action.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#patternsObject (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_actionObject



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

.run(options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/web/action.rb', line 71

def Action.run options={}
  if options.kind_of? Hash
    Web.process(options) do |cgi|
      pick(cgi).run(cgi)
    end
  elsif options.kind_of? Web::CGI
    pick(options).run(options)
    options.close
    options
  end
end

Instance Method Details

#applies?(cgi) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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