Skip to content

Commit

Permalink
Add dsl parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Genki Sugawara committed May 17, 2015
1 parent e55b734 commit 53b6e39
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/barkdog.rb
Expand Up @@ -3,6 +3,9 @@
module Barkdog; end

require 'barkdog/dsl'
require 'barkdog/dsl/context'
require 'barkdog/dsl/context/monitor'
require 'barkdog/dsl/context/monitor/options'
require 'barkdog/dsl/converter'
require 'barkdog/exporter'
require 'barkdog/version'
2 changes: 1 addition & 1 deletion lib/barkdog/dsl.rb
Expand Up @@ -4,6 +4,6 @@ def self.convert(exported, options = {})
end

def self.parse(dsl, path, options = {})
#Barkdog::DSL::Context.eval(dsl, path, options).result
Barkdog::DSL::Context.eval(dsl, path, options).result
end
end
42 changes: 42 additions & 0 deletions lib/barkdog/dsl/context.rb
@@ -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
23 changes: 23 additions & 0 deletions lib/barkdog/dsl/context/monitor.rb
@@ -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
19 changes: 19 additions & 0 deletions lib/barkdog/dsl/context/monitor/options.rb
@@ -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
2 changes: 1 addition & 1 deletion lib/barkdog/exporter.rb
Expand Up @@ -27,7 +27,7 @@ def normalize(monitors)
monitor_by_name = {}

monitors.each do |m|
name = m['name']
name = m.delete('name')

if monitor_by_name[name]
raise "Duplicate monitor name exists: #{name}"
Expand Down

0 comments on commit 53b6e39

Please sign in to comment.