-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsl.old
More file actions
79 lines (71 loc) · 2.45 KB
/
dsl.old
File metadata and controls
79 lines (71 loc) · 2.45 KB
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
72
73
74
75
76
77
78
79
require "erb"
module MightyMaps
module DSL
def self.included(base)
class << base
alias :original_name :name
end
base.extend(ClassMethods)
end
module ClassMethods
def block(options = {}, &block_param)
class_variable_set(:@@blocks, []) unless class_variable_defined?(:@@blocks)
blocks = class_variable_get(:@@blocks)
new_block = MightyMaps::Types::Block.new(options)
new_block.instance_exec(&block_param)
blocks << new_block
class_variable_set(:@@blocks, blocks) # should not be needed because we modify the array in place
self
end
def blocks
class_variable_get(:@@blocks)
end
# there is a ruby method Class.name, which is aliased to :original_name
def name(*args)
case args.length
when 0 then class_variable_get(:@@name)
when 1 then class_variable_set(:@@name, args.first.to_s)
else raise ArgumentError
end
end
def to_ruby(options = {})
template = <<-eor
require "mighty_maps"
__nl__
class #{original_name}
include MightyMaps::DSL
__nl__
name "#{name}"
<% blocks.each do |block| %>
__nl__
<% if options[:blocks] && options[:blocks].to_sym == :verbose %>
block do
<% if block.name %>name "<%= block.name %>"<% end %>
<% if block.description %>description "<%= block.description %>"<% end %>
__nl__
<% else %>
block name: <%= block.name %>, description: <%= block.description %> do
<% end %>
<% block.seats.each do |seat| %>
seat x: <%= seat.x.to_s %>, y: <%= seat.y.to_s %>, row: <%= seat.row ? '"' + seat.row.to_s + '"' : "nil" %>, number: <%= seat.number ? '"' + seat.number.to_s + '"' : "nil" %>
<% end %>
end
<% end %>
end
eor
# adapted from ActiveSupport's strip_heredoc
indent = template.scan(/^[ \t]*(?=\S)/).min.size
template = template.gsub(/^[ \t]{#{indent}}/, "")
# the following is just for aestetic reasons
ERB.new(template).result(binding)
.split("\n")
.map do |line|
line[/\A\s+\Z/] ? "" : line # make empty lines uniform ""
end
.join("\n")
.gsub(/\n+/, "\n")
.gsub("__nl__\n", "\n")
end
end
end
end