Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'optparse'
- require './nested-redis'
- options = {}
- COMMANDS = {save: 1, delete: 2, print: 3, leaf: 4, remove: 5, dummy: 6}
- FORMATS = {text: 1, json: 2, pretty_json:3}
- optparse = OptionParser.new { |opts|
- opts.banner = "Usage: redis-cmd.rb command [options]"
- opts.on('-s', '--save item, parent, comment', Array,
- 'Save a comment on an item.') { |itemId, parentId, comment|
- options[:command] = COMMANDS[:save]
- options[:itemId] = itemId
- options[:parentId] = parentId if parentId != '0'
- options[:comment] = comment
- }
- opts.on('-p', '--print itemId',
- 'Print the comment hierarchy of an item.') { |itemId|
- options[:command] = COMMANDS[:print]
- options[:itemId] = itemId
- options[:format] = FORMATS[:text]
- }
- opts.on('-l', '--leaf leafId',
- 'Print the hierarchy of a leaf.') { |leafId|
- options[:command] = COMMANDS[:leaf]
- options[:itemId] = leafId
- options[:format] = FORMATS[:text]
- }
- opts.on('-d', '--delete itemId',
- 'Delete all the comments of an item.') { |itemId|
- options[:command] = COMMANDS[:delete]
- options[:itemId] = itemId
- }
- opts.on('-r', '--remove item, leaf, parent', Array,
- 'Delete a leaf and it\'s hierarchy from a comment.') { |itemId, leafId, parentId|
- options[:command] = COMMANDS[:remove]
- options[:itemId] = itemId
- options[:leafId] = leafId
- options[:parentId] = parentId
- }
- opts.on('-u', '--dummy itemId',
- 'Save a dummy comment hierarchy on an item.') { |itemId|
- options[:command] = COMMANDS[:dummy]
- options[:itemId] = itemId
- }
- opts.on('-j', '--json [pretty]', 'Set the output format to JSON.') { |pretty|
- options[:format] = pretty ? FORMATS[:pretty_json] : FORMATS[:json]
- }
- opts.on( '-h', '--help', 'Display this screen' ) {
- puts opts
- exit
- }
- }
- def print_comments(comments, tabs = 0)
- comments.each { |comment|
- tabs.times { putc "\t" }
- puts "#{comment['comment']}"
- if comment['comments']
- print_comments comment['comments'], tabs + 1
- end
- }
- end
- def save_dummy(cr, id)
- tree = cr.save(id, { comment:'Tree', test:1 })
- root = cr.save(id, { comment:'Root', kind:'ginger', age:50 }, tree)
- trunk = cr.save(id, { comment:'Trunk', kmh_to_mph:0.621 }, tree)
- branch = cr.save(id, { comment:'Branch' }, trunk)
- leaf = cr.save(id, { comment:'Leaf' }, branch)
- top = cr.save(id, { comment:'Top' }, tree)
- end
- def print_result comments, options
- case options[:format]
- when FORMATS[:text]
- print_comments comments
- when FORMATS[:json]
- puts comments.to_json
- when FORMATS[:pretty_json]
- puts JSON.pretty_generate(comments)
- end
- end
- optparse.parse!
- id = options[:itemId]
- cr = CommentsRepository.new
- case options[:command]
- when COMMANDS[:save]
- comment = { comment: options[:comment] }
- id = cr.save(id, comment, options[:parentId])
- puts "New comment saved with id #{id}."
- when COMMANDS[:print]
- print_result cr.get(id), options
- when COMMANDS[:leaf]
- print_result [cr.leaf(id)], options
- when COMMANDS[:delete]
- cr.delete cr.get(id), id, id
- when COMMANDS[:remove]
- cr.delete cr.leaf(options[:leafId]), options[:parentId], id, false
- when COMMANDS[:dummy]
- save_dummy cr, id
- else
- puts optparse
- end
Add Comment
Please, Sign In to add comment