Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Genki Sugawara
committed
May 17, 2015
1 parent
e55b734
commit 53b6e39
Showing
6 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Barkdog::DSL::Context | ||
def self.eval(dsl, path, options = {}) | ||
self.new(path, options) { | ||
eval(dsl, binding, path) | ||
} | ||
end | ||
|
||
attr_reader :result | ||
|
||
def initialize(path, options = {}, &block) | ||
@path = path | ||
@options = options | ||
@result = {} | ||
instance_eval(&block) | ||
end | ||
|
||
private | ||
|
||
def require(file) | ||
barkfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file)) | ||
|
||
if File.exist?(barkfile) | ||
instance_eval(File.read(barkfile), barkfile) | ||
elsif File.exist?(barkfile + '.rb') | ||
instance_eval(File.read(barkfile + '.rb'), barkfile + '.rb') | ||
else | ||
Kernel.require(file) | ||
end | ||
end | ||
|
||
def monitor(name, fixed_options = {}, &block) | ||
name = name.to_s | ||
|
||
if @result[name] | ||
raise "Monitor `#{name}` is already defined" | ||
end | ||
|
||
fixed_options = Hash[fixed_options.map {|k, v| [k.to_s, v] }] | ||
attrs = Barkdog::DSL::Context::Monitor.new(name, &block).result | ||
@result[name] = fixed_options.merge(attrs) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Barkdog::DSL::Context::Monitor | ||
def initialize(name, &block) | ||
@monitor_name = name | ||
@result = {} | ||
instance_eval(&block) | ||
end | ||
|
||
attr_reader :result | ||
|
||
private | ||
|
||
def query(value) | ||
@result['query'] = value.to_s | ||
end | ||
|
||
def message(value) | ||
@result['message'] = value.to_s | ||
end | ||
|
||
def options(&block) | ||
@result['options'] = Barkdog::DSL::Context::Monitor::Options.new(&block).result | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Barkdog::DSL::Context::Monitor::Options | ||
def initialize(&block) | ||
@result = {} | ||
instance_eval(&block) | ||
end | ||
|
||
attr_reader :result | ||
|
||
private | ||
|
||
def method_missing(method_name, *args) | ||
if args.length == 1 | ||
value = args.first | ||
@result[method_name.to_s] = value | ||
else | ||
super | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters