Class: Rust::RustDatatype

Inherits:
Object show all
Defined in:
lib/rust/core/types/datatype.rb

Overview

Represents a data-type that can be loaded from and written to R.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pull_priorityObject

Returns the priority of this type when a #pull_variable operation is performed. Higher priority means that the type is to be preferred over other candidate types.



54
55
56
# File 'lib/rust/core/types/datatype.rb', line 54

def self.pull_priority
    0
end

.pull_variable(variable, forced_interpreter = nil) ⇒ Object

Retrieves the given variable from R and transforms it into the appropriate Ruby counterpart. To infer the type, it uses the class method #can_pull? of all the RustDatatype classes to check the types that are compatible with the given R variable (type and class). If more than a candidate is available, the one with maximum #pull_priority is chosen.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rust/core/types/datatype.rb', line 16

def self.pull_variable(variable, forced_interpreter = nil)
    r_type = Rust._pull("as.character(typeof(#{variable}))")
    r_class = Rust._pull("as.character(class(#{variable}))")
    
    if forced_interpreter
        raise ArgumentError, "Expected null or class as forced_interpreter" if forced_interpreter && !forced_interpreter.is_a?(Class)
        raise ArgumentError, "Class #{forced_interpreter} can not handle type #{r_type}, class #{r_class}" unless forced_interpreter.can_pull?(r_type, r_class)
        
        return forced_interpreter.pull_variable(variable, r_type, r_class)
    end
    
    candidates = []
    ObjectSpace.each_object(Class) do |type|
        if type < RustDatatype
            if type.can_pull?(r_type, r_class)
                candidates << type
            end
        end
    end
    
    if candidates.size > 0
        type = candidates.max_by { |c| c.pull_priority }
        
        puts "Using #{type} to pull #{variable}" if Rust.debug?
        return type.pull_variable(variable, r_type, r_class)
    else
        if Rust._pull("length(#{variable})") == 0
            return []
        else
            return Rust._pull(variable)
        end
    end
end

Instance Method Details

#load_in_r_as(variable_name) ⇒ Object

Writes the current object in R as variable_name.



61
62
63
# File 'lib/rust/core/types/datatype.rb', line 61

def load_in_r_as(variable_name)
    raise "Loading #{self.class} in R was not implemented"
end

#r_hashObject

Returns the hash of the current object.



97
98
99
# File 'lib/rust/core/types/datatype.rb', line 97

def r_hash
    self.hash.to_s
end

#r_mirrorObject

EXPERIMENTAL: Do not use



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rust/core/types/datatype.rb', line 80

def r_mirror
    varname = self.mirrored_R_variable_name
                
    if !Rust._pull("exists(\"#{varname}\")") || Rust._pull("#{varname}.hash") != self.r_hash
        puts "Loading #{varname}" if Rust.debug?
        Rust[varname] = self
        Rust["#{varname}.hash"] = self.r_hash
    else
        puts "Using cached value for #{varname}" if Rust.debug?
    end
    
    return varname
end

#r_mirror_to(other_variable) ⇒ Object

EXPERIMENTAL: Do not use



68
69
70
71
72
73
74
75
# File 'lib/rust/core/types/datatype.rb', line 68

def r_mirror_to(other_variable)
    varname = self.mirrored_R_variable_name
    
    Rust._eval("#{varname} = #{other_variable}")
    Rust["#{varname}.hash"] = self.r_hash
                
    return varname
end