Module: SLDB::Util

Included in:
AbstractSLDB
Defined in:
lib/sldb.rb,
lib/sldb-0.2.0.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(c) ⇒ Object

–}}}



29
30
31
32
33
34
# File 'lib/sldb.rb', line 29

def append_features c
#--{{{
  super
  c.extend self 
#--}}}  
end

.export(*syms) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/sldb.rb', line 20

def export(*syms)
#--{{{
  syms.each do |sym|
    sym = "#{ sym }".intern
    module_function sym 
    public sym
  end
#--}}}
end

Instance Method Details

#env_boolean(arg, opts = {}) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/sldb.rb', line 210

def env_boolean arg, opts = {}
#--{{{
  default = Util::getopt 'default', opts
  return default unless arg
  case arg
    when String
      arg =~ %r/true/io ? true : false
    when Symbol
      arg =~ :true ? true : false
    when Numeric
      arg == 1 ? true : false
    else
      arg ? true : false
  end
#--}}}
end

#env_float(arg, opts = {}) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/sldb.rb', line 237

def env_float arg, opts = {} 
#--{{{
  default = Util::getopt 'default', opts
  return default unless arg
  Float "#{ arg }"
#--}}}
end

#env_floatlist(arg, opts = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sldb.rb', line 194

def env_floatlist arg, opts = {}
#--{{{
  default = Util::getopt 'default', opts
  return default unless arg
  list =
    case arg
      when Array
        arg
      else
        "#{ arg }".split %r/,+/
    end
  list.map{|item| env_float item}
#--}}}
end

#env_int(arg, opts = {}) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/sldb.rb', line 228

def env_int arg, opts = {} 
#--{{{
  default = Util::getopt 'default', opts
  return default unless arg
  Integer "#{ arg }"
#--}}}
end

#env_intlist(arg, opts = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/sldb.rb', line 178

def env_intlist arg, opts = {}
#--{{{
  default = Util::getopt 'default', opts
  return default unless arg
  list =
    case arg
      when Array
        arg
      else
        "#{ arg }".split %r/,+/
    end
  list.map{|item| env_int item}
#--}}}
end

#erreq(a, b) ⇒ Object



246
247
248
249
250
251
252
# File 'lib/sldb.rb', line 246

def erreq a, b
#--{{{
  a.class == b.class and
  a.message == b.message and
  a.backtrace == b.backtrace
#--}}}
end

#escape(s, char, esc) ⇒ Object



266
267
268
269
270
271
272
# File 'lib/sldb.rb', line 266

def escape s, char, esc
#--{{{
  s = "#{ s }"
  escape! s, char, esc
  s
#--}}}
end

#escape!(s, char, esc) ⇒ Object



255
256
257
258
259
260
261
262
263
# File 'lib/sldb.rb', line 255

def escape! s, char, esc
#--{{{
  #re = %r/([#{ 0x5c.chr << esc }]*)#{ char }/
  re = %r/([#{ Regexp::quote esc }]*)#{ Regexp::quote char }/
  s.gsub!(re) do
    (($1.size % 2 == 0) ? ($1 << esc) : $1) + char 
  end
#--}}}
end

#fork(*args, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sldb.rb', line 37

def fork(*args, &block)
#--{{{
  begin
    verbose = $VERBOSE
    $VERBOSE = nil
    ::Process::fork(*args, &block)
  ensure
    $VERBOSE = verbose
  end
#--}}}
end

#getopt(opt, hash, default = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sldb.rb', line 50

def getopt opt, hash, default = nil
#--{{{
  key = opt
  return hash[key] if hash.has_key? key
  key = "#{ key }"
  return hash[key] if hash.has_key? key
  key = key.intern
  return hash[key] if hash.has_key? key
  return default
#--}}}
end

#hashify(*hashes) ⇒ Object



71
72
73
74
75
# File 'lib/sldb.rb', line 71

def hashify(*hashes)
#--{{{
  hashes.inject(accum={}){|accum,hash| accum.update hash}
#--}}}
end

#hostObject



85
86
87
88
89
# File 'lib/sldb.rb', line 85

def host
#--{{{
  @__host__ ||= Util::hostname.gsub(%r/\..*$/o,'')
#--}}}
end

#hostnameObject



78
79
80
81
82
# File 'lib/sldb.rb', line 78

def hostname
#--{{{
  @__hostname__ ||= Socket::gethostname
#--}}}
end

#optfilter(*list) ⇒ Object



63
64
65
66
67
68
# File 'lib/sldb.rb', line 63

def optfilter(*list)
#--{{{
  args, opts = [ list ].flatten.partition{|item| not Hash === item}
  [args, Util::hashify(*opts)]
#--}}}
end

#quote(list, qm = "'") ⇒ Object Also known as: q



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/sldb.rb', line 275

def quote list, qm = "'" 
#--{{{
  [ list ].flatten.map do |f| 
    if f
      qm + Util::escape(f,qm,qm) + qm 
    else
      'NULL'
    end
  end
#--}}}
end

#stamptime(string, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sldb.rb', line 148

def stamptime string, opts = {} 
#--{{{
  local = Util::getopt 'local', opts, false 
  string = "#{ string }"
  pat = %r/^\s*(\d\d\d\d)-(\d\d)-(\d\d)[\s_]+(\d\d):(\d\d):(\d\d)(?:.(\d+))?\s*$/o
  match = pat.match string
  raise ArgumentError, "<#{ string.inspect }>" unless match
  yyyy,mm,dd,h,m,s,u = match.to_a[1..-1].map{|m| (m || 0).to_i}
  if local
    Time::local yyyy,mm,dd,h,m,s,u
  else
    Time::gm yyyy,mm,dd,h,m,s,u
  end
#--}}}
end

#system(*args, &block) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sldb.rb', line 165

def system(*args, &block)
#--{{{
  begin
    verbose = $VERBOSE
    $VERBOSE = nil
    ::Kernel::system(*args, &block)
  ensure
    $VERBOSE = verbose
  end
#--}}}
end

#timestamp(opts = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sldb.rb', line 133

def timestamp opts = {}
#--{{{
  time = Util::getopt 'time', opts, Time::now
  local = Util::getopt 'local', opts, false 
  nospace = Util::getopt 'nospace', opts, false
  time = time.utc unless local
  usec = "#{ time.usec }"
  usec << ('0' * (6 - usec.size)) if usec.size < 6 
  stamp = time.strftime('%Y-%m-%d %H:%M:%S.') << usec
  stamp.gsub!(%r/\s+/,'_') if nospace
  stamp
#--}}}
end

#tmpnam(opts = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sldb.rb', line 114

def tmpnam opts = {} 
#--{{{
  dir = Util::getopt 'dir', opts, Dir::tmpdir
  seed = Util::getopt 'seed', opts, Util::prognam
  path = 
    "%s_%s_%s_%s_%d" % [
      Util::hostname, 
      seed,
      Process::pid, 
      Util::timestamp('nospace' => true),
      rand(101010)
    ] 
  dirname, basename = File::dirname(path), File::basename(path)
  tn = File::join(dir, dirname, basename.gsub(%r/[^0-9a-zA-Z]/,'_')).gsub(%r/\s+/, '_')
  File::expand_path tn
#--}}}
end

#uncache(file) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sldb.rb', line 92

def uncache file 
#--{{{
  refresh = nil
  begin
    is_a_file = File === file
    path = (is_a_file ? file.path : file.to_s) 
    stat = (is_a_file ? file.stat : File::stat(file.to_s)) 
    refresh = Util::tmpnam(File::dirname(path))
    File::link path, refresh rescue File::symlink path, refresh
    File::chmod stat.mode, path
    File::utime stat.atime, stat.mtime, path
    open(File::dirname(path)){|d| d.fsync rescue nil}
  ensure 
    begin
      File::unlink refresh if refresh
    rescue Errno::ENOENT
    end
  end
#--}}}
end