Class: AdHocTemplate::CommandLineInterface
- Inherits:
-
Object
- Object
- AdHocTemplate::CommandLineInterface
show all
- Includes:
- Utils
- Defined in:
- lib/ad_hoc_template/command_line_interface.rb
Constant Summary
collapse
- TAG_RE_TO_TYPE =
{
/\Ad(efault)?/i => :default,
/\Ac(urly_brackets)?/i => :curly_brackets,
/\As(quare_brackets)?/i => :square_brackets,
/\Axml_like1/i => :xml_like1,
/\Axml_like2/i => :xml_like2,
/\Axml_comment_like/i => :xml_comment_like,
}.freeze
- RE_TO_FORMAT =
{
/\Ad(efault)?/i => :default,
/\Ay(a?ml)?/i => :yaml,
/\Aj(son)?/i => :json,
/\Ac(sv)?/i => :csv,
/\At(sv)?/i => :tsv,
}.freeze
Constants included
from Utils
Utils::FILE_EXTENTIONS
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
#guess_file_format, #if_any_regex_match
Constructor Details
Returns a new instance of CommandLineInterface.
35
36
37
38
39
40
41
42
|
# 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
|
Instance Attribute Details
Returns the value of attribute data_format.
14
15
16
|
# File 'lib/ad_hoc_template/command_line_interface.rb', line 14
def data_format
@data_format
end
|
#output_empty_entry=(value) ⇒ Object
Sets the attribute output_empty_entry
16
17
18
|
# File 'lib/ad_hoc_template/command_line_interface.rb', line 16
def output_empty_entry=(value)
@output_empty_entry = value
end
|
#output_filename ⇒ Object
Returns the value of attribute output_filename.
14
15
16
|
# File 'lib/ad_hoc_template/command_line_interface.rb', line 14
def output_filename
@output_filename
end
|
#record_data ⇒ Object
Returns the value of attribute record_data.
14
15
16
|
# File 'lib/ad_hoc_template/command_line_interface.rb', line 14
def record_data
@record_data
end
|
#tag_type ⇒ Object
Returns the value of attribute tag_type.
14
15
16
|
# File 'lib/ad_hoc_template/command_line_interface.rb', line 14
def tag_type
@tag_type
end
|
#template_data ⇒ Object
Returns the value of attribute template_data.
14
15
16
|
# File 'lib/ad_hoc_template/command_line_interface.rb', line 14
def template_data
@template_data
end
|
Instance Method Details
#execute ⇒ Object
116
117
118
119
120
121
122
|
# 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 ⇒ Object
85
86
87
88
|
# File 'lib/ad_hoc_template/command_line_interface.rb', line 85
def generate_entry_format
tree = Parser.parse(@template_data, @tag_type)
EntryFormatGenerator.(tree, @data_format)
end
|
#generate_recipe_template(templates) ⇒ Object
97
98
99
100
101
|
# File 'lib/ad_hoc_template/command_line_interface.rb', line 97
def generate_recipe_template(templates)
encoding = Encoding.default_external.names[0]
AdHocTemplate::EntryFormatGenerator
.(templates, @tag_type, encoding)
end
|
#init_local_settings ⇒ Object
#open_output ⇒ Object
108
109
110
111
112
113
114
|
# 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 ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# 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') 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
|
69
70
71
72
73
74
75
76
77
78
|
# 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 ⇒ Object
80
81
82
83
|
# 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) ⇒ Object
103
104
105
106
|
# 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
|