Guest User

Untitled

a guest
Oct 12th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. require 'optparse'
  2. require './nested-redis'
  3.  
  4. options = {}
  5. COMMANDS = {save: 1, delete: 2, print: 3, leaf: 4, remove: 5, dummy: 6}
  6. FORMATS = {text: 1, json: 2, pretty_json:3}
  7.  
  8. optparse = OptionParser.new { |opts|
  9. opts.banner = "Usage: redis-cmd.rb command [options]"
  10.  
  11. opts.on('-s', '--save item, parent, comment', Array,
  12. 'Save a comment on an item.') { |itemId, parentId, comment|
  13. options[:command] = COMMANDS[:save]
  14. options[:itemId] = itemId
  15. options[:parentId] = parentId if parentId != '0'
  16. options[:comment] = comment
  17. }
  18.  
  19. opts.on('-p', '--print itemId',
  20. 'Print the comment hierarchy of an item.') { |itemId|
  21. options[:command] = COMMANDS[:print]
  22. options[:itemId] = itemId
  23. options[:format] = FORMATS[:text]
  24. }
  25.  
  26. opts.on('-l', '--leaf leafId',
  27. 'Print the hierarchy of a leaf.') { |leafId|
  28. options[:command] = COMMANDS[:leaf]
  29. options[:itemId] = leafId
  30. options[:format] = FORMATS[:text]
  31. }
  32.  
  33. opts.on('-d', '--delete itemId',
  34. 'Delete all the comments of an item.') { |itemId|
  35. options[:command] = COMMANDS[:delete]
  36. options[:itemId] = itemId
  37. }
  38.  
  39. opts.on('-r', '--remove item, leaf, parent', Array,
  40. 'Delete a leaf and it\'s hierarchy from a comment.') { |itemId, leafId, parentId|
  41. options[:command] = COMMANDS[:remove]
  42. options[:itemId] = itemId
  43. options[:leafId] = leafId
  44. options[:parentId] = parentId
  45. }
  46.  
  47. opts.on('-u', '--dummy itemId',
  48. 'Save a dummy comment hierarchy on an item.') { |itemId|
  49. options[:command] = COMMANDS[:dummy]
  50. options[:itemId] = itemId
  51. }
  52.  
  53. opts.on('-j', '--json [pretty]', 'Set the output format to JSON.') { |pretty|
  54. options[:format] = pretty ? FORMATS[:pretty_json] : FORMATS[:json]
  55. }
  56.  
  57. opts.on( '-h', '--help', 'Display this screen' ) {
  58. puts opts
  59. exit
  60. }
  61. }
  62.  
  63. def print_comments(comments, tabs = 0)
  64. comments.each { |comment|
  65. tabs.times { putc "\t" }
  66. puts "#{comment['comment']}"
  67. if comment['comments']
  68. print_comments comment['comments'], tabs + 1
  69. end
  70. }
  71. end
  72.  
  73. def save_dummy(cr, id)
  74. tree = cr.save(id, { comment:'Tree', test:1 })
  75. root = cr.save(id, { comment:'Root', kind:'ginger', age:50 }, tree)
  76. trunk = cr.save(id, { comment:'Trunk', kmh_to_mph:0.621 }, tree)
  77. branch = cr.save(id, { comment:'Branch' }, trunk)
  78. leaf = cr.save(id, { comment:'Leaf' }, branch)
  79. top = cr.save(id, { comment:'Top' }, tree)
  80. end
  81.  
  82. def print_result comments, options
  83. case options[:format]
  84. when FORMATS[:text]
  85. print_comments comments
  86. when FORMATS[:json]
  87. puts comments.to_json
  88. when FORMATS[:pretty_json]
  89. puts JSON.pretty_generate(comments)
  90. end
  91. end
  92.  
  93. optparse.parse!
  94. id = options[:itemId]
  95. cr = CommentsRepository.new
  96.  
  97. case options[:command]
  98. when COMMANDS[:save]
  99. comment = { comment: options[:comment] }
  100. id = cr.save(id, comment, options[:parentId])
  101. puts "New comment saved with id #{id}."
  102. when COMMANDS[:print]
  103. print_result cr.get(id), options
  104. when COMMANDS[:leaf]
  105. print_result [cr.leaf(id)], options
  106. when COMMANDS[:delete]
  107. cr.delete cr.get(id), id, id
  108. when COMMANDS[:remove]
  109. cr.delete cr.leaf(options[:leafId]), options[:parentId], id, false
  110. when COMMANDS[:dummy]
  111. save_dummy cr, id
  112. else
  113. puts optparse
  114. end
Add Comment
Please, Sign In to add comment