Class: QED::Script

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

Overview

Script

When run current working directory is changed to that of the demonstration script’s. So any relative file references within a demo must take that into account.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, scope = nil) ⇒ Script

New Script



31
32
33
34
35
# File 'lib/qed/script.rb', line 31

def initialize(file, scope=nil)
  @file     = file
  @scope    = scope || Scope.new
  apply_environment
end

Instance Attribute Details

#dirObject (readonly)

Expanded dirname of file.



25
26
27
# File 'lib/qed/script.rb', line 25

def dir
  @dir
end

#fileObject (readonly)

Demonstration file.



22
23
24
# File 'lib/qed/script.rb', line 22

def file
  @file
end

#scopeObject (readonly)

Returns the value of attribute scope.



28
29
30
# File 'lib/qed/script.rb', line 28

def scope
  @scope
end

Instance Method Details

#adviceObject



43
44
45
# File 'lib/qed/script.rb', line 43

def advice
  @scope.__advice__
end

#apply_environmentObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/qed/script.rb', line 113

def apply_environment
  environment.each do |file|
    case File.extname(file)
    when '.rb'
      eval(File.read(file), scope.__binding__, file)
    else
      Script.new(file, scope).run
    end
  end
end

#bindingObject

One binding per script.



38
39
40
# File 'lib/qed/script.rb', line 38

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

#documentObject

Nokogiri HTML document.



58
59
60
# File 'lib/qed/script.rb', line 58

def document
  @document ||= Nokogiri::HTML(to_html)
end

#environmentObject



107
108
109
110
# File 'lib/qed/script.rb', line 107

def environment
  glob = File.join(dir, '{environment,common,shared}', '*')
  Dir[glob]
end

#htmlObject

Open, convert to HTML and cache.



83
84
85
# File 'lib/qed/script.rb', line 83

def html
  @html ||= to_html
end

#nameObject

File basename less extension.



53
54
55
# File 'lib/qed/script.rb', line 53

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

#rootObject

Root node of the html document.



63
64
65
# File 'lib/qed/script.rb', line 63

def root
  document.root
end

#run(*observers) ⇒ Object



101
102
103
104
# File 'lib/qed/script.rb', line 101

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

#to_htmlObject

Open file and translate template into HTML text.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/qed/script.rb', line 68

def to_html
  #case file
  #when /^http/
  #  ext  = File.extname(file).sub('.','')
  #  Tilt[ext].new{ source }
  #else
  #end
  if File.extname(file) == '.html'
    File.read(file)
  else
    Tilt.new(file).render
  end
end