Class: Web::Action

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

Overview

:nodoc:

Constant Summary collapse

@@all_actions =
[]

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
# File 'lib/web/action.rb', line 4

def initialize patterns={}, &function
    @patterns = patterns
    @function = function || lambda{}
    @@all_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



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

.run(options = {}) ⇒ Object



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

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

Instance Method Details

#applies?(cgi) ⇒ Boolean

Returns:

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

Returns:

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

#run(cgi = Web::CGI.create) ⇒ Object



10
11
12
# File 'lib/web/action.rb', line 10

def run cgi=Web::CGI.create
    @function.call( cgi )
end