Class: QED::Demo

Inherits:
Object show all
Defined in:
lib/qed/demo.rb

Overview

The Demo class ecapsulates a demonstration document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Demo

New Script



27
28
29
30
31
32
33
34
35
36
# File 'lib/qed/demo.rb', line 27

def initialize(file, options={})
  @file    = file
  @mode    = options[:mode]
  @cwd     = options[:at] || fallback_cwd

  @scope   = options[:scope] || Scope.new(applique, cwd, file)

  @binding = @scope.__binding__
  #apply_environment
end

Instance Attribute Details

#cwdObject (readonly)

Working directory.



21
22
23
# File 'lib/qed/demo.rb', line 21

def cwd
  @cwd
end

#fileObject (readonly)

Demonstrandum file.



15
16
17
# File 'lib/qed/demo.rb', line 15

def file
  @file
end

#modeObject (readonly)

Parser mode.



18
19
20
# File 'lib/qed/demo.rb', line 18

def mode
  @mode
end

#scopeObject (readonly)

Scope to run demonstration within. (Known as a “World” in Cucumber.)



24
25
26
# File 'lib/qed/demo.rb', line 24

def scope
  @scope
end

Instance Method Details

#appliqueObject

Returns a cached Array of Applique modules.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/qed/demo.rb', line 60

def applique
  @applique ||= (
    list = [Applique.new]
    applique_locations.each do |location|
      Dir[location + '/**/*.rb'].each do |file|
        list << Applique.new(file)
      end
    end
    list
  )
end

#applique_locationsObject

Returns a list of applique directories to be used by this demonstrastion.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/qed/demo.rb', line 74

def applique_locations
  @applique_locations ||= (
    locations = []
    Dir.ascend(File.dirname(file)) do |path|
      break if path == Dir.pwd
      dir = File.join(path, 'applique')
      if File.directory?(dir)
        locations << dir
      end
    end
    locations
  )
end

#bindingObject

One binding per demo.



39
40
41
# File 'lib/qed/demo.rb', line 39

def binding
  @binding #||= @scope.__binding__
end

#directoryObject

Expanded dirname of file.



44
45
46
# File 'lib/qed/demo.rb', line 44

def directory
  @directory ||= File.expand_path(File.dirname(file))
end

#evaluate(code, line) ⇒ Object



54
55
56
57
# File 'lib/qed/demo.rb', line 54

def evaluate(code, line)
  #eval(code, @binding, @file, line)
  @scope.eval(code, @file, line)
end

#fallback_cwdObject

This shouldn’t be needed, but is here as a precaution.



127
128
129
# File 'lib/qed/demo.rb', line 127

def fallback_cwd
  @dir ||= File.join(Dir.tmpdir, 'qed', File.filename(Dir.pwd), Time.new.strftime("%Y%m%d%H%M%S"))
end

#nameObject

File basename less extension.



49
50
51
# File 'lib/qed/demo.rb', line 49

def name
  @name ||= File.basename(file).chomp(File.extname(file))
end

#parserParser

Get a new Parser instance for this demo.

Returns:

  • (Parser)

    parser for this demo



103
104
105
# File 'lib/qed/demo.rb', line 103

def parser
  Parser.new(file, :mode=>mode)
end

#run(*observers) ⇒ Object



108
109
110
111
# File 'lib/qed/demo.rb', line 108

def run(*observers)
  evaluator = Evaluator.new(self, *observers)
  evaluator.run
end

#stepsArray Also known as: parse

Demo steps, cached from parsing.

Returns:

  • (Array)

    parsed steps



91
92
93
# File 'lib/qed/demo.rb', line 91

def steps
  @steps ||= parser.parse
end