Skip to content

Commit

Permalink
refactor cli
Browse files Browse the repository at this point in the history
  • Loading branch information
masa21kik committed Jun 9, 2014
1 parent 53a9364 commit 7f47db4
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions lib/json_cli/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,29 @@ class CLI < Thor
option :join_file, required: true, aliases: :j
option :base_file, aliases: :b, default: '/dev/stdin'
def left_join
left_io = File.open(options[:base_file], 'r')
right_io = File.open(options[:join_file], 'r')
JsonCli::Command::Join.new(left_io, right_io, opts).left_join
[left_io, right_io, opts[:out]].each(&:close)
join_common(__method__)
end

desc 'right_join', 'right outer join of json files'
option :join_key, required: true, aliases: :k
option :join_file, required: true, aliases: :j
option :base_file, aliases: :b, default: '/dev/stdin'
def right_join
left_io = File.open(options[:base_file], 'r')
right_io = File.open(options[:join_file], 'r')
JsonCli::Command::Join.new(left_io, right_io, opts).right_join
[left_io, right_io, opts[:out]].each(&:close)
join_common(__method__)
end

desc 'inner_join', 'inner join of json files'
option :join_key, required: true, aliases: :k
option :join_file, required: true, aliases: :j
option :base_file, aliases: :b, default: '/dev/stdin'
def inner_join
left_io = File.open(options[:base_file], 'r')
right_io = File.open(options[:join_file], 'r')
JsonCli::Command::Join.new(left_io, right_io, opts).inner_join
[left_io, right_io, opts[:out]].each(&:close)
join_common(__method__)
end

desc 'unwind_array [JSON_FILE]', 'unwind array of json file'
option :unwind_key, required: true, aliases: :k
def unwind_array(json_file = '/dev/stdin')
io = File.open(json_file, 'r')
JsonCli::Command::Unwind.new(io, opts).unwind_array
[io, opts[:out]].each(&:close)
unwind_common(__method__, json_file)
end

desc 'unwind_hash [JSON_FILE]', 'unwind hash of json file'
Expand All @@ -52,13 +41,24 @@ def unwind_array(json_file = '/dev/stdin')
option :key_label
option :value_label
def unwind_hash(json_file = '/dev/stdin')
io = File.open(json_file, 'r')
JsonCli::Command::Unwind.new(io, opts).unwind_hash
[io, opts[:out]].each(&:close)
unwind_common(__method__, json_file)
end

private

def join_common(command)
left_io = File.open(options[:base_file], 'r')
right_io = File.open(options[:join_file], 'r')
JsonCli::Command::Join.new(left_io, right_io, opts).send(command)
[left_io, right_io, opts[:out]].each(&:close)
end

def unwind_common(command, json_file)
io = File.open(json_file, 'r')
JsonCli::Command::Unwind.new(io, opts).send(command)
[io, opts[:out]].each(&:close)
end

def opts
@opts ||= { out: File.open(options[:output], 'w') }
.merge(options.select { |key, _| key != :output })
Expand Down

0 comments on commit 7f47db4

Please sign in to comment.