og 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/AUTHORS +14 -4
  2. data/ChangeLog +192 -1
  3. data/README.og +2 -1
  4. data/RELEASES.og +35 -0
  5. data/Rakefile +1 -1
  6. data/examples/og/mock_example.rb +6 -9
  7. data/examples/og/mysql_to_psql.rb +100 -0
  8. data/examples/og/run.rb +8 -17
  9. data/lib/glue/array.rb +1 -1
  10. data/lib/glue/attribute.rb +86 -0
  11. data/lib/glue/cache.rb +1 -1
  12. data/lib/glue/hash.rb +1 -1
  13. data/lib/glue/inflector.rb +1 -1
  14. data/lib/glue/logger.rb +118 -18
  15. data/lib/glue/mixins.rb +1 -1
  16. data/lib/glue/number.rb +1 -1
  17. data/lib/glue/pool.rb +1 -1
  18. data/lib/glue/property.rb +48 -31
  19. data/lib/glue/string.rb +1 -1
  20. data/lib/glue/time.rb +2 -2
  21. data/lib/glue/validation.rb +400 -0
  22. data/lib/glue.rb +7 -8
  23. data/lib/og/backend.rb +47 -46
  24. data/lib/og/backends/mysql.rb +64 -63
  25. data/lib/og/backends/psql.rb +73 -72
  26. data/lib/og/connection.rb +7 -8
  27. data/lib/og/enchant.rb +80 -0
  28. data/lib/og/meta.rb +21 -21
  29. data/lib/og/mock.rb +31 -88
  30. data/lib/og/version.rb +6 -5
  31. data/lib/og.rb +95 -129
  32. data/test/tc_og.rb +3 -3
  33. data/vendor/extensions/_base.rb +153 -0
  34. data/vendor/extensions/_template.rb +36 -0
  35. data/vendor/extensions/all.rb +21 -0
  36. data/vendor/extensions/array.rb +68 -0
  37. data/vendor/extensions/binding.rb +224 -0
  38. data/vendor/extensions/class.rb +50 -0
  39. data/vendor/extensions/continuation.rb +71 -0
  40. data/vendor/extensions/enumerable.rb +250 -0
  41. data/vendor/extensions/hash.rb +23 -0
  42. data/vendor/extensions/io.rb +58 -0
  43. data/vendor/extensions/kernel.rb +42 -0
  44. data/vendor/extensions/module.rb +114 -0
  45. data/vendor/extensions/numeric.rb +230 -0
  46. data/vendor/extensions/object.rb +164 -0
  47. data/vendor/extensions/ostruct.rb +41 -0
  48. data/vendor/extensions/string.rb +316 -0
  49. data/vendor/extensions/symbol.rb +28 -0
  50. metadata +24 -4
  51. data/lib/glue/property.rb.old +0 -307
data/lib/glue/logger.rb CHANGED
@@ -1,45 +1,144 @@
1
- # = Logger
2
- #
3
- # A simple wrapper arround the Ruby logger. Mainly for compatibility
4
- # purposes.
5
- #
6
1
  # code:
7
2
  # * George Moschovitis <gm@navel.gr>
8
3
  #
9
4
  # (c) 2004 Navel, all rights reserved.
10
5
  # $Id: logger.rb 161 2004-11-18 10:51:51Z gmosx $
11
6
 
12
- require "logger"
7
+ require 'logger'
13
8
 
14
9
  # = Logger
15
10
  #
16
- # A simple wrapper arround the Ruby logger. Mainly for compatibility
17
- # purposes.
11
+ # A simple wrapper arround the Ruby logger. Mainly for
12
+ # compatibility purposes.
18
13
  #
19
14
  class Logger
20
15
  alias_method :devel, :debug
21
16
  alias_method :fine, :debug
22
17
 
23
- def detach
18
+ # Prints a trace message to DEBUGLOG (at debug level).
19
+ # Useful for emitting the value of variables, etc. Use
20
+ # like this:
21
+ #
22
+ # x = y = 5
23
+ # trace 'x' # -> 'x = 5'
24
+ # trace 'x ** y' # -> 'x ** y = 3125'
25
+ #
26
+ # If you have a more complicated value, like an array of
27
+ # hashes, then you'll probably want to use an alternative
28
+ # output format. For instance:
29
+ #
30
+ # trace 'value', :yaml
31
+ #
32
+ # Valid output format values (the _style_ parameter) are:
33
+ #
34
+ # :p :inspect
35
+ # :pp (pretty-print, using 'pp' library)
36
+ # :s :to_s
37
+ # :y :yaml :to_yaml (using the 'yaml' library')
38
+ #
39
+ # The default is <tt>:p</tt>.
40
+ #
41
+ # CREDITS:
42
+ #
43
+ # This code comes straight from the dev-utils Gem.
44
+ # Author: Gavin Sinclair <gsinclair@soyabean.com.au>
45
+ #
46
+ def trace(expr, style=:p)
47
+ unless expr.respond_to? :to_str
48
+ warn "trace: Can't evaluate the given value: #{caller.first}"
49
+ else
50
+ require 'extensions/binding'
51
+
52
+ Binding.of_caller do |b|
53
+ value = b.eval(expr.to_str)
54
+ formatter = TRACE_STYLES[style] || :inspect
55
+ case formatter
56
+ when :pp then require 'pp'
57
+ when :y, :yaml, :to_yaml then require 'yaml'
58
+ end
59
+ value_s = value.send(formatter)
60
+ message = "#{expr} = #{value_s}"
61
+ lines = message.split(/\n/)
62
+ indent = " "
63
+ debug(lines.shift)
64
+ lines.each do |line|
65
+ debug(indent + line)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ TRACE_STYLES = {} # :nodoc:
71
+ TRACE_STYLES.update( :pp => :pp_s, :s => :to_s, :p => :inspect, :y => :to_yaml,
72
+ :yaml => :to_yaml, :inspect => :inspect, :to_yaml => :to_yaml )
73
+
74
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75
+ # Global logger interface.
76
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77
+
78
+ # Override the logger with your custom version.
79
+ #
80
+ @@global_logger = Logger.new(STDERR)
81
+
82
+ def self.set(logger)
83
+ @@global_logger = logger
84
+ end
85
+
86
+ def self.get
87
+ @@global_logger
24
88
  end
25
89
 
26
- # Used for debuging, remove this in release code.
27
- #
28
- def d(str)
29
- self << "#{str}\n"
90
+ def self.warn(str)
91
+ @@global_logger.warn(str)
30
92
  end
31
93
 
32
- # Inspect an object. Used for debugging, remove this in release
33
- # code.
34
- #
35
- def i(obj)
36
- self << "Inspect #{obj.inspect()}\n"
94
+ def self.info(str)
95
+ @@global_logger.info(str)
96
+ end
97
+
98
+ def self.debug(str)
99
+ @@global_logger.debug(str)
100
+ end
101
+
102
+ def self.error(str)
103
+ @@global_logger.error(str)
37
104
  end
38
105
 
106
+ #--
107
+ # Saddly have to duplicate the code to make
108
+ # Binding.of_caller work.
109
+ #++
110
+ def self.trace(expr, style=:p)
111
+ unless expr.respond_to? :to_str
112
+ warn "trace: Can't evaluate the given value: #{caller.first}"
113
+ else
114
+ require 'extensions/binding'
115
+
116
+ Binding.of_caller do |b|
117
+ value = b.eval(expr.to_str)
118
+ formatter = TRACE_STYLES[style] || :inspect
119
+ case formatter
120
+ when :pp then require 'pp'
121
+ when :y, :yaml, :to_yaml then require 'yaml'
122
+ end
123
+ value_s = value.send(formatter)
124
+ message = "#{expr} = #{value_s}"
125
+ lines = message.split(/\n/)
126
+ indent = " "
127
+ debug(lines.shift)
128
+ lines.each do |line|
129
+ debug(indent + line)
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
136
+
39
137
  private
40
138
 
41
139
  # the default Ruby logger has a hardwired silly format.
42
140
  # we use some Ruby magic to fix it!
141
+
43
142
  remove_const "Format"
44
143
 
45
144
  Format = "%5s: %s\n"
@@ -47,5 +146,6 @@ class Logger
47
146
  def format_message(severity, timestamp, msg, progname)
48
147
  Format % [severity, msg]
49
148
  end
149
+
50
150
  end
51
151
 
data/lib/glue/mixins.rb CHANGED
@@ -9,7 +9,7 @@
9
9
  # (c) 2004 Navel, all rights reserved.
10
10
  # $Id$
11
11
 
12
- module G;
12
+ module N
13
13
 
14
14
  # = Expirable
15
15
  #
data/lib/glue/number.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # (c) 2004 Navel, all rights reserved.
5
5
  # $Id: number.rb 167 2004-11-23 14:03:10Z gmosx $
6
6
 
7
- module G;
7
+ module N;
8
8
 
9
9
  # = NumberUtils
10
10
  #
data/lib/glue/pool.rb CHANGED
@@ -7,7 +7,7 @@
7
7
  require "thread"
8
8
  require "monitor"
9
9
 
10
- module G
10
+ module N
11
11
 
12
12
  # = Pool
13
13
  #
data/lib/glue/property.rb CHANGED
@@ -1,15 +1,13 @@
1
1
  # code:
2
2
  # * George Moschovitis <gm@navel.gr>
3
- # design:
4
- # * Anastastios Koutoumanos <ak@navel.gr>
5
3
  #
6
4
  # (c) 2004 Navel, all rights reserved.
7
5
  # $Id: property.rb 200 2004-12-27 11:24:41Z gmosx $
8
6
 
9
- require "glue/array"
10
- require "glue/hash"
7
+ require 'glue/array'
8
+ require 'glue/hash'
11
9
 
12
- module G
10
+ module N
13
11
 
14
12
  # = Property
15
13
  #
@@ -61,14 +59,17 @@ module PropertyUtils
61
59
  # Add accessors to the properties to the given target
62
60
  # (Module or Class). For simplicity also create the
63
61
  # meta accessors.
62
+ #
63
+ # [+target+]
64
+ # The target class or module
64
65
  #--
65
66
  # gmosx: Perhaps we 'll optimize this in the future.
66
67
  #++
67
- def self.enchant(target)
68
+ def self.enchant(target, force = false)
68
69
  unless target.singleton_methods.include?('__props')
69
70
  target.module_eval <<-"end_eval", __FILE__, __LINE__
70
- @@__meta = G::SafeHash.new
71
- @@__props = G::SafeArray.new
71
+ @@__meta = N::SafeHash.new
72
+ @@__props = N::SafeArray.new
72
73
 
73
74
  def self.__props
74
75
  @@__props
@@ -98,7 +99,8 @@ module PropertyUtils
98
99
 
99
100
  # copy the metadata.
100
101
  src.__meta.each do |k, val|
101
- val.each { |v| dest.meta(k, v) } if val
102
+ dest.__meta[k] = val.dup
103
+ # val.each { |v| dest.meta(k, v) } if val
102
104
  end
103
105
  end
104
106
 
@@ -132,19 +134,19 @@ module PropertyUtils
132
134
 
133
135
  def __force_#{s}(val)
134
136
  self.#{s}=(} + case klass.name
135
- when Fixnum.name
136
- "val.to_i()"
137
- when String.name
138
- "val.to_s()"
139
- when Float.name
140
- "val.to_f()"
141
- when Time.name
142
- "Time.parse(val.to_s())"
143
- when TrueClass.name, FalseClass.name
144
- "val.to_i() > 0"
145
- else
146
- "val"
147
- end + %{)
137
+ when Fixnum.name
138
+ "val.to_i()"
139
+ when String.name
140
+ "val.to_s()"
141
+ when Float.name
142
+ "val.to_f()"
143
+ when Time.name
144
+ "Time.parse(val.to_s())"
145
+ when TrueClass.name, FalseClass.name
146
+ "val.to_i() > 0"
147
+ else
148
+ "val"
149
+ end + %{)
148
150
  end
149
151
  }
150
152
  end
@@ -161,7 +163,13 @@ module PropertyUtils
161
163
  end
162
164
  }
163
165
  end
164
-
166
+
167
+ # Get the property metadata for the given symbol.
168
+ #
169
+ def self.get_prop(klass, sym)
170
+ return klass.__props.find { |p| p.symbol == sym }
171
+ end
172
+
165
173
  end
166
174
 
167
175
  end # module
@@ -182,6 +190,7 @@ class Module
182
190
  # --> creates reader and writer.
183
191
  #
184
192
  def prop(*params)
193
+
185
194
  meta = {}
186
195
  klass = Object
187
196
 
@@ -200,7 +209,7 @@ class Module
200
209
  end
201
210
  end
202
211
 
203
- G::PropertyUtils.enchant(self)
212
+ N::PropertyUtils.enchant(self)
204
213
 
205
214
  if self.is_a?(Class)
206
215
 
@@ -209,8 +218,8 @@ class Module
209
218
  self.module_eval <<-"end_eval", __FILE__, __LINE__
210
219
 
211
220
  def self.inherited(sub)
212
- G::PropertyUtils.enchant(sub)
213
- G::PropertyUtils.copy_props(self, sub)
221
+ N::PropertyUtils.enchant(sub)
222
+ N::PropertyUtils.copy_props(self, sub)
214
223
  # gmosx: We have to define @@__props first to avoid reusing
215
224
  # the hash from the module. super must stay at the end.
216
225
  super
@@ -225,10 +234,15 @@ class Module
225
234
  self.module_eval <<-"end_eval", __FILE__, __LINE__
226
235
 
227
236
  def self.append_features(base)
228
- G::PropertyUtils.enchant(base)
229
- G::PropertyUtils.copy_props(self, base)
237
+ N::PropertyUtils.enchant(base)
238
+ N::PropertyUtils.copy_props(self, base)
230
239
  # gmosx: We have to define @@__props first to avoid reusing
231
240
  # the hash from the module. super must stay at the end.
241
+
242
+ unless base.included_modules.include?(N::Validation)
243
+ base.module_eval %{ include N::Validation }
244
+ end if defined?(N::Validation)
245
+
232
246
  super
233
247
  end
234
248
 
@@ -236,7 +250,7 @@ class Module
236
250
 
237
251
  end
238
252
 
239
- property = G::Property.new(symbol, klass, meta)
253
+ property = N::Property.new(symbol, klass, meta)
240
254
 
241
255
  reader = meta[:reader] || true
242
256
  writer = writer || meta[:writer] || false
@@ -248,7 +262,10 @@ class Module
248
262
  meta[:writer] = true if meta[:writer].nil?
249
263
  end
250
264
 
251
- G::PropertyUtils.add_prop(self, property)
265
+ N::PropertyUtils.add_prop(self, property)
266
+
267
+ # gmosx: should be placed AFTER enchant!
268
+ module_eval %{ include N::Validation } if defined?(N::Validation)
252
269
  end
253
270
 
254
271
  # Helper method. Accepts a collection of symbols and generates
@@ -303,7 +320,7 @@ class Module
303
320
  # the meta hash.
304
321
  meta = param
305
322
  else
306
- raise "Error when defining property!"
323
+ raise 'Error when defining property!'
307
324
  end
308
325
  end
309
326
 
data/lib/glue/string.rb CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  require "uri"
10
10
 
11
- module G;
11
+ module N;
12
12
 
13
13
  # = StringUtils
14
14
  #
data/lib/glue/time.rb CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  require "time.rb"
8
8
 
9
- module G;
9
+ module N;
10
10
 
11
11
  # = Time
12
12
  #
@@ -77,7 +77,7 @@ module TimeUtils
77
77
  #
78
78
  def self.time_in_day_range(time, stime=ZERO, etime=NEVER)
79
79
  if (etime <= stime)
80
- $log.debug "Invalid end time (#{etime} < #{stime})" if $DBG
80
+ Logger.debug "Invalid end time (#{etime} < #{stime})" if $DBG
81
81
  etime = NEVER
82
82
  end
83
83