Class: QED::Scope

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

Overview

Scope is the context in which QED documents are run.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(applique) ⇒ Scope

Returns a new instance of Scope.



21
22
23
24
25
26
27
28
# File 'lib/qed/scope.rb', line 21

def initialize(applique)
  super()
  @applique = applique
  extend self
  extend applique # TODO: extend or include applique or none ?
  #extend DSLi
  create_clean_binding_method
end

Class Method Details

.const_missing(name) ⇒ Object



16
17
18
# File 'lib/qed/scope.rb', line 16

def self.const_missing(name)
  @applique.const_get(name)
end

.new(applique) ⇒ Object



10
11
12
13
# File 'lib/qed/scope.rb', line 10

def self.new(applique)
  @applique = applique
  super(applique)
end

Instance Method Details

#After(type = :code, &procedure) ⇒ Object



68
69
70
# File 'lib/qed/scope.rb', line 68

def After(type=:code, &procedure)
  @applique.After(type, &procedure)
end

#Before(type = :code, &procedure) ⇒ Object



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

def Before(type=:code, &procedure)
  @applique.Before(type, &procedure)
end

#create_clean_binding_methodObject

This turned out to be the key to proper scoping.



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

def create_clean_binding_method
  define_method(:__binding__) do
    @__binding__ ||= binding
  end
end

#Data(file, &content) ⇒ Object

Read/Write a static data fixture. – TODO: Perhaps #Data would be best as some sort of Kernel extension.

TODO: Look for files relative to script first? ++



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/qed/scope.rb', line 92

def Data(file, &content)
  raise if File.directory?(file)
  if content
    FileUtils.mkdir_p(File.dirname(fname))
    case File.extname(file)
    when '.yml', '.yaml'
      File.open(file, 'w'){ |f| f << content.call.to_yaml }
    else
      File.open(file, 'w'){ |f| f << content.call }
    end
  else
    #raise LoadError, "no such fixture file -- #{fname}" unless File.exist?(fname)
    case File.extname(file)
    when '.yml', '.yaml'
      YAML.load(File.new(file))
    else
      File.read(file)
    end
  end
end

#eval(code) ⇒ Object



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

def eval(code)
  super(code, __binding__)
end

#Table(file = nil, &blk) ⇒ Object

Table-based steps. – TODO: Look for files relative to script first? ++



76
77
78
79
80
81
82
83
84
# File 'lib/qed/scope.rb', line 76

def Table(file=nil, &blk)
  file = file || @_tables.last
  tbl = YAML.load(File.new(file))
  tbl.each do |set|
    blk.call(*set)
  end
  @__tables__ ||= []
  @__tables__ << file
end

#When(*patterns, &procedure) ⇒ Object



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

def When(*patterns, &procedure)
  @applique.When(*patterns, &procedure)
end