Module: Mingle::CastImpl

Defined in:
lib/mingle.rb

Class Method Summary collapse

Class Method Details

.cast_atomic_value(val, typ, path) ⇒ Object



2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
# File 'lib/mingle.rb', line 2545

def cast_atomic_value( val, typ, path )
    
    case typ
    when TYPE_INT32, TYPE_INT64, TYPE_UINT32, TYPE_UINT64, 
         TYPE_FLOAT32, TYPE_FLOAT64 
        cast_num( val, typ, path )
    when TYPE_TIMESTAMP then cast_timestamp( val, typ, path )
    when TYPE_STRING then cast_string( val, typ, path )
    else raise "Can't cast to #{typ}"
    end
end

.cast_num(val, typ, path) ⇒ Object



2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
# File 'lib/mingle.rb', line 2505

def cast_num( val, typ, path )
    
    cls, meth = 
        case typ
        when TYPE_INT32 then [ MingleInt32, :to_i ]
        when TYPE_INT64 then [ MingleInt64, :to_i ]
        when TYPE_UINT32 then [ MingleUint32, :to_i ]
        when TYPE_UINT64 then [ MingleUint64, :to_i ]
        when TYPE_FLOAT32 then [ MingleFloat32, :to_f ]
        when TYPE_FLOAT64 then [ MingleFloat64, :to_f ]
        else raise "Bad num type: #{typ}"
        end
    
    case val
    when cls then val
    when MingleString then cls.new( val.to_s.send( meth ) )
    when MingleNumber then cls.new( val.num.send( meth ) )
    else fail_cast( val, typ, path )
    end
end

.cast_string(val, typ, path) ⇒ Object



2535
2536
2537
2538
2539
2540
2541
2542
2543
# File 'lib/mingle.rb', line 2535

def cast_string( val, typ, path )
    
    case val
    when MingleString then val
    when MingleTimestamp then val.rfc3339
    when MingleNumber, MingleBoolean then MingleString.new( val.to_s )
    else fail_cast( val, typ, path )
    end
end

.cast_timestamp(val, typ, path) ⇒ Object



2526
2527
2528
2529
2530
2531
2532
2533
# File 'lib/mingle.rb', line 2526

def cast_timestamp( val, typ, path )
    
    case val
    when MingleTimestamp then val
    when MingleString then MingleTimestamp.rfc3339( val )
    else fail_cast( val, typ, path )
    end
end

.cast_value(val, typ, path) ⇒ Object



2557
2558
2559
2560
2561
2562
2563
# File 'lib/mingle.rb', line 2557

def cast_value( val, typ, path )

    case typ
    when AtomicTypeReference then cast_atomic_value( val, typ, path )
    else raise "Unimplemented"
    end
end

.fail_cast(val, typ, path) ⇒ Object

Raises:



2496
2497
2498
2499
2500
2501
2502
2503
# File 'lib/mingle.rb', line 2496

def fail_cast( val, typ, path )
    
    raise TypeCastError.new(
        :actual => Mingle.type_of( val ),
        :expected => typ,
        :path => path
    )
end