module Patriot::Command::Parser

namespace for DSL parser

Public Instance Methods

batch_macro(name, &blk) click to toggle source

define macro. this method is used in macro files @param name name of the macro @yieldreturn the result of the macro evaluation

# File lib/patriot/command/parser.rb, line 43
def batch_macro(name, &blk)
  raise "#{name} is invalid macro name (duplicate or defined method)" if respond_to?(name.to_sym)
  eigenclass = class << self
    self
  end
  @macros[name] = blk
  eigenclass.send(:define_method, name, &blk)
end
import_erb_config(file, _vars) click to toggle source

import an ERB template @param file [String] path to the ERB template @param _vars [Hash] a hash from variable name to its value used in the ERB

# File lib/patriot/command/parser.rb, line 55
def import_erb_config(file, _vars)
  file = File.expand_path(file,$home)
  # set variables
  erb = _vars.map{|k,v| "<%#{k} = #{v.inspect}%>"}.join("\n")
  erb << "\n"

  # read the ERB
  exp=""
  open(file) do |f|
    erb << f.read
    exp = ERB.new(erb).result(binding)
  end
  begin
    self.instance_eval(exp)
  rescue => e
    @logger.error("failed to parse #{file}")
    @logger.error(erb)
    raise e
  end
end
load_macro(macro_file) click to toggle source

load macro to be able to be used in the DSL @param macro_file [String] path to a macro file

# File lib/patriot/command/parser.rb, line 31
def load_macro(macro_file)
  macro_file = File.expand_path(macro_file,$home)
  @logger.info "loading macro file #{macro_file}" 
  open(macro_file){|f|
    self.instance_eval(f.read)
  }
end
new_command(cls, &blk) click to toggle source

create a command @param cls [Class] class of the command @yield block to set attributes to the command

# File lib/patriot/command/parser.rb, line 11
def new_command(cls, &blk)
  raise "configuration is not set" if @config.nil?
  command = cls.new(@config)
  command.target_datetime = @target_datetime
  @macros.each{|n,b| command.batch_macro(n,&b)} unless @macros.nil?
  command.instance_eval(&blk) if block_given?
  return command
end
parse(datetime, blk) click to toggle source

parse DSL by processing the DSL description as block @param datetime [Time] the datetime for which the job works @return a list of command defined in the DSL description

# File lib/patriot/command/parser.rb, line 23
def parse(datetime, blk)
  self.target_datetime = datetime
  self.instance_eval(blk)
  return self.build({})
end