-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsamples.rb
55 lines (42 loc) · 796 Bytes
/
samples.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
51
52
53
54
55
require 'pathname'
SAMPLE_FILE = '.sample'
class Context
def initialize(data)
@data = data
yield binding
end
def method_missing(m, *args)
if @data.has_key? m
raise "Key #{m} already defined"
end
@data[m] = args
end
end
# Read sample file in current directory
def read_sample_file
source = IO.read(SAMPLE_FILE)
data = { path: Pathname.pwd }
Context.new(data) do |bindings|
eval source, bindings
end
data
end
# Look for samples
def scan
if File.exists? SAMPLE_FILE
yield read_sample_file
else
Dir['*'].select do |entry|
File.directory? entry
end.each do |directory|
Dir.chdir directory do
scan do |result|
yield result
end
end
end
end
end
scan do |data|
p data
end