Module: Mingle::MingleModels
- Extended by:
- BitGirder::Core::BitGirderMethods
- Defined in:
- lib/mingle.rb
Constant Summary collapse
- NUM_TYPES =
{ :mingle_int64 => MingleInt64, :mingle_int32 => MingleInt32, :mingle_uint32 => MingleUint32, :mingle_uint64 => MingleUint64, :mingle_float64 => MingleFloat64, :mingle_float32 => MingleFloat32, }
Constants included from BitGirder::Core::BitGirderMethods
BitGirder::Core::BitGirderMethods::PARAM_TYPE_ARG, BitGirder::Core::BitGirderMethods::PARAM_TYPE_ENVVAR, BitGirder::Core::BitGirderMethods::PARAM_TYPE_KEY
Class Method Summary collapse
- .as_mingle_boolean(val) ⇒ Object
- .as_mingle_buffer(val) ⇒ Object
- .as_mingle_instance(val, typ) ⇒ Object
- .as_mingle_integer(num) ⇒ Object
- .as_mingle_list(val) ⇒ Object
- .as_mingle_number(val, num_typ) ⇒ Object
- .as_mingle_string(val) ⇒ Object
- .as_mingle_struct(val) ⇒ Object
- .as_mingle_symbol_map(val) ⇒ Object
- .as_mingle_timestamp(val) ⇒ Object
- .as_mingle_value(val) ⇒ Object
- .as_ruby_error(me, trace = nil) ⇒ Object
- .create_coerce_error(val, targ_type) ⇒ Object
-
.get_coerce_type_symbol(typ) ⇒ Object
No assertions or other type checking at this :level => if it’s a sym we assume it is of the form mingle_foo_bar where foo_bar is a coercable type; if a class we assume it is of the form MingleFooBar that we can convert to a symbol of the former form.
- .impl_as_typed_value(val, typ) ⇒ Object
- .impl_string_to_num(str, typ) ⇒ Object
- .raise_as_ruby_error(me) ⇒ Object
Methods included from BitGirder::Core::BitGirderMethods
argv_to_argh, check_fail_prefix, class_name_to_sym, code, compares_to, console, ext_to_class_name, ext_to_sym, has_env, has_key, has_keys, nonnegative, not_nil, positive, raisef, set_from_key, set_var, split_argv, sym_to_cli_switch, sym_to_ext_id, to_bool, unpack_argv_array, unpack_argv_hash, warn
Class Method Details
.as_mingle_boolean(val) ⇒ Object
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 |
# File 'lib/mingle.rb', line 2340 def as_mingle_boolean( val ) case val when MingleBoolean then val when MingleString case val.to_s.strip.downcase when "true" then MingleBoolean::TRUE when "false" then MingleBoolean::FALSE end else raise create_coerce_error( val, MingleBoolean ) end end |
.as_mingle_buffer(val) ⇒ Object
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 |
# File 'lib/mingle.rb', line 2356 def as_mingle_buffer( val ) not_nil( val, "val" ) case val when MingleBuffer then val when MingleString MingleBuffer.new( BitGirder::Io.strict_decode64( val.to_s ) ) else raise create_coerce_error( val, MingleBuffer ) end end |
.as_mingle_instance(val, typ) ⇒ Object
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 |
# File 'lib/mingle.rb', line 2458 def as_mingle_instance( val, typ ) type_sym = get_coerce_type_symbol( not_nil( typ, "typ" ) ) if num_typ = NUM_TYPES[ type_sym ] as_mingle_number( val, num_typ ) else send( :"as_#{type_sym}", val ) end end |
.as_mingle_integer(num) ⇒ Object
2406 2407 2408 2409 2410 2411 2412 2413 2414 |
# File 'lib/mingle.rb', line 2406 def as_mingle_integer( num ) case when MingleInt32.can_hold?( num ) then MingleInt32.new( num ) when MingleInt64.can_hold?( num ) then MingleInt64.new( num ) when MingleUint64.can_hold?( num ) then MingleUint64.new( num ) else raise "Number is out of range for mingle integer types: #{num}" end end |
.as_mingle_list(val) ⇒ Object
2398 2399 2400 |
# File 'lib/mingle.rb', line 2398 def as_mingle_list( val ) impl_as_typed_value( val, MingleList ) end |
.as_mingle_number(val, num_typ) ⇒ Object
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 |
# File 'lib/mingle.rb', line 2325 def as_mingle_number( val, num_typ ) case val when num_typ then val when MingleString num_typ.new( impl_string_to_num( val.to_s, num_typ ) ) when MingleNumber then num_typ.new( val.num ) else raise create_coerce_error( val, num_typ ) end end |
.as_mingle_string(val) ⇒ Object
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 |
# File 'lib/mingle.rb', line 2300 def as_mingle_string( val ) case val when MingleString then val when MingleNumber, MingleBoolean, MingleTimestamp MingleString.new( val.to_s ) else raise create_coerce_error( val, MingleString ) end end |
.as_mingle_struct(val) ⇒ Object
2394 2395 2396 |
# File 'lib/mingle.rb', line 2394 def as_mingle_struct( val ) impl_as_typed_value( val, MingleStruct ) end |
.as_mingle_symbol_map(val) ⇒ Object
2402 2403 2404 |
# File 'lib/mingle.rb', line 2402 def as_mingle_symbol_map( val ) impl_as_typed_value( val, MingleSymbolMap ) end |
.as_mingle_timestamp(val) ⇒ Object
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 |
# File 'lib/mingle.rb', line 2371 def ( val ) not_nil( val, "val" ) case val when MingleTimestamp then val when MingleString then MingleTimestamp.rfc3339( val ) when Time then MingleTimestamp.new( val ) else raise create_coerce_error( val, MingleTimestamp ) end end |
.as_mingle_value(val) ⇒ Object
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 |
# File 'lib/mingle.rb', line 2416 def as_mingle_value( val ) case val when MingleValue then val when String then MingleString.new( val ) when Symbol then MingleString.new( val.to_s ) when TrueClass then MingleBoolean::TRUE when FalseClass then MingleBoolean::FALSE when Integer then as_mingle_integer( val ) when Float, BigDecimal, Rational then MingleFloat64.new( val ) when Array then MingleList.new( val ) when Hash then MingleSymbolMap.create( val ) when Time then ( val ) when nil then MingleNull::INSTANCE else raise "Can't create mingle value for instance of #{val.class}" end end |
.as_ruby_error(me, trace = nil) ⇒ Object
2469 2470 2471 |
# File 'lib/mingle.rb', line 2469 def as_ruby_error( me, trace = nil ) GenericRaisedMingleError.new( not_nil( me, :me ), trace ) end |
.create_coerce_error(val, targ_type) ⇒ Object
2296 2297 2298 |
# File 'lib/mingle.rb', line 2296 def create_coerce_error( val, targ_type ) TypeError.new "Can't coerce value of type #{val.class} to #{targ_type}" end |
.get_coerce_type_symbol(typ) ⇒ Object
No assertions or other type checking at this :level => if it’s a sym we assume it is of the form mingle_foo_bar where foo_bar is a coercable type; if a class we assume it is of the form MingleFooBar that we can convert to a symbol of the former form.
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 |
# File 'lib/mingle.rb', line 2438 def get_coerce_type_symbol( typ ) case typ when Symbol then typ when Class base_name = typ.to_s.split( /::/ )[ -1 ] # Get last name part str = base_name.gsub( /(^[A-Z])|([A-Z])/ ) do |m| ( $2 ? "_" : "" ) + m.downcase end str.to_sym else raise "Unexpected coerce type indicator: #{typ}" end end |
.impl_as_typed_value(val, typ) ⇒ Object
2385 2386 2387 2388 2389 2390 2391 2392 |
# File 'lib/mingle.rb', line 2385 def impl_as_typed_value( val, typ ) if val.is_a?( typ ) val else sym = get_coerce_type_symbol( typ ) raise create_coerce_error( val, typ ) end end |
.impl_string_to_num(str, typ) ⇒ Object
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 |
# File 'lib/mingle.rb', line 2313 def impl_string_to_num( str, typ ) if [ MingleInt64, MingleInt32, MingleUint32, MingleUint64 ]. include?( typ ) str.to_i elsif [ MingleFloat64, MingleFloat32 ].include?( typ ) str.to_f else raise "Unhandled number target type: #{typ}" end end |
.raise_as_ruby_error(me) ⇒ Object
2473 2474 2475 |
# File 'lib/mingle.rb', line 2473 def raise_as_ruby_error( me ) raise as_ruby_error( me ) end |