class AdHocTemplate::CommandLineInterface
Constants
- RE_TO_FORMAT
- TAG_RE_TO_TYPE
Attributes
data_format[RW]
output_empty_entry[W]
output_filename[RW]
record_data[RW]
tag_type[RW]
template_data[RW]
Public Class Methods
new()
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 35 def initialize @tag_formatter = AdHocTemplate::DefaultTagFormatter.new @output_filename = nil @tag_type = :default @data_format = nil @force_update = false @init_local_settings = false end
Public Instance Methods
execute()
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 116 def execute parse_command_line_options exit if @init_local_settings return update_output_files_in_recipe(@recipe_yaml) if @recipe_yaml read_input_files open_output {|out| out.print generate_output } end
generate_entry_format()
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 85 def generate_entry_format tree = Parser.parse(@template_data, @tag_type) EntryFormatGenerator.extract_form(tree, @data_format) end
generate_recipe_template(templates)
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 97 def generate_recipe_template(templates) encoding = Encoding.default_external.names[0] AdHocTemplate::EntryFormatGenerator .extract_recipes_from_template_files(templates, @tag_type, encoding) end
init_local_settings()
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 90 def init_local_settings AdHocTemplate::ConfigManager.init_local_settings config_dir = ConfigManager.expand_path('') puts "Please edit configuration files created in #{config_dir}" @init_local_settings = true end
open_output() { |out| ... }
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 108 def open_output if @output_filename File.open(@output_filename, 'wb') {|out| yield out } else yield STDOUT end end
parse_command_line_options()
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 44 def parse_command_line_options OptionParser.new_with_yaml do |opt| opt.banner = "USAGE: #{File.basename($0)} [OPTION]... TEMPLATE_FILE DATA_FILE" opt.version = AdHocTemplate::VERSION opt.inherit_ruby_options('E') # -E, --encoding opt.on(:output_file) {|file| @output_filename = File.expand_path(file) } opt.on(:tag_type) {|given_type| choose_tag_type(given_type) } opt.on(:data_format) {|data_format| choose_data_format(data_format) } opt.on(:tag_config) {|yaml| register_user_defined_tag_type(yaml) } opt.on(:entry_format) { @output_empty_entry = true } opt.on(:init_local_settings) { init_local_settings } opt.on(:recipe_template) { @output_recipe_template = true } opt.on(:cooking_recipe) {|recipe_yaml| @recipe_yaml = recipe_yaml } opt.on(:force_update) { @force_update = true } opt.parse! end unless @data_format guessed_format = ARGV.length < 2 ? :default : guess_file_format(ARGV[1]) @data_format = guessed_format || :default end end
read_input_files()
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 69 def read_input_files template, record = ARGV.map {|arg| File.expand_path(arg) if arg } if template @template_data = File.read(template) else STDERR.puts 'No template file is given.' end @record_data = record ? File.read(record) : ARGF.read end
render()
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 80 def render AdHocTemplate.render(@record_data, @template_data, @tag_type, @data_format, @tag_formatter) end
update_output_files_in_recipe(recipe)
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 103 def update_output_files_in_recipe(recipe) AdHocTemplate::RecipeManager .update_output_files_in_recipe(recipe, @force_update) end
Private Instance Methods
choose_data_format(data_format)
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 140 def choose_data_format(data_format) err_msg = 'The given format is not found. The default format is chosen.' format_part, label_part = data_format.split(/:/, 2) if_any_regex_match(RE_TO_FORMAT, format_part, err_msg) do |_, format| csv_with_label = value_assigned?(label_part) && csv_or_tsv?(format) @data_format = csv_with_label ? { format => label_part } : format end end
choose_tag_type(given_type)
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 132 def choose_tag_type(given_type) err_msg = 'The given type is not found. The default tag is chosen.' if_any_regex_match(TAG_RE_TO_TYPE, given_type, err_msg) do |_, tag_type| @tag_type = tag_type end end
generate_output()
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 126 def generate_output return generate_entry_format if @output_empty_entry return generate_recipe_template(ARGV) if @output_recipe_template render end
register_user_defined_tag_type(tag_config_yaml)
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 150 def register_user_defined_tag_type(tag_config_yaml) config = File.read(File.expand_path(tag_config_yaml)) @tag_type = Parser.register_user_defined_tag_type(config) end
value_assigned?(iteration_label)
click to toggle source
# File lib/ad_hoc_template/command_line_interface.rb, line 155 def value_assigned?(iteration_label) !(iteration_label.nil? || iteration_label.empty?) end