module Mingle::CastImpl

Public Instance Methods

cast_atomic_value( val, typ, path ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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