Class: FlatKit::Command::Sort
- Inherits:
-
FlatKit::Command
- Object
- FlatKit::Command
- FlatKit::Command::Sort
- Defined in:
- lib/flat_kit/command/sort.rb
Overview
Internal: The implementation of the sort command.
Instance Attribute Summary collapse
-
#compare_keys ⇒ Object
readonly
Returns the value of attribute compare_keys.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#sort ⇒ Object
readonly
Returns the value of attribute sort.
Attributes inherited from FlatKit::Command
#argv, #env, #logger, #opts, #readers, #writer
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from FlatKit::Command
Methods included from DescendantTracker
#children, #find_child, #find_children, #inherited
Constructor Details
This class inherits a constructor from FlatKit::Command
Instance Attribute Details
#compare_keys ⇒ Object (readonly)
Returns the value of attribute compare_keys.
64 65 66 |
# File 'lib/flat_kit/command/sort.rb', line 64 def compare_keys @compare_keys end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
64 65 66 |
# File 'lib/flat_kit/command/sort.rb', line 64 def reader @reader end |
#sort ⇒ Object (readonly)
Returns the value of attribute sort.
64 65 66 |
# File 'lib/flat_kit/command/sort.rb', line 64 def sort @sort end |
Class Method Details
.description ⇒ Object
13 14 15 |
# File 'lib/flat_kit/command/sort.rb', line 13 def self.description "Sort a given file by a set of fields." end |
.name ⇒ Object
9 10 11 |
# File 'lib/flat_kit/command/sort.rb', line 9 def self.name "sort" end |
.parser ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/flat_kit/command/sort.rb', line 17 def self.parser ::Optimist::Parser.new do Sort.description.to_s "" <<~BANNER Given an input file and a sort key, order the records in that file by that key. If no input file is given the stdin is assumed. If no output file is given then stdout is assumed. The --key parameter is required, and it must be a comma separated list of field nams on the input on which to use as the sort key for the merge process. There must also be only 1 input files. The flatfile type(s) will be automatically determined by the file name. If the output is not a file, but to stdout then the output type will be the same as the first input file, or it can be specified as a commandline switch. BANNER <<~USAGE Usage: fk sort --key surname,given_name file.csv > sorted.csv fk sort --key surname,given_name --output-format json file.csv > sorted.json fk sort --key field1,field2 --output-format json input.csv | gzip -c > sorted.json.gz fk sort --key field1 file.json.gz -o sorted.json.gz gunzip -c file.json.gz | fk sort --key field1 --input-format json --output-format json > gzip -c sorted.json.gz USAGE <<~OPTIONS Options: OPTIONS opt :output, "Send the output to the given path instead of standard out.", default: "<stdout>" opt :input_format, "Input format, csv or json", default: "auto", short: :none opt :output_format, "Output format, csv or json", default: "auto", short: :none opt :key, "The comma separted list of field(s) to use for sorting the input", required: true, type: :string end end |
Instance Method Details
#call ⇒ Object
83 84 85 |
# File 'lib/flat_kit/command/sort.rb', line 83 def call sort.call end |
#parse ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/flat_kit/command/sort.rb', line 66 def parse parser = self.class.parser ::Optimist.with_standard_exception_handling(parser) do @opts = parser.parse(argv) @compare_keys = CSV.parse_line(opts[:key]) paths = parser.leftovers raise ::Optimist::CommandlineError, "1 and only 1 input file is allowed" if paths.size > 1 path = paths.first || "-" # default to stdin @sort = ::FlatKit::Sort.new(input: path, input_fallback: opts[:input_format], output: opts[:output], output_fallback: opts[:output_format], compare_fields: @compare_keys) rescue ::FlatKit::Error => e raise ::Optimist::CommandlineError, e. end end |