Class: AdHocTemplate::CommandLineInterface

Inherits:
Object
  • Object
show all
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,
}
FORMAT_RE_TO_FORMAT =
{
  /\Ad(efault)?/i => :default,
  /\Ay(a?ml)?/i => :yaml,
  /\Aj(son)?/i => :json,
  /\Ac(sv)?/i => :csv,
  /\At(sv)?/i => :tsv,
}
FILE_EXTENTIONS =
{
  /\.ya?ml\Z/i => :yaml,
  /\.json\Z/i => :json,
  /\.csv\Z/i => :csv,
  /\.tsv\Z/i => :tsv,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLineInterface

Returns a new instance of CommandLineInterface.



33
34
35
36
37
38
# File 'lib/ad_hoc_template/command_line_interface.rb', line 33

def initialize
  @tag_formatter = AdHocTemplate::DefaultTagFormatter.new
  @output_filename = nil
  @tag_type = :default
  @data_format = nil
end

Instance Attribute Details

#data_formatObject

Returns the value of attribute data_format.



8
9
10
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8

def data_format
  @data_format
end

#output_filenameObject

Returns the value of attribute output_filename.



8
9
10
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8

def output_filename
  @output_filename
end

#record_dataObject

Returns the value of attribute record_data.



8
9
10
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8

def record_data
  @record_data
end

#tag_typeObject

Returns the value of attribute tag_type.



8
9
10
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8

def tag_type
  @tag_type
end

#template_dataObject

Returns the value of attribute template_data.



8
9
10
# File 'lib/ad_hoc_template/command_line_interface.rb', line 8

def template_data
  @template_data
end

Instance Method Details

#convertObject



93
94
95
96
# File 'lib/ad_hoc_template/command_line_interface.rb', line 93

def convert
  AdHocTemplate.convert(@record_data, @template_data, @tag_type,
                        @data_format, @tag_formatter)
end

#executeObject



108
109
110
111
112
# File 'lib/ad_hoc_template/command_line_interface.rb', line 108

def execute
  parse_command_line_options
  read_input_files
  open_output {|out| out.print convert }
end

#open_outputObject



98
99
100
101
102
103
104
105
106
# File 'lib/ad_hoc_template/command_line_interface.rb', line 98

def open_output
  if @output_filename
    open(@output_filename, "wb") do |out|
      yield out
    end
  else
    yield STDOUT
  end
end

#parse_command_line_optionsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ad_hoc_template/command_line_interface.rb', line 46

def parse_command_line_options
  OptionParser.new("USAGE: #{File.basename($0)} [OPTION]... TEMPLATE_FILE DATA_FILE") do |opt|
    opt.on("-E [ex[:in]]", "--encoding [=ex[:in]]",
           "Specify the default external and internal character encodings (same as the option of MRI") do |given_opt|
      self.set_encoding(given_opt)
    end

    opt.on("-o [output_file]", "--output [=output_file]",
           "Save the result into the specified file.") do |output_file|
      @output_filename = File.expand_path(output_file)
    end

    opt.on("-t [tag_type]", "--tag-type [=tag_type]",
           "Choose a template tag type: default, curly_brackets or square_brackets") do |given_type|
      choose_tag_type(given_type)
    end

    opt.on("-d [data_format]", "--data-format [=data_format]",
           "Specify the format of input data: default, yaml, json, csv or tsv") do |data_format|
      choose_data_format(data_format)
    end

    opt.on("-u [tag_config.yaml]","--user-defined-tag [=tag_config.yaml]",
           "Configure a user-defined tag. The configuration file is in YAML format.") do |tag_config_yaml|
      register_user_defined_tag_type(tag_config_yaml)
    end

    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_filesObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/ad_hoc_template/command_line_interface.rb', line 82

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

#set_encoding(given_opt) ⇒ Object



40
41
42
43
44
# File 'lib/ad_hoc_template/command_line_interface.rb', line 40

def set_encoding(given_opt)
  external, internal = given_opt.split(/:/o, 2)
  Encoding.default_external = external if external and not external.empty?
  Encoding.default_internal = internal if internal and not internal.empty?
end