Module: Generator::Function

Defined in:
lib/rubysol/generator.rb

Class Method Summary collapse

Class Method Details

.params(method, inputs, *args_unsafe, **kwargs_unsafe) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubysol/generator.rb', line 46

def self.params( method, inputs, *args_unsafe, **kwargs_unsafe )
    m = method
    params = m.parameters
    ## puts "params for #{m} - #{m.owner}:"
    ## pp params
    
    ## e.g.
    ## [[:keyreq, :name], 
    ##  [:keyreq, :symbol], 
    ##  [:keyreq, :decimals], 
    ##  [:keyreq, :totalSupply]]
    ## get keys
    keys = params.map { |param| param[1] }
    ## puts "keys:"
    ## pp keys
 
    kwargs =  if !args_unsafe.empty? 
                 values = inputs.zip( args_unsafe ).map do |type, value|
                              typecheck( type, value )
                          end
                 ## puts "args:"
                 ## puts values.pretty_print_inspect

                 keys.zip( values ).map do |key,value|
                                         [key,value]
                 end.to_h
              elsif !kwargs_unsafe.empty?
                 types = keys.zip( inputs ).map do |key,type|
                                         [key,type]
                         end.to_h
                 ## puts "types:"
                 ## pp types
                 kwargs_unsafe.map do |key,value|
                    type = types[key]
                    raise ArgumentError, "unknown kwarg #{key}; sorry"   if type.nil?
                    [key, typecheck( type, value)]
                end.to_h
              else
                ## assume no args - e.g. construct - double check for empty input spec/def!!!
                if inputs.empty?
                  {}   
                else
                  raise ArgumentError, "Array (args) or Hash (kwargs) required for func call; sorry"
                end
              end

    unless kwargs.empty?          
      puts "#{kwargs.size} kwarg(s):"
      pp kwargs
    end
    
    kwargs
end

.typecheck(type, value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubysol/generator.rb', line 33

def self.typecheck( type, value )
    ##   check if value is already typed?
    if value.is_a?( Types::Typed )
       ## type check
       raise TypeError, "type #{type} expected; got #{value.pretty_print_inspect}"  unless type == value.type
       value
    else
        ## assume "literal" value
        type.new( value )
    end
end