Skip to content

Commit

Permalink
Merge pull request #5 from winebarrel/support_template
Browse files Browse the repository at this point in the history
Support template
  • Loading branch information
marcy-terui authored Aug 22, 2016
2 parents 60e971f + f865fa1 commit 8b608be
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 37 deletions.
3 changes: 2 additions & 1 deletion dashdog.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "dogapi"
spec.add_dependency "dslh", "~> 0.3.8"
spec.add_dependency "dslh", "~> 0.3.9"
spec.add_dependency "thor", "~> 0.19.1"
spec.add_dependency "coderay"
spec.add_dependency "diffy"
spec.add_dependency "parallel"
spec.add_dependency "hashie"

spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down
1 change: 1 addition & 0 deletions lib/dashdog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "dashdog/converter"
require "dashdog/actions"
require "dashdog/cli"
require "dashdog/dsl_context"

module Dashdog
# Your code goes here...
Expand Down
36 changes: 2 additions & 34 deletions lib/dashdog/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,8 @@ def screenboards_to_dsl(screenboards)
end

def to_h(dsl_file)
@_dsl_file = dsl_file
instance_eval(File.read(dsl_file), dsl_file)
@boards
context = DSLContext.new
context.eval_dsl(dsl_file)
end

private

def require(file)
boardfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@_dsl_file), file))

if File.exist?(boardfile)
instance_eval(File.read(boardfile), boardfile)
elsif File.exist?(boardfile + '.rb')
instance_eval(File.read(boardfile + '.rb'), boardfile + '.rb')
else
Kernel.require(file)
end
end

def timeboard(value = nil, &block)
hash = Dslh.eval(
allow_empty_args: true,
&block)
hash['title'] = value
@boards['timeboards'] << hash
end

def screenboard(value = nil, &block)
hash = Dslh.eval(
allow_empty_args: true,
&block)
hash['board_title'] = value
@boards['screenboards'] << hash
end

end
end
78 changes: 78 additions & 0 deletions lib/dashdog/dsl_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'hashie'

module Dashdog
class DSLContext
def initialize
@boards = {'timeboards' => [], 'screenboards' => []}
@templates = {}
@context = Hashie::Mash.new()
end

def eval_dsl(dsl_file)
@_dsl_file = dsl_file
instance_eval(File.read(dsl_file), dsl_file)
@boards
end

private

def template(name, &block)
@templates[name.to_s] = block
end

def context
@context
end

def require(file)
boardfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@_dsl_file), file))

if File.exist?(boardfile)
instance_eval(File.read(boardfile), boardfile)
elsif File.exist?(boardfile + '.rb')
instance_eval(File.read(boardfile + '.rb'), boardfile + '.rb')
else
Kernel.require(file)
end
end

def timeboard(value = nil, &block)
hash = dslh_eval(block)
hash['title'] = value
@boards['timeboards'] << hash
end

def screenboard(value = nil, &block)
hash = dslh_eval(block)
hash['board_title'] = value
@boards['screenboards'] << hash
end

def dslh_eval(block)
scope_hook = proc do |scope|
scope.instance_eval(<<-'EOS')
def include_template(template_name, context = {})
tmplt = @templates[template_name.to_s]
unless tmplt
raise "Template '#{template_name}' is not defined"
end
context_orig = @context
@context = @context.merge(context)
instance_eval(&tmplt)
@context = context_orig
end
def context
@context
end
EOS
end

scope_vars = {templates: @templates, context: @context}

Dslh.eval(allow_empty_args: true, scope_hook: scope_hook, scope_vars: scope_vars, &block)
end
end
end
4 changes: 2 additions & 2 deletions lib/dashdog/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Utils

def self.diff(hash1, hash2)
Diffy::Diff.new(
JSON.pretty_generate(hash1),
JSON.pretty_generate(hash2),
JSON.pretty_generate(hash1) + "\n",
JSON.pretty_generate(hash2) + "\n",
:diff => '-u'
).to_s(:color)
end
Expand Down

0 comments on commit 8b608be

Please # to comment.