glue 0.20.0 → 0.21.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +161 -110
- data/INSTALL +12 -12
- data/README +1 -1
- data/Rakefile +43 -45
- data/doc/AUTHORS +5 -5
- data/doc/LICENSE +3 -3
- data/doc/RELEASES +32 -24
- data/install.rb +7 -17
- data/lib/facet/object/alias_class.rb +12 -0
- data/lib/glue.rb +35 -35
- data/lib/glue/array.rb +46 -46
- data/lib/glue/aspects.rb +199 -209
- data/lib/glue/attribute.rb +15 -15
- data/lib/glue/autoreload.rb +1 -1
- data/lib/glue/builder.rb +48 -0
- data/lib/glue/builder/xml.rb +114 -0
- data/lib/glue/cache.rb +189 -0
- data/lib/glue/configuration.rb +108 -90
- data/lib/glue/flexob.rb +17 -17
- data/lib/glue/hash.rb +71 -71
- data/lib/glue/helper.rb +12 -12
- data/lib/glue/idgen.rb +9 -0
- data/lib/glue/idgen/md5.rb +24 -0
- data/lib/glue/idgen/sequential.rb +15 -0
- data/lib/glue/literal_method.rb +44 -0
- data/lib/glue/localization.rb +130 -0
- data/lib/glue/logger.rb +98 -98
- data/lib/glue/misc.rb +7 -7
- data/lib/glue/mixins.rb +19 -19
- data/lib/glue/number.rb +8 -8
- data/lib/glue/object.rb +2 -2
- data/lib/glue/pool.rb +43 -43
- data/lib/glue/property.rb +392 -392
- data/lib/glue/sanitize.rb +34 -34
- data/lib/glue/settings.rb +1 -1
- data/lib/glue/snapshot.rb +104 -0
- data/lib/glue/string.rb +129 -129
- data/lib/glue/time.rb +53 -53
- data/lib/glue/uri.rb +162 -162
- data/lib/glue/validation.rb +421 -421
- data/lib/vendor/blankslate.rb +53 -0
- data/test/glue/builder/tc_xml.rb +56 -0
- data/test/glue/tc_aspects.rb +90 -90
- data/test/glue/tc_attribute.rb +11 -11
- data/test/glue/tc_builder.rb +30 -0
- data/test/glue/tc_configuration.rb +97 -97
- data/test/glue/tc_flexob.rb +10 -10
- data/test/glue/tc_hash.rb +23 -23
- data/test/glue/tc_localization.rb +49 -0
- data/test/glue/tc_logger.rb +31 -31
- data/test/glue/tc_numbers.rb +9 -9
- data/test/glue/tc_property.rb +67 -67
- data/test/glue/tc_property_mixins.rb +17 -17
- data/test/glue/tc_property_type_checking.rb +13 -13
- data/test/glue/tc_strings.rb +94 -94
- data/test/glue/tc_uri.rb +65 -65
- data/test/glue/tc_validation.rb +196 -196
- metadata +26 -4
data/lib/glue/time.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# * George Moschovitis <gm@navel.gr>
|
2
2
|
# (c) 2004-2005 Navel, all rights reserved.
|
3
|
-
# $Id: time.rb
|
3
|
+
# $Id: time.rb 182 2005-07-22 10:07:50Z gmosx $
|
4
4
|
|
5
5
|
require 'time.rb'
|
6
6
|
|
@@ -20,65 +20,65 @@ module Glue;
|
|
20
20
|
|
21
21
|
module TimeUtils
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
NOW = Time.now
|
24
|
+
NEVER = Time.mktime(2038)
|
25
|
+
ZERO = Time.mktime(1972)
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
27
|
+
# Convert the time to a nice String representation.
|
28
|
+
|
29
|
+
def self.date_time(time)
|
30
|
+
return nil unless time
|
31
|
+
return time.strftime("%d-%m-%Y %H:%M")
|
32
|
+
end
|
33
|
+
|
34
|
+
# This method calculates the days extrema given two time objects.
|
35
|
+
# start time is the given time1 at 00:00:00
|
36
|
+
# end time is the given time2 at 23:59:59:999
|
37
|
+
#
|
38
|
+
# Input:
|
39
|
+
# - the two times (if only time1 is provided then you get an extrema
|
40
|
+
# of exactly one day extrema.
|
41
|
+
#
|
42
|
+
# Output
|
43
|
+
# - the time range. you can get the start/end times using
|
44
|
+
# range methods.
|
45
|
+
|
46
|
+
def self.days_extrema(time1, time2=nil)
|
47
|
+
time2 = time1 if (not time2.valid? Time)
|
48
|
+
time2 = NEVER if (time2 <= time1)
|
49
|
+
start_time = Time.self.start_of_day(time1)
|
50
|
+
end_time = self.end_of_day(time2)
|
51
|
+
return (start_time..end_time)
|
52
|
+
end
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
# Set time to start of day
|
55
|
+
|
56
|
+
def self.start_of_day(time)
|
57
|
+
return Time.mktime(time.year, time.month, time.day, 0, 0, 0, 0)
|
58
|
+
end
|
59
59
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
60
|
+
|
61
|
+
# Set time to end of day
|
62
|
+
|
63
|
+
def self.end_of_day(time)
|
64
|
+
return Time.mktime(time.year, time.month, time.day, 23, 59, 59, 999)
|
65
|
+
end
|
66
66
|
|
67
67
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
68
|
+
# Returns true only if day of time is included in the
|
69
|
+
# range (stime..etime). Only year days are checked.
|
70
|
+
|
71
|
+
def self.time_in_day_range(time, stime=ZERO, etime=NEVER)
|
72
|
+
if (etime <= stime)
|
73
|
+
Logger.debug "Invalid end time (#{etime} < #{stime})" if $DBG
|
74
|
+
etime = NEVER
|
75
|
+
end
|
76
76
|
|
77
|
-
|
78
|
-
|
77
|
+
stime = start_of_day(stime)
|
78
|
+
etime = end_of_day(etime)
|
79
79
|
|
80
|
-
|
81
|
-
|
80
|
+
return (stime..etime).include?(time)
|
81
|
+
end
|
82
82
|
|
83
83
|
end
|
84
84
|
|
data/lib/glue/uri.rb
CHANGED
@@ -21,168 +21,168 @@ module Glue
|
|
21
21
|
|
22
22
|
module UriUtils
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
24
|
+
# Decode the uri components.
|
25
|
+
|
26
|
+
def self.decode(uri)
|
27
|
+
# gmosx: hmm is this needed?
|
28
|
+
# guard against invalid filenames for example pictures with
|
29
|
+
# spaces uploaded by users
|
30
|
+
escaped_uri = uri.gsub(/ /, "+")
|
31
|
+
|
32
|
+
if md = URI::REGEXP::REL_URI.match(escaped_uri)
|
33
|
+
|
34
|
+
path = "#{md[5]}#{md[6]}"
|
35
|
+
type = File.extname(path)
|
36
|
+
query_string = md[7]
|
37
|
+
|
38
|
+
# real_path = "#{$root_dir}/#{path}"
|
39
|
+
|
40
|
+
parameters = UriUtils.query_string_to_hash(query_string)
|
41
|
+
path.gsub!(/\+/, " ")
|
42
|
+
|
43
|
+
return [path, type, parameters, query_string]
|
44
|
+
|
45
|
+
end # match
|
46
|
+
|
47
|
+
# this is usefull for uncovering bugs!
|
48
|
+
raise ArgumentError.new("the parameter '#{uri}' is not a valid uri")
|
49
|
+
end
|
50
|
+
|
51
|
+
# Extend the basic query string parser provided by the cgi module.
|
52
|
+
# converts single valued params (the most common case) to
|
53
|
+
# objects instead of arrays
|
54
|
+
#
|
55
|
+
# Input:
|
56
|
+
# the query string
|
57
|
+
#
|
58
|
+
# Output:
|
59
|
+
# hash of parameters, contains arrays for multivalued parameters
|
60
|
+
# (multiselect, checkboxes , etc)
|
61
|
+
# If no query string is provided (nil or "") returns an empty hash.
|
62
|
+
|
63
|
+
def self.query_string_to_hash(query_string)
|
64
|
+
return {} unless query_string
|
65
|
+
|
66
|
+
query_parameters = CGI::parse(query_string)
|
67
|
+
|
68
|
+
query_parameters.each { |key, val|
|
69
|
+
# replace the array with an object
|
70
|
+
query_parameters[key] = val[0] if 1 == val.length
|
71
|
+
}
|
72
|
+
|
73
|
+
# set default value to nil! cgi sets this to []
|
74
|
+
query_parameters.default = nil
|
75
|
+
|
76
|
+
return query_parameters
|
77
|
+
end
|
78
|
+
|
79
|
+
# Given a hash with parameter/value pairs construct a
|
80
|
+
# standard query string. This method only encodes simple
|
81
|
+
# types (Numeric, String) to avoid query string polution
|
82
|
+
# with marshal, etc.
|
83
|
+
#
|
84
|
+
# gmosx, FIXME: only numeric and strings are passed to
|
85
|
+
# the latest code, so update old code and optimize this!
|
86
|
+
#
|
87
|
+
# Input:
|
88
|
+
# the parameter hash
|
89
|
+
#
|
90
|
+
# Output:
|
91
|
+
# the query string
|
92
|
+
|
93
|
+
def self.hash_to_query_string(parameters)
|
94
|
+
return nil unless parameters
|
95
|
+
pairs = []
|
96
|
+
parameters.each { |param, value|
|
97
|
+
# only encode simple classes !
|
98
|
+
|
99
|
+
if value.is_a?(Numeric) or value.is_a?(String)
|
100
|
+
pairs << "#{param}=#{value}"
|
101
|
+
end
|
102
|
+
}
|
103
|
+
return pairs.join(";")
|
104
|
+
end
|
105
|
+
|
106
|
+
# This method returns the query string of a uri
|
107
|
+
#
|
108
|
+
# Input:
|
109
|
+
# the uri
|
110
|
+
#
|
111
|
+
# Output:
|
112
|
+
# the query string.
|
113
|
+
# returns nil if no query string
|
114
|
+
|
115
|
+
def self.get_query_string(uri)
|
116
|
+
return nil unless uri
|
117
|
+
# gmosx: INVESTIGATE ruby's URI seems to differently handle
|
118
|
+
# abs and rel uris.
|
119
|
+
if md = URI::REGEXP::ABS_URI.match(uri)
|
120
|
+
return md[8]
|
121
|
+
elsif md = URI::REGEXP::REL_URI.match(uri)
|
122
|
+
return md[7]
|
123
|
+
end
|
124
|
+
return nil
|
125
|
+
end
|
126
|
+
|
127
|
+
# Removes the query string from a uri
|
128
|
+
#
|
129
|
+
# Input:
|
130
|
+
# the uri
|
131
|
+
#
|
132
|
+
# Output:
|
133
|
+
# the chomped uri.
|
134
|
+
|
135
|
+
def self.chomp_query_string(uri)
|
136
|
+
return nil unless uri
|
137
|
+
query_string = self.get_query_string(uri)
|
138
|
+
return uri.dup.chomp("?#{query_string}")
|
139
|
+
end
|
140
|
+
|
141
|
+
# Get a uri and a hash of parameters. Inject the hash values
|
142
|
+
# as parameters in the query sting path. Returns the full
|
143
|
+
# uri.
|
144
|
+
#
|
145
|
+
# Input:
|
146
|
+
# the uri to filter (String)
|
147
|
+
# hash of parameters to update
|
148
|
+
#
|
149
|
+
# Output:
|
150
|
+
# the full updated query string
|
151
|
+
#
|
152
|
+
# === TODO:
|
153
|
+
# optimize this a litle bit.
|
154
|
+
|
155
|
+
def self.update_query_string(uri, parameters)
|
156
|
+
query_string = self.get_query_string(uri)
|
157
|
+
rest = uri.dup.gsub(/\?#{query_string}/, "")
|
158
|
+
|
159
|
+
hash = self.query_string_to_hash(query_string)
|
160
|
+
hash.update(parameters)
|
161
|
+
query_string = self.hash_to_query_string(hash)
|
162
|
+
|
163
|
+
if Glue::StringUtils.valid?(query_string)
|
164
|
+
return "#{rest}?#{query_string}"
|
165
|
+
else
|
166
|
+
return rest
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# TODO: find a better name.
|
171
|
+
# Gets the request uri, injects extra parameters in the query string
|
172
|
+
# and returns a new uri. The request object is not modified.
|
173
|
+
# There is always a qs string so an extra test is skipped.
|
174
|
+
|
175
|
+
def self.update_request_uri(request, parameters)
|
176
|
+
hash = request.parameters.dup()
|
177
|
+
hash.update(parameters)
|
178
|
+
|
179
|
+
# use this in hash_to_querystring.
|
180
|
+
query_string = hash.collect { |k, v|
|
181
|
+
"#{k}=#{v}"
|
182
|
+
}.join(";")
|
183
|
+
|
184
|
+
return "#{request.translated_uri}?#{query_string}"
|
185
|
+
end
|
186
186
|
|
187
187
|
end
|
188
188
|
|