Class: Collectd::DSL
- Inherits:
-
Object
show all
- Defined in:
- lib/collectd-dsl.rb
Constant Summary
collapse
- VERSION =
"0.3.5"
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(&block) ⇒ DSL
Returns a new instance of DSL.
5
6
7
8
9
|
# File 'lib/collectd-dsl.rb', line 5
def initialize(&block)
@directives = []
@indent = 0
instance_eval(&block) if block_given?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/collectd-dsl.rb', line 27
def method_missing method_name, *args
mapped_args = args.map do |a|
if a.class == String
"\"" + a.to_s + "\""
else
a.to_s
end
end.join(" ")
mapped_args = (" " + mapped_args) unless mapped_args.empty?
camel_method_name = snake_to_camel method_name.to_s
if block_given?
@directives << indent("<#{camel_method_name}#{mapped_args}>")
@indent += 1
yield @indent -= 1
@directives << indent("</#{camel_method_name}>")
else
@directives << indent("#{camel_method_name}#{mapped_args}")
end
end
|
Class Method Details
.parse(&block) ⇒ Object
15
16
17
|
# File 'lib/collectd-dsl.rb', line 15
def self.parse(&block)
DSL.new(&block).dump if block_given?
end
|
Instance Method Details
#dump ⇒ Object
11
12
13
|
# File 'lib/collectd-dsl.rb', line 11
def dump
@directives.join("\n") + "\n"
end
|
#indent(str) ⇒ Object
19
20
21
|
# File 'lib/collectd-dsl.rb', line 19
def indent str
("\t" * @indent) + str
end
|
#snake_to_camel(method_name) ⇒ Object
23
24
25
|
# File 'lib/collectd-dsl.rb', line 23
def snake_to_camel method_name
method_name.split("_").map{|w| w.capitalize }.join("")
end
|