Class: MultiEval

Inherits:
Object show all
Includes:
FromHash
Defined in:
lib/formula_eval/multi_eval.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &b) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/formula_eval/multi_eval.rb', line 28

def method_missing(sym,*args,&b)
  objs.each do |obj|
    if obj.smart_respond_to?(sym,args)
      res = obj.send(sym,*args,&b)
      #return MultiEval.new(:objs => [res]+without_obj(obj)) #if res
      return res if res
    end
  end
  raise 'none respond'
  objs.first.send(sym,*args,&b)
end

Instance Attribute Details

#objsObject

Returns the value of attribute objs.



2
3
4
# File 'lib/formula_eval/multi_eval.rb', line 2

def objs
  @objs
end

Class Method Details

.fix_str(str) ⇒ Object



61
62
63
# File 'lib/formula_eval/multi_eval.rb', line 61

def self.fix_str(str)
  str.gsub(/\.(\d+)\./) { "._arrayindex_#{$1}." }.gsub(/\.(\d+)$/) { "._arrayindex_#{$1}" }
end

.get_nested(obj, method_str) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/formula_eval/multi_eval.rb', line 51

def self.get_nested(obj,method_str)
  method_str = fix_str(method_str)
  other = obj.to_wrapped.instance_eval(method_str)
  mylog 'get_nested', :obj => obj, :method_str => method_str, :other => other
  arr = [other,obj]
  new(:objs => arr)
end

Instance Method Details

#*(x) ⇒ Object



48
49
50
# File 'lib/formula_eval/multi_eval.rb', line 48

def *(x)
  objs.first * x
end

#+(x) ⇒ Object



45
46
47
# File 'lib/formula_eval/multi_eval.rb', line 45

def +(x)
  objs.first + x
end

#coerce(x) ⇒ Object



42
43
44
# File 'lib/formula_eval/multi_eval.rb', line 42

def coerce(x)
  objs.first.coerce(x)
end

#methodf_missing(sym, *args, &b) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/formula_eval/multi_eval.rb', line 4

def methodf_missing(sym,*args,&b)
  # puts "multi_eval mm #{sym}"
  objs.each do |obj|
    begin
      res = obj.send(sym,*args,&b)
      if res
        #other = objs - [obj]
        # puts "success sending #{sym} to #{obj}"
        return MultiEval.new(:objs => [res])
      end
    rescue => exp
      # puts "error sending #{sym} to #{obj} #{exp.message}"
    end
  end
  objs.first.send(sym,*args,&b)
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/formula_eval/multi_eval.rb', line 39

def respond_to?(sym)
  objs.any? { |x| x.respond_to?(sym) }
end

#to_unwrappedObject



58
59
60
# File 'lib/formula_eval/multi_eval.rb', line 58

def to_unwrapped
  objs.first.to_unwrapped
end

#without_obj(obj) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/formula_eval/multi_eval.rb', line 20

def without_obj(obj)
  res = objs.reject { |x| x == obj }
  raise "didn't find" unless objs.size == res.size+1
  res
rescue => exp
  puts "#{objs.inspect} #{obj.inspect}"
  raise exp
end