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
-
#batch_macro(name, &blk) ⇒ Object
define macro.
-
#import_erb_config(file, _vars) ⇒ Object
import an ERB template.
-
#load_macro(macro_file) ⇒ Object
load macro to be able to be used in the DSL.
-
#new_command(cls) { ... } ⇒ Object
create a command.
-
#parse(datetime, blk) ⇒ Object
parse DSL by processing the DSL description as block.
Instance Method Details
#batch_macro(name, &blk) ⇒ Object
define macro. this method is used in macro files
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
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.(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
31 32 33 34 35 36 37 |
# File 'lib/patriot/command/parser.rb', line 31 def load_macro(macro_file) macro_file = File.(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
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
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 |