exocora 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/exocora.rb +3 -3
- data/lib/exocora/errors.rb +4 -8
- data/lib/exocora/sheet.rb +54 -12
- data/lib/exocora/validation.rb +21 -1
- data/lib/exocora/version.rb +1 -1
- data/lib/{array.rb → support/array.rb} +0 -0
- data/lib/{cgi.rb → support/cgi.rb} +1 -1
- data/lib/{string.rb → support/string.rb} +0 -0
- metadata +8 -7
data/lib/exocora.rb
CHANGED
@@ -14,9 +14,9 @@ BASEDIR = File.dirname(File.expand_path(__FILE__))
|
|
14
14
|
$LOAD_PATH.unshift(BASEDIR)
|
15
15
|
$LOAD_PATH.uniq!
|
16
16
|
|
17
|
-
require 'string'
|
18
|
-
require 'array'
|
19
|
-
require 'cgi'
|
17
|
+
require 'support/string'
|
18
|
+
require 'support/array'
|
19
|
+
require 'support/cgi'
|
20
20
|
require 'exocora/support'
|
21
21
|
require 'exocora/errors'
|
22
22
|
require 'exocora/validation'
|
data/lib/exocora/errors.rb
CHANGED
@@ -28,20 +28,16 @@ module Exocora
|
|
28
28
|
|
29
29
|
# Represents an error encountered when processing a template.
|
30
30
|
class TemplateError < RuntimeError
|
31
|
-
attr_accessor :message, :
|
32
|
-
def initialize(message,
|
31
|
+
attr_accessor :message, :original
|
32
|
+
def initialize(message, original)
|
33
33
|
@message = message
|
34
|
-
@
|
34
|
+
@original = original
|
35
35
|
end
|
36
36
|
|
37
37
|
def to_html
|
38
38
|
"<p>" + Erubis::XmlHelper::escape_xml(@message) + "</p>" +
|
39
39
|
"<p>The original exception was:</p>" +
|
40
|
-
|
41
|
-
@exception.to_html
|
42
|
-
else
|
43
|
-
Erubis::XmlHelper::escape_xml(@exception.to_s)
|
44
|
-
end
|
40
|
+
@original.to_html
|
45
41
|
end
|
46
42
|
end
|
47
43
|
end
|
data/lib/exocora/sheet.rb
CHANGED
@@ -22,6 +22,13 @@ module Exocora
|
|
22
22
|
@log_file = self.class.to_s.underscore + '.log'
|
23
23
|
end
|
24
24
|
|
25
|
+
# Add a validation to the validation stack
|
26
|
+
def self.add_validation(param, validation)
|
27
|
+
@@validations[param.to_sym] ||= []
|
28
|
+
@@validations[param.to_sym] << validation
|
29
|
+
validation
|
30
|
+
end
|
31
|
+
|
25
32
|
# Adds a validation condition for a parameter.
|
26
33
|
# validate :query { |q| q.size > 0 }
|
27
34
|
# validate :query, "is invalid" { |q|
|
@@ -31,10 +38,12 @@ module Exocora
|
|
31
38
|
validation.when block
|
32
39
|
end
|
33
40
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
41
|
+
add_validation param, validation
|
42
|
+
end
|
43
|
+
|
44
|
+
# Ensure that a parameter is non-nil
|
45
|
+
def self.validate_presence_of(param, *params)
|
46
|
+
add_validation param, ValidationOfPresence.new(*params)
|
38
47
|
end
|
39
48
|
|
40
49
|
# Creates an instance of this sheet, and runs it.
|
@@ -42,6 +51,12 @@ module Exocora
|
|
42
51
|
new.run
|
43
52
|
end
|
44
53
|
|
54
|
+
# Adds an error on a parameter
|
55
|
+
def add_error(param, error)
|
56
|
+
@errors[param] ||= []
|
57
|
+
@errors[param] << error
|
58
|
+
end
|
59
|
+
|
45
60
|
# Casts incoming CGI parameters to nil, numerics, collapses single-element
|
46
61
|
# arrays, and so forth.
|
47
62
|
def cast_params(params)
|
@@ -57,6 +72,10 @@ module Exocora
|
|
57
72
|
end
|
58
73
|
end
|
59
74
|
|
75
|
+
# I prefer indexing by symbols
|
76
|
+
key = key.to_sym
|
77
|
+
|
78
|
+
# Store cast values
|
60
79
|
if values.empty?
|
61
80
|
cast[key] = nil
|
62
81
|
elsif values.size == 1
|
@@ -99,10 +118,11 @@ module Exocora
|
|
99
118
|
end
|
100
119
|
|
101
120
|
# Breaks the normal rendering flow, and outputs an HTTP redirect header.
|
102
|
-
def redirect_to(uri_fragment)
|
121
|
+
def redirect_to(uri_fragment = @cgi.uri, params = {})
|
103
122
|
@headers['Status'] = '302 Moved'
|
104
|
-
@headers['Location'] = @cgi.full_uri_for
|
123
|
+
@headers['Location'] = url_for(@cgi.full_uri_for(uri_fragment), params)
|
105
124
|
output
|
125
|
+
exit
|
106
126
|
end
|
107
127
|
|
108
128
|
# Renders the erubis template for this action. Takes a hash of variables to
|
@@ -123,8 +143,13 @@ module Exocora
|
|
123
143
|
# Perform templating
|
124
144
|
begin
|
125
145
|
result = eruby.evaluate context
|
126
|
-
rescue
|
127
|
-
|
146
|
+
rescue Exception => e
|
147
|
+
if e.backtrace.first =~ /\(erubis\):(\d+)/
|
148
|
+
message = "encountered error processing template #{template_filename} at line #{$1}"
|
149
|
+
else
|
150
|
+
message = "encountered error processing template #{template_filename}"
|
151
|
+
end
|
152
|
+
raise TemplateError.new(message, e)
|
128
153
|
end
|
129
154
|
|
130
155
|
# Output result
|
@@ -161,6 +186,13 @@ module Exocora
|
|
161
186
|
end
|
162
187
|
end
|
163
188
|
|
189
|
+
# Returns a URL for the given url and parameters
|
190
|
+
def url_for(url = @cgi.uri, params = {})
|
191
|
+
url + params.inject('?') do |query_string, pair|
|
192
|
+
query_string << CGI::escape(pair[0].to_s) + '=' + CGI::escape(pair[1].to_s)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
164
196
|
# Sets the name of the template to render.
|
165
197
|
def use_template(template)
|
166
198
|
@template = template
|
@@ -187,13 +219,23 @@ module Exocora
|
|
187
219
|
unless values.kind_of? Array
|
188
220
|
values = [values]
|
189
221
|
end
|
190
|
-
|
191
|
-
|
222
|
+
|
223
|
+
# if validation.kind_of? ValidationOfPresence
|
224
|
+
# # Make sure we perform a validation even if no parameters are
|
225
|
+
# # present.
|
226
|
+
# begin
|
227
|
+
# validation.validate values.first
|
228
|
+
# rescue ValidationError => e
|
229
|
+
# @errors[param] ||= []
|
230
|
+
# @errors[param] << e
|
231
|
+
# end
|
232
|
+
# end
|
233
|
+
|
234
|
+
values.each do |value|
|
192
235
|
begin
|
193
236
|
validation.validate(value)
|
194
237
|
rescue ValidationError => e
|
195
|
-
|
196
|
-
@errors[param] << e
|
238
|
+
add_error param, e
|
197
239
|
end
|
198
240
|
end
|
199
241
|
end
|
data/lib/exocora/validation.rb
CHANGED
@@ -4,9 +4,11 @@ module Exocora
|
|
4
4
|
# Otherwise, compares filter === value.
|
5
5
|
|
6
6
|
class Validation
|
7
|
+
DEFAULT_MESSAGE = 'must be present'
|
8
|
+
|
7
9
|
attr_accessor :message, :filter, :negate
|
8
10
|
|
9
|
-
def initialize(filter
|
11
|
+
def initialize(filter=Object, message=DEFAULT_MESSAGE)
|
10
12
|
@filter = filter
|
11
13
|
@message = message
|
12
14
|
@negate = false
|
@@ -70,4 +72,22 @@ module Exocora
|
|
70
72
|
alias :with :if
|
71
73
|
alias :when :if
|
72
74
|
end
|
75
|
+
|
76
|
+
# ValidationOfPresence is a special validation which ensures its value
|
77
|
+
# is both present and non-empty.
|
78
|
+
class ValidationOfPresence < Validation
|
79
|
+
DEFAULT_MESSAGE = 'must be present'
|
80
|
+
|
81
|
+
def initialize(message=DEFAULT_MESSAGE)
|
82
|
+
@message = message
|
83
|
+
end
|
84
|
+
|
85
|
+
def validate(value = nil)
|
86
|
+
if value.nil? or (value.respond_to? :empty? and value.empty?)
|
87
|
+
raise ValidationError.new(value, @message)
|
88
|
+
else
|
89
|
+
value
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
73
93
|
end
|
data/lib/exocora/version.rb
CHANGED
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: exocora
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2008-03-
|
6
|
+
version: 0.1.1
|
7
|
+
date: 2008-03-30 00:00:00 -05:00
|
8
8
|
summary: A small framework for cgi scripts
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -20,7 +20,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 1.8.
|
23
|
+
version: 1.8.5
|
24
24
|
version:
|
25
25
|
platform: ruby
|
26
26
|
signing_key:
|
@@ -35,10 +35,11 @@ files:
|
|
35
35
|
- examples/hello_world.cgi
|
36
36
|
- examples/request.rhtml
|
37
37
|
- examples/request.cgi
|
38
|
-
- lib/cgi.rb
|
39
|
-
- lib/array.rb
|
40
38
|
- lib/exocora.rb
|
41
|
-
- lib/
|
39
|
+
- lib/support
|
40
|
+
- lib/support/cgi.rb
|
41
|
+
- lib/support/array.rb
|
42
|
+
- lib/support/string.rb
|
42
43
|
- lib/exocora
|
43
44
|
- lib/exocora/support.rb
|
44
45
|
- lib/exocora/validation.rb
|