module Mingle::MingleModels

Constants

NUM_TYPES

Public Instance Methods

as_mingle_boolean( val ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# File lib/mingle.rb, line 2398
def as_mingle_list( val )
    impl_as_typed_value( val, MingleList )
end
as_mingle_number( val, num_typ ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# File lib/mingle.rb, line 2394
def as_mingle_struct( val )
    impl_as_typed_value( val, MingleStruct )
end
as_mingle_symbol_map( val ) click to toggle source
# File lib/mingle.rb, line 2402
def as_mingle_symbol_map( val )
    impl_as_typed_value( val, MingleSymbolMap )
end
as_mingle_timestamp( val ) click to toggle source
# File lib/mingle.rb, line 2371
def as_mingle_timestamp( 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 ) click to toggle source
# 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 as_mingle_timestamp( 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source

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.

# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# File lib/mingle.rb, line 2473
def raise_as_ruby_error( me )
    raise as_ruby_error( me )
end