-
Notifications
You must be signed in to change notification settings - Fork 0
/
dslalternative.rb
executable file
·71 lines (57 loc) · 1.21 KB
/
dslalternative.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
require 'pp'
class GenericDslSupport
(public_instance_methods(true) - ['__id__', '__send__', 'instance_eval']).each do |meth|
str = <<END
def #{meth}(*args, &block)
method_missing(:#{meth}, *args, &block)
end
END
class_eval(str)
end
def self.dsl(&block)
GenericDslSupport.new.instance_eval(&block)
end
def initialize
@ret = []
end
def method_missing(name, *args, &block)
raise "cannot have block and arguments" unless args.empty? or block == nil
return name if args.empty? and block == nil
return _head_with_block(name, block) if block
_list_with_args(name, args)
end
def _head_with_block(name, block)
_tail_content name, GenericDslSupport.dsl(&block)
end
def _list_with_args(name, args)
_tail_content name, args
end
def _tail_content(name, content)
@ret << [name].push(*content)
end
end
def dsl(&blk)
GenericDslSupport.dsl &blk
end
def fat n
return n if n <= 1
return n * fat(n - 1)
end
x = dsl {
computer do
processor {
cores 2
type 386
}
disk {
size fat(50)
}
disk {
size 75
speed 7200, max
interface sata
}
end
}
pp x[0]