Class: Cauldron::ArrayCollectTemplate::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/cauldron/array_collect_template/default.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indexes) ⇒ Default

Returns a new instance of Default.



9
10
11
# File 'lib/cauldron/array_collect_template/default.rb', line 9

def initialize(indexes)
  @indexes = indexes
end

Instance Attribute Details

#indexesObject (readonly)

Returns the value of attribute indexes.



7
8
9
# File 'lib/cauldron/array_collect_template/default.rb', line 7

def indexes
  @indexes
end

Instance Method Details

#branch?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/cauldron/array_collect_template/default.rb', line 87

def branch?
  true
end

#clone_statementObject



79
80
81
82
83
84
85
# File 'lib/cauldron/array_collect_template/default.rb', line 79

def clone_statement
  # TODO Need to clone the sexp methods
  # o = DynamicOperator.new(@information, @sexp_methods)
  # o.instance_eval(Sorcerer.source(@sexp_methods, indent: true))
  # o
  self.class.new(@indexes.clone)
end

#context_realizable?(context) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
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
# File 'lib/cauldron/array_collect_template/default.rb', line 13

def context_realizable?(context)
  
  vars = context.keys.select {|x| x.match(/var\d/) }
  var_names = vars.collect(&:to_s)
  
  first_variable = 'var'+@indexes[0].to_s

  a = %Q{
  def function(#{first_variable})
    #{Sorcerer.source(to_sexp(Cauldron::Scope.new(var_names), []), indent: true)}
  end
  }       
  
  o = Object.new
  o.instance_eval(a)

  begin
    #o.function(*vars.collect {|x| context[x] })  
    o.function context[first_variable.to_sym]
  rescue NoMethodError => e
    return false
  rescue StandardError => e
    puts e
    return false
  end
  return true
   
end

#to_ruby(scope, operators) ⇒ Object



75
76
77
# File 'lib/cauldron/array_collect_template/default.rb', line 75

def to_ruby(scope, operators)
  Sorcerer.source to_sexp(scope, operators)
end

#to_sexp(scope, children) ⇒ Object



42
43
44
45
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
# File 'lib/cauldron/array_collect_template/default.rb', line 42

def to_sexp(scope, children)
  scope_var = scope.new_variable!
  first_variable = 'var'+@indexes[0].to_s
  [:method_add_block, 
    [:call, 
      [:vcall, 
        # [:@ident, scope[@indexes[0]] ]], 
        [:@ident, first_variable ]], 
        :".", 
        [:@ident, "collect"]
    ], 
    unless children.empty?
      [:brace_block, 
        [:block_var, 
          [:params, [[:@ident, scope_var]]]], 
          [
            :stmts_add, 
            [:stmts_new], 
            # TODO Shouild probably be passing the children through here
            children.first.content.to_sexp(scope, [])
          ]
      ]
    else
      [:brace_block, 
        [:block_var, 
          [:params, [[:@ident, scope_var]]], 
          [:stmts_add, [:stmts_new]]
        ]
      ]        
    end
  ]    
end