Class: Bandicoot::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/bandicoot/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bandicoot/context.rb', line 5

def initialize(opts={})
  @parent = opts[:parent]
  @key = opts[:key]
  
  # if this is the top level context
  if !parent
    @key ||= "__main__"
    @continuing = !!opts[:continue]
    if continuing?
      @save_file = SaveFile.continue(opts[:continue])
      @save_points = @save_file.save_points[@key] || SavePointHash.new
    else
      @save_file = SaveFile.create(opts[:save_file] || default_save_filename)
      @save_points = SavePointHash.new
    end
  else
    @save_points = parent.save_points[@key] || SavePointHash.new
  end
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/bandicoot/context.rb', line 3

def parent
  @parent
end

#save_pointsObject (readonly)

Returns the value of attribute save_points.



3
4
5
# File 'lib/bandicoot/context.rb', line 3

def save_points
  @save_points
end

Instance Method Details

#continuing?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/bandicoot/context.rb', line 33

def continuing?
  @continuing ||= (parent && parent.continuing?)
end

#finish!(retval = nil) ⇒ Object



50
51
52
53
# File 'lib/bandicoot/context.rb', line 50

def finish!(retval=nil)
  save_file.write(key, retval) if save_file
  retval
end

#keyObject



25
26
27
# File 'lib/bandicoot/context.rb', line 25

def key
  @m_key ||= ((parent && parent.key) || []) + [@key]
end

#run(blk) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/bandicoot/context.rb', line 42

def run(blk)
  if continuing? && save_point.completed?
    save_point.ret_val
  else
    finish! blk.call
  end
end

#save_fileObject



29
30
31
# File 'lib/bandicoot/context.rb', line 29

def save_file
  @save_file ||= (parent && parent.save_file)
end

#save_pointObject

makes things a little prettier



38
39
40
# File 'lib/bandicoot/context.rb', line 38

def save_point
  @save_points
end