Module: BitGirder::Core::BitGirderMethods
- Included in:
- BitGirder::Concurrent::EmUtils, BitGirderAttribute, BitGirderClass, BitGirderClass, BitGirderClassDefinition, BitGirderClassDefinition, BitGirderError, InstanceMapper, ObjectPath, ObjectPath, MySql, Ops::Java::JavaEnvironments, Ops::Ruby::RubyIncludes, Testing::AssertMethods, Mingle::GenericRaisedMingleError, Mingle::MingleModels, Mingle::MingleSymbolMap, Mingle::MingleTimestamp, Mingle::Service::MingleServices
- Defined in:
- lib/bitgirder/core.rb
Constant Summary collapse
- PARAM_TYPE_ARG =
Each constant corresponds to a prefix in an error message appropriate to that type
"Argument"- PARAM_TYPE_ENVVAR =
"Environment Variable"- PARAM_TYPE_KEY =
"Value for key"
Class Method Summary collapse
- .argv_to_argh(argv, syms) ⇒ Object
- .check_fail_prefix(name, param_type) ⇒ Object
-
.class_name_to_sym(cls_name) ⇒ Object
Changes SomeClass to :some_class.
- .code(*argv) ⇒ Object (also: debug)
- .compares_to(val, comp_target, comp_type, name = nil, param_type = PARAM_TYPE_ARG) ⇒ Object
- .console(*argv) ⇒ Object
-
.ext_to_class_name(str) ⇒ Object
changes "some-arg" to SomeArg (we can add in namespace qualifiers too if needed, for instance 'some-mod/some-other-mod/some-class' becomes SomeMod::SomeOtherMod::SomeClass).
-
.ext_to_sym(str) ⇒ Object
changes "some-arg" to :some_arg.
- .has_env(key) ⇒ Object
-
.has_key(map, key, param_typ = PARAM_TYPE_KEY) ⇒ Object
It is assumed that map responds to []= and is not nil.
- .has_keys(map, *keys) ⇒ Object
- .nonnegative(val, name = nil, param_type = PARAM_TYPE_ARG) ⇒ Object
- .not_nil(val, name = nil, param_type = PARAM_TYPE_ARG) ⇒ Object
- .positive(val, name = nil, param_type = PARAM_TYPE_ARG) ⇒ Object
- .raisef(*argv) ⇒ Object
- .set_from_key(map, *syms) ⇒ Object
-
.set_var(sym, val) ⇒ Object
helper method to do the sym-conversion part only (no validity checking done on either the sym or val).
-
.split_argv(argv) ⇒ Object
Returns two arrays (both possibly empty) of stuff in argv before the first '--', if any, and stuff after, if any, including any subsequent appearances of '--'.
-
.sym_to_cli_switch(sym) ⇒ Object
Changes :some_sym to --some-sym ('cli' == 'command line interface').
-
.sym_to_ext_id(sym) ⇒ Object
Changes :some_sym to "some-sym".
- .to_bool(obj) ⇒ Object
- .unpack_argv_array(arr, vars, trace) ⇒ Object
- .unpack_argv_hash(argh, vars, expct = false) ⇒ Object
- .warn(*argv) ⇒ Object
Class Method Details
.argv_to_argh(argv, syms) ⇒ Object
365 366 367 368 369 370 371 372 373 |
# File 'lib/bitgirder/core.rb', line 365 def argv_to_argh( argv, syms ) argv = unpack_argv_array( argv, syms, caller ) argh = {} argv.size.times { |i| argh[ syms[ i ] ] = argv[ i ] } argh end |
.check_fail_prefix(name, param_type) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/bitgirder/core.rb', line 190 def check_fail_prefix( name, param_type ) # Since we'll most often be using symbols instead of strings for arg # parameter names we special case displaying them as strings name_str = if param_type == PARAM_TYPE_ARG && name.is_a?( Symbol ) %{"#{name_str}"} else name.inspect end "#{param_type} #{name.inspect}" end |
.class_name_to_sym(cls_name) ⇒ Object
Changes SomeClass to :some_class
289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/bitgirder/core.rb', line 289 def class_name_to_sym( cls_name ) not_nil( cls_name, "cls_name" ) if cls_name.size == 0 raise "Name is empty string" else str = cls_name[ 0 ].downcase + cls_name[ 1 .. -1 ]. gsub( /([A-Z])/ ) { |m| "_#{m.downcase}" } str.to_sym end end |
.code(*argv) ⇒ Object Also known as: debug
169 170 171 |
# File 'lib/bitgirder/core.rb', line 169 def code( *argv ) BitGirderLogger.get_logger.code( *argv ) end |
.compares_to(val, comp_target, comp_type, name = nil, param_type = PARAM_TYPE_ARG) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/bitgirder/core.rb', line 217 def compares_to( val, comp_target, comp_type, name = nil, param_type = PARAM_TYPE_ARG ) if val.send( comp_type, comp_target ) val else raise "#{check_fail_prefix( name, param_type )}' is out of range " \ "(#{comp_target} #{comp_type} #{val})" end end |
.console(*argv) ⇒ Object
173 174 175 |
# File 'lib/bitgirder/core.rb', line 173 def console( *argv ) BitGirderLogger.get_logger.console( *argv ) end |
.ext_to_class_name(str) ⇒ Object
changes "some-arg" to SomeArg (we can add in namespace qualifiers too if needed, for instance 'some-mod/some-other-mod/some-class' becomes SomeMod::SomeOtherMod::SomeClass)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/bitgirder/core.rb', line 272 def ext_to_class_name( str ) not_nil( str, "str" ) str = str.to_s res = "" if str.size > 0 res = str[ 0 ].upcase + str[ 1 .. -1 ].gsub( /-(.)/ ) { |m| m[ 1 ].upcase } end res end |
.ext_to_sym(str) ⇒ Object
changes "some-arg" to :some_arg
240 241 242 243 244 245 246 247 |
# File 'lib/bitgirder/core.rb', line 240 def ext_to_sym( str ) not_nil( str, "str" ) # do str.to_s in case it's some random string-ish thing (like a sym) but # not an actual String str.to_s.gsub( /-/, "_" ).to_sym end |
.has_env(key) ⇒ Object
331 332 333 |
# File 'lib/bitgirder/core.rb', line 331 def has_env( key ) has_key( ENV, key, PARAM_TYPE_ENVVAR ) end |
.has_key(map, key, param_typ = PARAM_TYPE_KEY) ⇒ Object
It is assumed that map responds to []= and is not nil. The 'key' parameter may be nil. Returns the value if key is present and not nil
323 324 325 |
# File 'lib/bitgirder/core.rb', line 323 def has_key( map, key, param_typ = PARAM_TYPE_KEY ) not_nil( map[ key ], key, param_typ ) end |
.has_keys(map, *keys) ⇒ Object
327 328 329 |
# File 'lib/bitgirder/core.rb', line 327 def has_keys( map, *keys ) keys.inject( [] ) { |arr, key| arr << has_key( map, key ) } end |
.nonnegative(val, name = nil, param_type = PARAM_TYPE_ARG) ⇒ Object
231 232 233 |
# File 'lib/bitgirder/core.rb', line 231 def nonnegative( val, name = nil, param_type = PARAM_TYPE_ARG ) compares_to( val, 0, :>=, name, param_type ) end |
.not_nil(val, name = nil, param_type = PARAM_TYPE_ARG) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/bitgirder/core.rb', line 204 def not_nil( val, name = nil, param_type = PARAM_TYPE_ARG ) if val.nil? if name raise "#{check_fail_prefix( name, param_type )} cannot be nil" else raise "Value is nil" end else val end end |
.positive(val, name = nil, param_type = PARAM_TYPE_ARG) ⇒ Object
235 236 237 |
# File 'lib/bitgirder/core.rb', line 235 def positive( val, name = nil, param_type = PARAM_TYPE_ARG ) compares_to( val, 0, :>, name, param_type ) end |
.raisef(*argv) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/bitgirder/core.rb', line 388 def raisef( *argv ) raise if argv.empty? cls = nil case v = argv.first when Exception then raise v when Class then cls = argv.shift end argv2 = cls == nil ? [] : [ cls ] argv2 << sprintf( *argv ) unless argv.empty? raise *argv2 end |
.set_from_key(map, *syms) ⇒ Object
335 336 337 |
# File 'lib/bitgirder/core.rb', line 335 def set_from_key( map, *syms ) syms.each { |sym| set_var( sym, has_key( map, sym ) ) } end |
.set_var(sym, val) ⇒ Object
helper method to do the sym-conversion part only (no validity checking done on either the sym or val)
316 317 318 |
# File 'lib/bitgirder/core.rb', line 316 def set_var( sym, val ) instance_variable_set( "@#{sym}".to_sym, val ) end |
.split_argv(argv) ⇒ Object
Returns two arrays (both possibly empty) of stuff in argv before the first '--', if any, and stuff after, if any, including any subsequent appearances of '--'
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/bitgirder/core.rb', line 252 def split_argv( argv ) not_nil( argv, :argv ) first, last = [], nil argv.each do |arg| if last then last << arg else if arg == "--" then last = []; else first << arg end end end [ first, last ] end |
.sym_to_cli_switch(sym) ⇒ Object
Changes :some_sym to --some-sym ('cli' == 'command line interface')
310 311 312 |
# File 'lib/bitgirder/core.rb', line 310 def sym_to_cli_switch( sym ) "--" + sym_to_ext_id( sym ) end |
.sym_to_ext_id(sym) ⇒ Object
Changes :some_sym to "some-sym"
305 306 307 |
# File 'lib/bitgirder/core.rb', line 305 def sym_to_ext_id( sym ) not_nil( sym, "sym" ).to_s.gsub( /_/, "-" ) end |
.to_bool(obj) ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/bitgirder/core.rb', line 375 def to_bool( obj ) case obj when true then true when false then false when nil then false when /^\s*(true|yes)\s*$/i then true when /^\s*(false|no)\s*$/i then false else raise "Invalid boolean string: #{obj}" end end |
.unpack_argv_array(arr, vars, trace) ⇒ Object
355 356 357 358 359 360 361 362 363 |
# File 'lib/bitgirder/core.rb', line 355 def unpack_argv_array( arr, vars, trace ) if arr.size == vars.size arr else msg = "wrong number of arguments (#{arr.size} for #{vars.size})" raise( Exception, msg, trace ) end end |
.unpack_argv_hash(argh, vars, expct = false) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/bitgirder/core.rb', line 339 def unpack_argv_hash( argh, vars, expct = false ) vars.inject( [] ) do |arr, sym| val = argh[ sym ] if val != nil arr << val else raise "Missing parameter: #{sym} in arg hash" if expct end arr end end |
.warn(*argv) ⇒ Object
179 180 181 |
# File 'lib/bitgirder/core.rb', line 179 def warn( *argv ) BitGirderLogger.get_logger.warn( *argv ) end |