Class: Verstehen::List

Inherits:
Object
  • Object
show all
Defined in:
lib/verstehen/list.rb

Instance Method Summary collapse

Constructor Details

#initialize(&expression) ⇒ List

Returns a new instance of List.



3
4
5
6
7
8
# File 'lib/verstehen/list.rb', line 3

def initialize &expression
  @expression = expression
  @result = []
  @dimensions = []
  @context = Context.new
end

Instance Method Details

#comprehendObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/verstehen/list.rb', line 26

def comprehend
  generated = ""
  @dimensions.each_with_index do |dimension, i|
    block_params = dimension.for.collect {|sym| sym.to_s}.join(", ")
    generated << "for #{block_params} in @context.instance_eval(&@dimensions[#{i}].in)\n"
    generated << dimension.for.map {|sym| "@context.#{sym} = #{sym}\n" }.join
    if dimension == @dimensions.last
      if @dimensions[i].if 
        generated << "if @context.instance_eval(&@dimensions[#{i}].if)\n"
      end
      generated << "@result << @context.instance_eval(&@expression)\n"
      if @dimensions[i].if 
        generated << "end\n"
      end
    end
  end
  @dimensions.each_with_index do |dimension, i|
    generated << "end\n"
  end

  #puts generated
  eval generated
  
  @result
end

#for(*symbols) ⇒ Object



10
11
12
13
14
# File 'lib/verstehen/list.rb', line 10

def for *symbols
  @dimensions << Dimension.new(symbols)
  symbols.each { |sym| Context.send(:attr_accessor, sym) }
  self
end

#if(&condition) ⇒ Object



21
22
23
24
# File 'lib/verstehen/list.rb', line 21

def if &condition
  @dimensions.last.if = condition
  self
end

#in(&range) ⇒ Object



16
17
18
19
# File 'lib/verstehen/list.rb', line 16

def in &range
  @dimensions.last.in = range
  self
end