Class: HMap::Command::Writer

Inherits:
HMap::Command show all
Defined in:
lib/hmap/command/hmap_writer.rb

Overview

hmap file gen cmd

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Writer

Returns a new instance of Writer.



20
21
22
23
24
25
26
# File 'lib/hmap/command/hmap_writer.rb', line 20

def initialize(argv)
  super
  json_path = argv.option('json-path')
  @json_path = json_path unless json_path.nil?
  output_path = argv.option('output-path')
  @output_path = output_path.nil? ? Pathname('.') : Pathname(output_path)
end

Class Method Details

.optionsObject

help



35
36
37
38
39
40
# File 'lib/hmap/command/hmap_writer.rb', line 35

def self.options
  [
    ['--json-path=/project/dir/json', 'The path to the hmap json data.'],
    ['--output-path=/project/dir/hmap file', 'The path json data to the hmap file.']
  ].concat(super)
end

Instance Method Details

#runObject



42
43
44
45
46
47
48
49
50
# File 'lib/hmap/command/hmap_writer.rb', line 42

def run
  puts "\n[hmap-gen-from-json] start..............".yellow
  json_file = File.read(@json_path)
  json = JSON.parse(json_file)
  path = @output_path
  path = path.join("#{File.basename(@json_path, '.*')}.hmap") if path.directory?
  HMapSaver.new_from_buckets(json).write_to(path)
  puts '[hmap-gen-from-json] finish..............'.yellow
end

#validate!Object



28
29
30
31
32
# File 'lib/hmap/command/hmap_writer.rb', line 28

def validate!
  super
  help! 'error: no input json files which to use with the `--json-path` option.' if @json_path.nil?
  help! "error: Input json file #{@json_path}: no such file" unless File.exist?(@json_path)
end