Module: Patriot::Command::Parser

Included in:
Base, Worker::Servlet::JobAPIServlet
Defined in:
lib/patriot/command/parser.rb

Overview

namespace for DSL parser

Instance Method Summary collapse

Instance Method Details

#batch_macro(name, &blk) ⇒ Object

define macro. this method is used in macro files

Parameters:

  • name

    name of the macro

Yield Returns:

  • the result of the macro evaluation



43
44
45
46
47
48
49
50
# 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) ⇒ Object

import an ERB template

Parameters:

  • file (String)

    path to the ERB template

  • _vars (Hash)

    a hash from variable name to its value used in the ERB



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

load macro to be able to be used in the DSL

Parameters:

  • macro_file (String)

    path to a macro file



31
32
33
34
35
36
37
# 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) { ... } ⇒ Object

create a command

Parameters:

  • cls (Class)

    class of the command

Yields:

  • block to set attributes to the command



11
12
13
14
15
16
17
18
# 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) ⇒ Object

parse DSL by processing the DSL description as block

Parameters:

  • datetime (Time)

    the datetime for which the job works

Returns:

  • a list of command defined in the DSL description



23
24
25
26
27
# File 'lib/patriot/command/parser.rb', line 23

def parse(datetime, blk)
  self.target_datetime = datetime
  self.instance_eval(blk)
  return self.build({})
end