chess_engine 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (292) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile.lock +21 -0
  4. data/README.md +26 -0
  5. data/chess_engine.gemspec +14 -0
  6. data/lib/chess_engine/board.rb +95 -0
  7. data/lib/chess_engine/cli.rb +105 -0
  8. data/lib/chess_engine/game.rb +148 -0
  9. data/lib/chess_engine/input.rb +19 -0
  10. data/lib/chess_engine/move.rb +35 -0
  11. data/lib/chess_engine/piece.rb +97 -0
  12. data/lib/chess_engine/validator.rb +87 -0
  13. data/lib/chess_engine.rb +1 -0
  14. data/spec/board_spec.rb +70 -0
  15. data/spec/game_helper.rb +5 -0
  16. data/spec/game_spec.rb +79 -0
  17. data/spec/spec_helper.rb +100 -0
  18. data/spec/validator_spec.rb +26 -0
  19. data/vendor/bundle/bin/coderay +27 -0
  20. data/vendor/bundle/bin/pry +27 -0
  21. data/vendor/bundle/cache/coderay-1.1.2.gem +0 -0
  22. data/vendor/bundle/cache/colorize-0.8.1.gem +0 -0
  23. data/vendor/bundle/cache/method_source-0.9.2.gem +0 -0
  24. data/vendor/bundle/cache/pry-0.12.2.gem +0 -0
  25. data/vendor/bundle/cache/rb-readline-0.5.5.gem +0 -0
  26. data/vendor/bundle/gems/coderay-1.1.2/MIT-LICENSE +22 -0
  27. data/vendor/bundle/gems/coderay-1.1.2/README_INDEX.rdoc +123 -0
  28. data/vendor/bundle/gems/coderay-1.1.2/bin/coderay +215 -0
  29. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/duo.rb +81 -0
  30. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/_map.rb +17 -0
  31. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/comment_filter.rb +25 -0
  32. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/count.rb +39 -0
  33. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/debug.rb +49 -0
  34. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/debug_lint.rb +63 -0
  35. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/div.rb +23 -0
  36. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/encoder.rb +190 -0
  37. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/filter.rb +58 -0
  38. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb +65 -0
  39. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html/numbering.rb +108 -0
  40. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html/output.rb +166 -0
  41. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html.rb +333 -0
  42. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/json.rb +83 -0
  43. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/lines_of_code.rb +45 -0
  44. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/lint.rb +59 -0
  45. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/null.rb +18 -0
  46. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/page.rb +24 -0
  47. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/span.rb +23 -0
  48. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/statistic.rb +95 -0
  49. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/terminal.rb +195 -0
  50. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/text.rb +46 -0
  51. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/token_kind_filter.rb +111 -0
  52. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/xml.rb +72 -0
  53. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/yaml.rb +50 -0
  54. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders.rb +18 -0
  55. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/for_redcloth.rb +95 -0
  56. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/helpers/file_type.rb +151 -0
  57. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/helpers/plugin.rb +55 -0
  58. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/helpers/plugin_host.rb +221 -0
  59. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/helpers/word_list.rb +72 -0
  60. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/_map.rb +24 -0
  61. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/c.rb +189 -0
  62. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/clojure.rb +217 -0
  63. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/cpp.rb +217 -0
  64. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/css.rb +196 -0
  65. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/debug.rb +75 -0
  66. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/delphi.rb +144 -0
  67. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/diff.rb +221 -0
  68. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/erb.rb +81 -0
  69. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/go.rb +208 -0
  70. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/groovy.rb +268 -0
  71. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/haml.rb +168 -0
  72. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/html.rb +275 -0
  73. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/java/builtin_types.rb +421 -0
  74. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/java.rb +174 -0
  75. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/java_script.rb +237 -0
  76. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/json.rb +98 -0
  77. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/lua.rb +280 -0
  78. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/php.rb +527 -0
  79. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/python.rb +287 -0
  80. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/raydebug.rb +75 -0
  81. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/ruby/patterns.rb +178 -0
  82. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/ruby/string_state.rb +79 -0
  83. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/ruby.rb +477 -0
  84. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/sass.rb +232 -0
  85. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/scanner.rb +337 -0
  86. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/sql.rb +169 -0
  87. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/taskpaper.rb +36 -0
  88. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/text.rb +26 -0
  89. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/xml.rb +17 -0
  90. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners/yaml.rb +140 -0
  91. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/scanners.rb +27 -0
  92. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/styles/_map.rb +7 -0
  93. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/styles/alpha.rb +153 -0
  94. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/styles/style.rb +18 -0
  95. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/styles.rb +15 -0
  96. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/token_kinds.rb +85 -0
  97. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/tokens.rb +161 -0
  98. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/tokens_proxy.rb +55 -0
  99. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay/version.rb +3 -0
  100. data/vendor/bundle/gems/coderay-1.1.2/lib/coderay.rb +284 -0
  101. data/vendor/bundle/gems/colorize-0.8.1/CHANGELOG +65 -0
  102. data/vendor/bundle/gems/colorize-0.8.1/LICENSE +339 -0
  103. data/vendor/bundle/gems/colorize-0.8.1/README.md +105 -0
  104. data/vendor/bundle/gems/colorize-0.8.1/Rakefile +13 -0
  105. data/vendor/bundle/gems/colorize-0.8.1/colorize.gemspec +36 -0
  106. data/vendor/bundle/gems/colorize-0.8.1/lib/colorize/class_methods.rb +120 -0
  107. data/vendor/bundle/gems/colorize-0.8.1/lib/colorize/instance_methods.rb +132 -0
  108. data/vendor/bundle/gems/colorize-0.8.1/lib/colorize.rb +12 -0
  109. data/vendor/bundle/gems/colorize-0.8.1/lib/colorized_string.rb +19 -0
  110. data/vendor/bundle/gems/colorize-0.8.1/test/test_colorize.rb +164 -0
  111. data/vendor/bundle/gems/method_source-0.9.2/Gemfile +2 -0
  112. data/vendor/bundle/gems/method_source-0.9.2/LICENSE +25 -0
  113. data/vendor/bundle/gems/method_source-0.9.2/README.markdown +91 -0
  114. data/vendor/bundle/gems/method_source-0.9.2/Rakefile +81 -0
  115. data/vendor/bundle/gems/method_source-0.9.2/lib/method_source/code_helpers.rb +154 -0
  116. data/vendor/bundle/gems/method_source-0.9.2/lib/method_source/source_location.rb +138 -0
  117. data/vendor/bundle/gems/method_source-0.9.2/lib/method_source/version.rb +3 -0
  118. data/vendor/bundle/gems/method_source-0.9.2/lib/method_source.rb +141 -0
  119. data/vendor/bundle/gems/method_source-0.9.2/method_source.gemspec +35 -0
  120. data/vendor/bundle/gems/method_source-0.9.2/spec/method_source/code_helpers_spec.rb +43 -0
  121. data/vendor/bundle/gems/method_source-0.9.2/spec/method_source_spec.rb +143 -0
  122. data/vendor/bundle/gems/method_source-0.9.2/spec/spec_helper.rb +101 -0
  123. data/vendor/bundle/gems/pry-0.12.2/CHANGELOG.md +953 -0
  124. data/vendor/bundle/gems/pry-0.12.2/LICENSE +25 -0
  125. data/vendor/bundle/gems/pry-0.12.2/README.md +390 -0
  126. data/vendor/bundle/gems/pry-0.12.2/bin/pry +8 -0
  127. data/vendor/bundle/gems/pry-0.12.2/lib/pry/basic_object.rb +6 -0
  128. data/vendor/bundle/gems/pry-0.12.2/lib/pry/cli.rb +218 -0
  129. data/vendor/bundle/gems/pry-0.12.2/lib/pry/code/code_file.rb +110 -0
  130. data/vendor/bundle/gems/pry-0.12.2/lib/pry/code/code_range.rb +71 -0
  131. data/vendor/bundle/gems/pry-0.12.2/lib/pry/code/loc.rb +98 -0
  132. data/vendor/bundle/gems/pry-0.12.2/lib/pry/code.rb +353 -0
  133. data/vendor/bundle/gems/pry-0.12.2/lib/pry/code_object.rb +195 -0
  134. data/vendor/bundle/gems/pry-0.12.2/lib/pry/color_printer.rb +65 -0
  135. data/vendor/bundle/gems/pry-0.12.2/lib/pry/command.rb +723 -0
  136. data/vendor/bundle/gems/pry-0.12.2/lib/pry/command_set.rb +415 -0
  137. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/amend_line.rb +98 -0
  138. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/bang.rb +20 -0
  139. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/bang_pry.rb +17 -0
  140. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  141. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/exception_formatter.rb +79 -0
  142. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb +71 -0
  143. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  144. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat.rb +63 -0
  145. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cd.rb +41 -0
  146. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/change_inspector.rb +27 -0
  147. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/change_prompt.rb +46 -0
  148. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/clear_screen.rb +14 -0
  149. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/code_collector.rb +167 -0
  150. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/disable_pry.rb +27 -0
  151. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/disabled_commands.rb +2 -0
  152. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/easter_eggs.rb +112 -0
  153. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/edit/exception_patcher.rb +25 -0
  154. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/edit/file_and_line_locator.rb +36 -0
  155. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/edit.rb +200 -0
  156. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/exit.rb +43 -0
  157. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/exit_all.rb +29 -0
  158. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/exit_program.rb +23 -0
  159. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/find_method.rb +191 -0
  160. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/fix_indent.rb +19 -0
  161. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_cd.rb +26 -0
  162. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_install.rb +32 -0
  163. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_list.rb +33 -0
  164. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_open.rb +29 -0
  165. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_readme.rb +25 -0
  166. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_search.rb +40 -0
  167. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gem_stats.rb +83 -0
  168. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/gist.rb +102 -0
  169. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/help.rb +164 -0
  170. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/hist.rb +181 -0
  171. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/import_set.rb +23 -0
  172. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/install_command.rb +54 -0
  173. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/jump_to.rb +29 -0
  174. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/list_inspectors.rb +35 -0
  175. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/constants.rb +58 -0
  176. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/formatter.rb +51 -0
  177. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/globals.rb +46 -0
  178. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/grep.rb +19 -0
  179. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/instance_vars.rb +38 -0
  180. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/interrogatable.rb +18 -0
  181. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/jruby_hacks.rb +49 -0
  182. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/local_names.rb +33 -0
  183. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/local_vars.rb +37 -0
  184. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/ls_entity.rb +69 -0
  185. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/methods.rb +54 -0
  186. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/methods_helper.rb +46 -0
  187. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls/self_methods.rb +33 -0
  188. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ls.rb +113 -0
  189. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/nesting.rb +25 -0
  190. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/play.rb +102 -0
  191. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/pry_backtrace.rb +25 -0
  192. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/pry_version.rb +17 -0
  193. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/raise_up.rb +33 -0
  194. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/reload_code.rb +62 -0
  195. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/reset.rb +18 -0
  196. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/ri.rb +65 -0
  197. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/save_file.rb +61 -0
  198. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/shell_command.rb +75 -0
  199. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/shell_mode.rb +25 -0
  200. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/show_doc.rb +81 -0
  201. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/show_info.rb +216 -0
  202. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/show_input.rb +17 -0
  203. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/show_source.rb +53 -0
  204. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/stat.rb +40 -0
  205. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/switch_to.rb +23 -0
  206. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/toggle_color.rb +24 -0
  207. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/watch_expression/expression.rb +38 -0
  208. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/watch_expression.rb +107 -0
  209. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/whereami.rb +197 -0
  210. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands/wtf.rb +70 -0
  211. data/vendor/bundle/gems/pry-0.12.2/lib/pry/commands.rb +6 -0
  212. data/vendor/bundle/gems/pry-0.12.2/lib/pry/config/behavior.rb +255 -0
  213. data/vendor/bundle/gems/pry-0.12.2/lib/pry/config/convenience.rb +28 -0
  214. data/vendor/bundle/gems/pry-0.12.2/lib/pry/config/default.rb +159 -0
  215. data/vendor/bundle/gems/pry-0.12.2/lib/pry/config/memoization.rb +48 -0
  216. data/vendor/bundle/gems/pry-0.12.2/lib/pry/config.rb +35 -0
  217. data/vendor/bundle/gems/pry-0.12.2/lib/pry/core_extensions.rb +142 -0
  218. data/vendor/bundle/gems/pry-0.12.2/lib/pry/editor.rb +128 -0
  219. data/vendor/bundle/gems/pry-0.12.2/lib/pry/exceptions.rb +76 -0
  220. data/vendor/bundle/gems/pry-0.12.2/lib/pry/forwardable.rb +23 -0
  221. data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/base_helpers.rb +200 -0
  222. data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/command_helpers.rb +157 -0
  223. data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/documentation_helpers.rb +76 -0
  224. data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/options_helpers.rb +27 -0
  225. data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/platform.rb +58 -0
  226. data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/table.rb +114 -0
  227. data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers/text.rb +115 -0
  228. data/vendor/bundle/gems/pry-0.12.2/lib/pry/helpers.rb +6 -0
  229. data/vendor/bundle/gems/pry-0.12.2/lib/pry/history.rb +161 -0
  230. data/vendor/bundle/gems/pry-0.12.2/lib/pry/hooks.rb +171 -0
  231. data/vendor/bundle/gems/pry-0.12.2/lib/pry/indent.rb +408 -0
  232. data/vendor/bundle/gems/pry-0.12.2/lib/pry/input_completer.rb +265 -0
  233. data/vendor/bundle/gems/pry-0.12.2/lib/pry/input_lock.rb +130 -0
  234. data/vendor/bundle/gems/pry-0.12.2/lib/pry/inspector.rb +27 -0
  235. data/vendor/bundle/gems/pry-0.12.2/lib/pry/last_exception.rb +61 -0
  236. data/vendor/bundle/gems/pry-0.12.2/lib/pry/method/disowned.rb +55 -0
  237. data/vendor/bundle/gems/pry-0.12.2/lib/pry/method/patcher.rb +122 -0
  238. data/vendor/bundle/gems/pry-0.12.2/lib/pry/method/weird_method_locator.rb +198 -0
  239. data/vendor/bundle/gems/pry-0.12.2/lib/pry/method.rb +552 -0
  240. data/vendor/bundle/gems/pry-0.12.2/lib/pry/object_path.rb +83 -0
  241. data/vendor/bundle/gems/pry-0.12.2/lib/pry/output.rb +50 -0
  242. data/vendor/bundle/gems/pry-0.12.2/lib/pry/pager.rb +250 -0
  243. data/vendor/bundle/gems/pry-0.12.2/lib/pry/platform.rb +91 -0
  244. data/vendor/bundle/gems/pry-0.12.2/lib/pry/plugins.rb +122 -0
  245. data/vendor/bundle/gems/pry-0.12.2/lib/pry/prompt.rb +145 -0
  246. data/vendor/bundle/gems/pry-0.12.2/lib/pry/pry_class.rb +425 -0
  247. data/vendor/bundle/gems/pry-0.12.2/lib/pry/pry_instance.rb +699 -0
  248. data/vendor/bundle/gems/pry-0.12.2/lib/pry/repl.rb +261 -0
  249. data/vendor/bundle/gems/pry-0.12.2/lib/pry/repl_file_loader.rb +73 -0
  250. data/vendor/bundle/gems/pry-0.12.2/lib/pry/ring.rb +84 -0
  251. data/vendor/bundle/gems/pry-0.12.2/lib/pry/rubygem.rb +84 -0
  252. data/vendor/bundle/gems/pry-0.12.2/lib/pry/slop/LICENSE +20 -0
  253. data/vendor/bundle/gems/pry-0.12.2/lib/pry/slop/commands.rb +195 -0
  254. data/vendor/bundle/gems/pry-0.12.2/lib/pry/slop/option.rb +206 -0
  255. data/vendor/bundle/gems/pry-0.12.2/lib/pry/slop.rb +661 -0
  256. data/vendor/bundle/gems/pry-0.12.2/lib/pry/terminal.rb +91 -0
  257. data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/evalable.rb +15 -0
  258. data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/mockable.rb +14 -0
  259. data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/pry_tester.rb +73 -0
  260. data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/utility.rb +26 -0
  261. data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable/variables.rb +46 -0
  262. data/vendor/bundle/gems/pry-0.12.2/lib/pry/testable.rb +70 -0
  263. data/vendor/bundle/gems/pry-0.12.2/lib/pry/version.rb +3 -0
  264. data/vendor/bundle/gems/pry-0.12.2/lib/pry/wrapped_module/candidate.rb +131 -0
  265. data/vendor/bundle/gems/pry-0.12.2/lib/pry/wrapped_module.rb +378 -0
  266. data/vendor/bundle/gems/pry-0.12.2/lib/pry.rb +132 -0
  267. data/vendor/bundle/gems/rb-readline-0.5.5/CHANGES +94 -0
  268. data/vendor/bundle/gems/rb-readline-0.5.5/LICENSE +25 -0
  269. data/vendor/bundle/gems/rb-readline-0.5.5/README.md +81 -0
  270. data/vendor/bundle/gems/rb-readline-0.5.5/Rakefile +25 -0
  271. data/vendor/bundle/gems/rb-readline-0.5.5/bench/_rl_adjust_point.rb +26 -0
  272. data/vendor/bundle/gems/rb-readline-0.5.5/examples/example_readline.rb +8 -0
  273. data/vendor/bundle/gems/rb-readline-0.5.5/examples/example_readline_with_completion.rb +18 -0
  274. data/vendor/bundle/gems/rb-readline-0.5.5/examples/tinyirb.rb +11 -0
  275. data/vendor/bundle/gems/rb-readline-0.5.5/lib/rb-readline.rb +18 -0
  276. data/vendor/bundle/gems/rb-readline-0.5.5/lib/rbreadline/version.rb +3 -0
  277. data/vendor/bundle/gems/rb-readline-0.5.5/lib/rbreadline.rb +8920 -0
  278. data/vendor/bundle/gems/rb-readline-0.5.5/lib/readline.rb +539 -0
  279. data/vendor/bundle/gems/rb-readline-0.5.5/rb-readline.gemspec +51 -0
  280. data/vendor/bundle/gems/rb-readline-0.5.5/setup.rb +1585 -0
  281. data/vendor/bundle/gems/rb-readline-0.5.5/test/support/filesystem_completion_helper.rb +53 -0
  282. data/vendor/bundle/gems/rb-readline-0.5.5/test/test_completion.rb +100 -0
  283. data/vendor/bundle/gems/rb-readline-0.5.5/test/test_filename_completion_proc.rb +88 -0
  284. data/vendor/bundle/gems/rb-readline-0.5.5/test/test_history.rb +29 -0
  285. data/vendor/bundle/gems/rb-readline-0.5.5/test/test_rbreadline.rb +23 -0
  286. data/vendor/bundle/gems/rb-readline-0.5.5/test/test_readline.rb +221 -0
  287. data/vendor/bundle/specifications/coderay-1.1.2.gemspec +26 -0
  288. data/vendor/bundle/specifications/colorize-0.8.1.gemspec +38 -0
  289. data/vendor/bundle/specifications/method_source-0.9.2.gemspec +35 -0
  290. data/vendor/bundle/specifications/pry-0.12.2.gemspec +38 -0
  291. data/vendor/bundle/specifications/rb-readline-0.5.5.gemspec +39 -0
  292. metadata +363 -0
@@ -0,0 +1,195 @@
1
+ class Pry
2
+ # This class is responsible for taking a string (identifying a
3
+ # command/class/method/etc) and returning the relevant type of object.
4
+ # For example, if the user looks up "show-source" then a
5
+ # `Pry::Command` will be returned. Alternatively, if the user passes in "Pry#repl" then
6
+ # a `Pry::Method` object will be returned.
7
+ #
8
+ # The `CodeObject.lookup` method is responsible for 1. figuring out what kind of
9
+ # object the user wants (applying precedence rules in doing so -- i.e methods
10
+ # get precedence over commands with the same name) and 2. Returning
11
+ # the appropriate object. If the user fails to provide a string
12
+ # identifer for the object (i.e they pass in `nil` or "") then the
13
+ # object looked up will be the 'current method' or 'current class'
14
+ # associated with the Binding.
15
+ #
16
+ # TODO: This class is a clusterfuck. We need a much more robust
17
+ # concept of what a "Code Object" really is. Currently
18
+ # commands/classes/candidates/methods and so on just share a very
19
+ # ill-defined interface.
20
+ class CodeObject
21
+ module Helpers
22
+ # we need this helper as some Pry::Method objects can wrap Procs
23
+ # @return [Boolean]
24
+ def real_method_object?
25
+ is_a?(::Method) || is_a?(::UnboundMethod)
26
+ end
27
+
28
+ def c_method?
29
+ real_method_object? && source_type == :c
30
+ end
31
+
32
+ def module_with_yard_docs?
33
+ is_a?(WrappedModule) && yard_docs?
34
+ end
35
+
36
+ def command?
37
+ is_a?(Module) && self <= Pry::Command
38
+ end
39
+
40
+ # @return [Boolean] `true` if this module was defined by means of the C API,
41
+ # `false` if it's a Ruby module.
42
+ # @note If a module defined by C was extended with a lot of methods written
43
+ # in Ruby, this method would fail.
44
+ def c_module?
45
+ if is_a?(WrappedModule)
46
+
47
+ method_locations = wrapped.methods(false).map do |m|
48
+ wrapped.method(m).source_location
49
+ end
50
+
51
+ method_locations.concat(wrapped.instance_methods(false).map do |m|
52
+ wrapped.instance_method(m).source_location
53
+ end)
54
+
55
+ c_methods = method_locations.grep(nil).count
56
+ ruby_methods = method_locations.count - c_methods
57
+
58
+ c_methods > ruby_methods
59
+ end
60
+ end
61
+ end
62
+
63
+ include Pry::Helpers::CommandHelpers
64
+
65
+ class << self
66
+ def lookup(str, _pry_, options = {})
67
+ co = new(str, _pry_, options)
68
+
69
+ co.default_lookup || co.method_or_class_lookup ||
70
+ co.command_lookup || co.empty_lookup
71
+ end
72
+ end
73
+
74
+ attr_accessor :str
75
+ attr_accessor :target
76
+ attr_accessor :_pry_
77
+ attr_accessor :super_level
78
+
79
+ def initialize(str, _pry_, options = {})
80
+ options = {
81
+ super: 0,
82
+ }.merge!(options)
83
+
84
+ @str = str
85
+ @_pry_ = _pry_
86
+ @target = _pry_.current_context
87
+ @super_level = options[:super]
88
+ end
89
+
90
+ def command_lookup
91
+ # TODO: just make it so find_command_by_match_or_listing doesn't
92
+ # raise?
93
+ _pry_.commands.find_command_by_match_or_listing(str) rescue nil
94
+ end
95
+
96
+ # when no paramter is given (i.e CodeObject.lookup(nil)), then we
97
+ # lookup the 'current object' from the binding.
98
+ def empty_lookup
99
+ return nil if str && !str.empty?
100
+
101
+ obj = if internal_binding?(target)
102
+ mod = target_self.is_a?(Module) ? target_self : target_self.class
103
+ Pry::WrappedModule(mod)
104
+ else
105
+ Pry::Method.from_binding(target)
106
+ end
107
+
108
+ # respect the super level (i.e user might have specified a
109
+ # --super flag to show-source)
110
+ lookup_super(obj, super_level)
111
+ end
112
+
113
+ # lookup variables and constants and `self` that are not modules
114
+ def default_lookup
115
+
116
+ # we skip instance methods as we want those to fall through to method_or_class_lookup()
117
+ if safe_to_evaluate?(str) && !looks_like_an_instance_method?(str)
118
+ obj = target.eval(str)
119
+
120
+ # restrict to only objects we KNOW for sure support the full API
121
+ # Do NOT support just any object that responds to source_location
122
+ if sourcable_object?(obj)
123
+ Pry::Method(obj)
124
+ elsif !obj.is_a?(Module)
125
+ Pry::WrappedModule(obj.class)
126
+ else
127
+ nil
128
+ end
129
+ end
130
+
131
+ rescue Pry::RescuableException
132
+ nil
133
+ end
134
+
135
+ def method_or_class_lookup
136
+ obj = case str
137
+ when /\S+\(\)\z/
138
+ Pry::Method.from_str(str.sub(/\(\)\z/, ''),target) || Pry::WrappedModule.from_str(str, target)
139
+ else
140
+ Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
141
+ end
142
+
143
+ lookup_super(obj, super_level)
144
+ end
145
+
146
+ private
147
+
148
+ def sourcable_object?(obj)
149
+ [::Proc, ::Method, ::UnboundMethod].any? { |o| obj.is_a?(o) }
150
+ end
151
+
152
+ # Returns true if `str` looks like a method, i.e Klass#method
153
+ # We need to consider this case because method lookups should fall
154
+ # through to the `method_or_class_lookup()` method but a
155
+ # defined?() on a "Klass#method` string will see the `#` as a
156
+ # comment and only evaluate the `Klass` part.
157
+ # @param [String] str
158
+ # @return [Boolean] Whether the string looks like an instance method.
159
+ def looks_like_an_instance_method?(str)
160
+ str =~ /\S#\S/
161
+ end
162
+
163
+ # We use this method to decide whether code is safe to eval. Method's are
164
+ # generally not, but everything else is.
165
+ # TODO: is just checking != "method" enough??
166
+ # TODO: see duplication of this method in Pry::WrappedModule
167
+ # @param [String] str The string to lookup
168
+ # @return [Boolean]
169
+ def safe_to_evaluate?(str)
170
+ return true if str.strip == "self"
171
+ return false if str =~ /%/
172
+
173
+ kind = target.eval("defined?(#{str})")
174
+ kind =~ /variable|constant/
175
+ end
176
+
177
+ def target_self
178
+ target.eval('self')
179
+ end
180
+
181
+ # grab the nth (`super_level`) super of `obj
182
+ # @param [Object] obj
183
+ # @param [Fixnum] super_level How far up the super chain to ascend.
184
+ def lookup_super(obj, super_level)
185
+ return nil if !obj
186
+
187
+ sup = obj.super(super_level)
188
+ if !sup
189
+ raise Pry::CommandError, "No superclass found for #{obj.wrapped}"
190
+ else
191
+ sup
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,65 @@
1
+ # PP subclass for streaming inspect output in color.
2
+ class Pry
3
+ class ColorPrinter < ::PP
4
+ OBJ_COLOR = begin
5
+ code = CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
6
+ if code.start_with? "\e"
7
+ code
8
+ else
9
+ "\e[0m\e[0;#{code}m"
10
+ end
11
+ end
12
+
13
+ CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"
14
+
15
+ def self.pp(obj, out = $>, width = 79, newline = "\n")
16
+ q = ColorPrinter.new(out, width, newline)
17
+ q.guard_inspect_key { q.pp obj }
18
+ q.flush
19
+ out << "\n"
20
+ end
21
+
22
+ def text(str, width = str.length)
23
+ # Don't recolorize output with color [Issue #751]
24
+ if str.include?("\e[")
25
+ super "#{str}\e[0m", width
26
+ elsif str.start_with?('#<') || str == '=' || str == '>'
27
+ super highlight_object_literal(str), width
28
+ else
29
+ super CodeRay.scan(str, :ruby).term, width
30
+ end
31
+ end
32
+
33
+ def pp(obj)
34
+ if String === obj
35
+ # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
36
+ # Strings prettier
37
+ text(obj.inspect)
38
+ else
39
+ super
40
+ end
41
+ rescue => e
42
+ raise if e.is_a? Pry::Pager::StopPaging
43
+
44
+ begin
45
+ str = obj.inspect
46
+ rescue Exception
47
+ # Read the class name off of the singleton class to provide a default
48
+ # inspect.
49
+ singleton = class << obj; self; end
50
+ ancestors = Pry::Method.safe_send(singleton, :ancestors)
51
+ klass = ancestors.reject { |k| k == singleton }.first
52
+ obj_id = obj.__id__.to_s(16) rescue 0
53
+ str = "#<#{klass}:0x#{obj_id}>"
54
+ end
55
+
56
+ text highlight_object_literal(str)
57
+ end
58
+
59
+ private
60
+
61
+ def highlight_object_literal(object_literal)
62
+ "#{OBJ_COLOR}#{object_literal}\e[0m"
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,723 @@
1
+ require 'delegate'
2
+ require 'pry/helpers/documentation_helpers'
3
+
4
+ class Pry
5
+ # The super-class of all commands, new commands should be created by calling
6
+ # {Pry::CommandSet#command} which creates a BlockCommand or {Pry::CommandSet#create_command}
7
+ # which creates a ClassCommand. Please don't use this class directly.
8
+ class Command
9
+ extend Helpers::DocumentationHelpers
10
+ extend CodeObject::Helpers
11
+
12
+ # represents a void return value for a command
13
+ VOID_VALUE = Object.new
14
+
15
+ # give it a nice inspect
16
+ def VOID_VALUE.inspect() "void" end
17
+
18
+ # Properties of the command itself (as passed as arguments to
19
+ # {CommandSet#command} or {CommandSet#create_command}).
20
+ class << self
21
+ attr_writer :block
22
+ attr_writer :description
23
+ attr_writer :command_options
24
+ attr_writer :match
25
+
26
+ def match(arg = nil)
27
+ if arg
28
+ @command_options ||= default_options(arg)
29
+ @command_options[:listing] = arg.is_a?(String) ? arg : arg.inspect
30
+ @match = arg
31
+ end
32
+ @match ||= nil
33
+ end
34
+
35
+ # Define or get the command's description
36
+ def description(arg = nil)
37
+ @description = arg if arg
38
+ @description ||= nil
39
+ end
40
+
41
+ # Define or get the command's options
42
+ def command_options(arg = nil)
43
+ @command_options ||= default_options(match)
44
+ @command_options.merge!(arg) if arg
45
+ @command_options
46
+ end
47
+ # backward compatibility
48
+ alias_method :options, :command_options
49
+ alias_method :options=, :command_options=
50
+
51
+ # Define or get the command's banner
52
+ def banner(arg = nil)
53
+ @banner = arg if arg
54
+ @banner ||= description
55
+ end
56
+
57
+ def block
58
+ @block || instance_method(:process)
59
+ end
60
+
61
+ def source
62
+ file, line = block.source_location
63
+ strip_leading_whitespace(Pry::Code.from_file(file).expression_at(line))
64
+ end
65
+
66
+ def doc
67
+ new.help
68
+ end
69
+
70
+ def source_file
71
+ Array(block.source_location).first
72
+ end
73
+ alias_method :file, :source_file
74
+
75
+ def source_line
76
+ Array(block.source_location).last
77
+ end
78
+ alias_method :line, :source_line
79
+
80
+ def default_options(match)
81
+ {
82
+ requires_gem: [],
83
+ keep_retval: false,
84
+ argument_required: false,
85
+ interpolate: true,
86
+ shellwords: true,
87
+ listing: (String === match ? match : match.inspect),
88
+ use_prefix: true,
89
+ takes_block: false
90
+ }
91
+ end
92
+ end
93
+
94
+ # Make those properties accessible to instances
95
+ def name; self.class.name; end
96
+
97
+ def match; self.class.match; end
98
+
99
+ def description; self.class.description; end
100
+
101
+ def block; self.class.block; end
102
+
103
+ def command_options; self.class.options; end
104
+
105
+ def command_name; self.class.command_name; end
106
+
107
+ def source; self.class.source; end
108
+
109
+ class << self
110
+ def name
111
+ super.to_s == "" ? "#<class(Pry::Command #{match.inspect})>" : super
112
+ end
113
+
114
+ def inspect
115
+ name
116
+ end
117
+
118
+ def command_name
119
+ self.options[:listing]
120
+ end
121
+
122
+ # Create a new command with the given properties.
123
+ # @param [String, Regex] match The thing that triggers this command
124
+ # @param [String] description The description to appear in `help`
125
+ # @param [Hash] options Behavioral options (see {Pry::CommandSet#command})
126
+ # @param [Module] helpers A module of helper functions to be included.
127
+ # @yield optional, used for BlockCommands
128
+ # @return [Class] (a subclass of {Pry::Command})
129
+ def subclass(match, description, options, helpers, &block)
130
+ klass = Class.new(self)
131
+ klass.send(:include, helpers)
132
+ klass.match = match
133
+ klass.description = description
134
+ klass.command_options = options
135
+ klass.block = block
136
+ klass
137
+ end
138
+
139
+ # Should this command be called for the given line?
140
+ # @param [String] val A line input at the REPL
141
+ # @return [Boolean]
142
+ def matches?(val)
143
+ command_regex =~ val
144
+ end
145
+
146
+ # How well does this command match the given line?
147
+ #
148
+ # Higher scores are better because they imply that this command matches
149
+ # the line more closely.
150
+ #
151
+ # The score is calculated by taking the number of characters at the start
152
+ # of the string that are used only to identify the command, not as part of
153
+ # the arguments.
154
+ #
155
+ # @example
156
+ # /\.(.*)/.match_score(".foo") #=> 1
157
+ # /\.*(.*)/.match_score("...foo") #=> 3
158
+ # 'hi'.match_score("hi there") #=> 2
159
+ #
160
+ # @param [String] val A line input at the REPL
161
+ # @return [Fixnum]
162
+ def match_score(val)
163
+ if command_regex =~ val
164
+ Regexp.last_match.size > 1 ? Regexp.last_match.begin(1) : Regexp.last_match.end(0)
165
+ else
166
+ -1
167
+ end
168
+ end
169
+
170
+ # @deprecated Replaced with {Pry::Hooks#add_hook}. Left for compatibility.
171
+ # Store hooks to be run before or after the command body.
172
+ def hooks
173
+ Pry.hooks
174
+ end
175
+
176
+ def command_regex
177
+ pr = Pry.respond_to?(:config) ? Pry.config.command_prefix : ""
178
+ prefix = convert_to_regex(pr)
179
+ prefix = "(?:#{prefix})?" unless options[:use_prefix]
180
+
181
+ /^#{prefix}#{convert_to_regex(match)}(?!\S)/
182
+ end
183
+
184
+ def convert_to_regex(obj)
185
+ case obj
186
+ when String
187
+ Regexp.escape(obj)
188
+ else
189
+ obj
190
+ end
191
+ end
192
+
193
+ # The group in which the command should be displayed in "help" output.
194
+ # This is usually auto-generated from directory naming, but it can be
195
+ # manually overridden if necessary.
196
+ # Group should not be changed once it is initialized.
197
+ def group(name = nil)
198
+ @group ||= if name
199
+ name
200
+ else
201
+ case Pry::Method(block).source_file
202
+ when %r{/pry/.*_commands/(.*).rb}
203
+ $1.capitalize.gsub(/_/, " ")
204
+ when %r{(pry-\w+)-([\d\.]+([\w\.]+)?)}
205
+ name, version = $1, $2
206
+ "#{name} (v#{version})"
207
+ when /pryrc/
208
+ "pryrc"
209
+ else
210
+ "(other)"
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ # Properties of one execution of a command (passed by {Pry#run_command} as a hash of
217
+ # context and expanded in `#initialize`
218
+ attr_accessor :output
219
+ attr_accessor :target
220
+ attr_accessor :captures
221
+ attr_accessor :eval_string
222
+ attr_accessor :arg_string
223
+ attr_accessor :context
224
+ attr_accessor :command_set
225
+ attr_accessor :hooks
226
+ attr_accessor :_pry_
227
+
228
+ # The block we pass *into* a command so long as `:takes_block` is
229
+ # not equal to `false`
230
+ # @example
231
+ # my-command | do
232
+ # puts "block content"
233
+ # end
234
+ attr_accessor :command_block
235
+
236
+ # Run a command from another command.
237
+ # @param [String] command_string The string that invokes the command
238
+ # @param [Array] args Further arguments to pass to the command
239
+ # @example
240
+ # run "show-input"
241
+ # @example
242
+ # run ".ls"
243
+ # @example
244
+ # run "amend-line", "5", 'puts "hello world"'
245
+ def run(command_string, *args)
246
+ command_string = _pry_.config.command_prefix.to_s + command_string
247
+ complete_string = "#{command_string} #{args.join(" ")}".rstrip
248
+ command_set.process_line(complete_string, context)
249
+ end
250
+
251
+ def commands
252
+ command_set.to_hash
253
+ end
254
+
255
+ #
256
+ # @deprecated
257
+ # Please use black(), white(), etc directly instead (as you would with helper
258
+ # functions from BaseHelpers and CommandHelpers)
259
+ #
260
+ # @return [Module]
261
+ # Returns Pry::Helpers::Text
262
+ #
263
+ def text
264
+ Pry::Helpers::Text
265
+ end
266
+
267
+ def void
268
+ VOID_VALUE
269
+ end
270
+
271
+ include Pry::Helpers::BaseHelpers
272
+ include Pry::Helpers::CommandHelpers
273
+ include Pry::Helpers::Text
274
+
275
+ # Instantiate a command, in preparation for calling it.
276
+ # @param [Hash] context The runtime context to use with this command.
277
+ def initialize(context = {})
278
+ self.context = context
279
+ self.target = context[:target]
280
+ self.output = context[:output]
281
+ self.eval_string = context[:eval_string]
282
+ self.command_set = context[:command_set]
283
+ self.hooks = context[:hooks]
284
+ self._pry_ = context[:pry_instance]
285
+ end
286
+
287
+ # @return [Object] The value of `self` inside the `target` binding.
288
+ def target_self; target.eval('self'); end
289
+
290
+ # @return [Hash] Pry commands can store arbitrary state
291
+ # here. This state persists between subsequent command invocations.
292
+ # All state saved here is unique to the command, it does not
293
+ # need to be namespaced.
294
+ # @example
295
+ # state.my_state = "my state" # this will not conflict with any
296
+ # # `state.my_state` used in another command.
297
+ def state
298
+ _pry_.command_state[match] ||= Pry::Config.from_hash({})
299
+ end
300
+
301
+ # Revaluate the string (str) and perform interpolation.
302
+ # @param [String] str The string to reevaluate with interpolation.
303
+ #
304
+ # @return [String] The reevaluated string with interpolations
305
+ # applied (if any).
306
+ def interpolate_string(str)
307
+ dumped_str = str.dump
308
+ if dumped_str.gsub!(/\\\#\{/, '#{')
309
+ target.eval(dumped_str)
310
+ else
311
+ str
312
+ end
313
+ end
314
+
315
+ # Display a warning if a command collides with a local/method in
316
+ # the current scope.
317
+ def check_for_command_collision(command_match, arg_string)
318
+ collision_type = target.eval("defined?(#{command_match})")
319
+ collision_type ||= 'local-variable' if arg_string.match(%r{\A\s*[-+*/%&|^]*=})
320
+
321
+ if collision_type
322
+ output.puts "#{text.bold('WARNING:')} Calling Pry command '#{command_match}', which conflicts with a #{collision_type}.\n\n"
323
+ end
324
+ rescue Pry::RescuableException
325
+ end
326
+
327
+ # Extract necessary information from a line that Command.matches? this
328
+ # command.
329
+ #
330
+ # Returns an array of four elements:
331
+ #
332
+ # ```
333
+ # [String] the portion of the line that matched with the Command match
334
+ # [String] a string of all the arguments (i.e. everything but the match)
335
+ # [Array] the captures caught by the command_regex
336
+ # [Array] the arguments obtained by splitting the arg_string
337
+ # ```
338
+ #
339
+ # @param [String] val The line of input
340
+ # @return [Array]
341
+ def tokenize(val)
342
+ val.replace(interpolate_string(val)) if command_options[:interpolate]
343
+
344
+ self.class.command_regex =~ val
345
+
346
+ # please call Command.matches? before Command#call_safely
347
+ raise CommandError, "fatal: called a command which didn't match?!" unless Regexp.last_match
348
+
349
+ captures = Regexp.last_match.captures
350
+ pos = Regexp.last_match.end(0)
351
+
352
+ arg_string = val[pos..-1]
353
+
354
+ # remove the one leading space if it exists
355
+ arg_string.slice!(0) if arg_string.start_with?(" ")
356
+
357
+ # process and pass a block if one is found
358
+ pass_block(arg_string) if command_options[:takes_block]
359
+
360
+ if arg_string
361
+ args = command_options[:shellwords] ? Shellwords.shellwords(arg_string) : arg_string.split(" ")
362
+ else
363
+ args = []
364
+ end
365
+
366
+ [val[0..pos].rstrip, arg_string, captures, args]
367
+ end
368
+
369
+ # Process a line that Command.matches? this command.
370
+ # @param [String] line The line to process
371
+ # @return [Object, Command::VOID_VALUE]
372
+ def process_line(line)
373
+ command_match, arg_string, captures, args = tokenize(line)
374
+
375
+ check_for_command_collision(command_match, arg_string) if Pry.config.collision_warning
376
+
377
+ self.arg_string = arg_string
378
+ self.captures = captures
379
+
380
+ call_safely(*(captures + args))
381
+ end
382
+
383
+ # Pass a block argument to a command.
384
+ # @param [String] arg_string The arguments (as a string) passed to the command.
385
+ # We inspect these for a '| do' or a '| {' and if we find it we use it
386
+ # to start a block input sequence. Once we have a complete
387
+ # block, we save it to an accessor that can be retrieved from the command context.
388
+ # Note that if we find the '| do' or '| {' we delete this and the
389
+ # elements following it from `arg_string`.
390
+ def pass_block(arg_string)
391
+ # Workaround for weird JRuby bug where rindex in this case can return nil
392
+ # even when there's a match.
393
+ arg_string.scan(/\| *(?:do|\{)/)
394
+ block_index = $~ && $~.offset(0)[0]
395
+
396
+ return if !block_index
397
+
398
+ block_init_string = arg_string.slice!(block_index..-1)[1..-1]
399
+ prime_string = "proc #{block_init_string}\n"
400
+
401
+ if !Pry::Code.complete_expression?(prime_string)
402
+ block_string = _pry_.r(target, prime_string)
403
+ else
404
+ block_string = prime_string
405
+ end
406
+
407
+ begin
408
+ self.command_block = target.eval(block_string)
409
+ rescue Pry::RescuableException
410
+ raise CommandError, "Incomplete block definition."
411
+ end
412
+ end
413
+
414
+ private :pass_block
415
+
416
+ # Run the command with the given `args`.
417
+ #
418
+ # This is a public wrapper around `#call` which ensures all preconditions
419
+ # are met.
420
+ #
421
+ # @param [Array<String>] args The arguments to pass to this command.
422
+ # @return [Object] The return value of the `#call` method, or
423
+ # {Command::VOID_VALUE}.
424
+ def call_safely(*args)
425
+ unless dependencies_met?
426
+ gems_needed = Array(command_options[:requires_gem])
427
+ gems_not_installed = gems_needed.select { |g| !Rubygem.installed?(g) }
428
+ output.puts "\nThe command '#{command_name}' is #{text.bold("unavailable")} because it requires the following gems to be installed: #{(gems_not_installed.join(", "))}"
429
+ output.puts "-"
430
+ output.puts "Type `install-command #{command_name}` to install the required gems and activate this command."
431
+ return void
432
+ end
433
+
434
+ if command_options[:argument_required] && args.empty?
435
+ raise CommandError, "The command '#{command_name}' requires an argument."
436
+ end
437
+
438
+ ret = use_unpatched_symbol do
439
+ call_with_hooks(*args)
440
+ end
441
+ command_options[:keep_retval] ? ret : void
442
+ end
443
+
444
+ def use_unpatched_symbol
445
+ call_method = Symbol.method_defined?(:call) && Symbol.instance_method(:call)
446
+ Symbol.class_eval { undef :call } if call_method
447
+ yield
448
+ ensure
449
+ Symbol.instance_eval { define_method(:call, call_method) } if call_method
450
+ end
451
+
452
+ # Are all the gems required to use this command installed?
453
+ #
454
+ # @return Boolean
455
+ def dependencies_met?
456
+ @dependencies_met ||= command_dependencies_met?(command_options)
457
+ end
458
+
459
+ # Generate completions for this command
460
+ #
461
+ # @param [String] _search The line typed so far
462
+ # @return [Array<String>] Completion words
463
+ def complete(_search)
464
+ []
465
+ end
466
+
467
+ private
468
+
469
+ def find_hooks(event)
470
+ event_name = "#{event}_#{command_name}"
471
+ (self.hooks || self.class.hooks).get_hooks(event_name).values
472
+ end
473
+
474
+ def before_hooks
475
+ find_hooks('before')
476
+ end
477
+
478
+ def after_hooks
479
+ find_hooks('after')
480
+ end
481
+
482
+ # Run the `#call` method and all the registered hooks.
483
+ # @param [Array<String>] args The arguments to `#call`
484
+ # @return [Object] The return value from `#call`
485
+ def call_with_hooks(*args)
486
+ before_hooks.each { |block| instance_exec(*args, &block) }
487
+
488
+ ret = call(*args)
489
+
490
+ after_hooks.each do |block|
491
+ ret = instance_exec(*args, &block)
492
+ end
493
+
494
+ ret
495
+ end
496
+
497
+ # Fix the number of arguments we pass to a block to avoid arity warnings.
498
+ # @param [Fixnum] arity The arity of the block
499
+ # @param [Array] args The arguments to pass
500
+ # @return [Array] A (possibly shorter) array of the arguments to pass
501
+ def correct_arg_arity(arity, args)
502
+ case
503
+ when arity < 0
504
+ args
505
+ when arity == 0
506
+ []
507
+ when arity > 0
508
+ args.values_at(*(0..(arity - 1)).to_a)
509
+ end
510
+ end
511
+ end
512
+
513
+ # A super-class for Commands that are created with a single block.
514
+ #
515
+ # This class ensures that the block is called with the correct number of arguments
516
+ # and the right context.
517
+ #
518
+ # Create subclasses using {Pry::CommandSet#command}.
519
+ class BlockCommand < Command
520
+ # backwards compatibility
521
+ alias_method :opts, :context
522
+
523
+ # Call the block that was registered with this command.
524
+ # @param [Array<String>] args The arguments passed
525
+ # @return [Object] The return value of the block
526
+ def call(*args)
527
+ instance_exec(*correct_arg_arity(block.arity, args), &block)
528
+ end
529
+
530
+ def help
531
+ "#{command_options[:listing].to_s.ljust(18)} #{description}"
532
+ end
533
+ end
534
+
535
+ # A super-class of Commands with structure.
536
+ #
537
+ # This class implements the bare-minimum functionality that a command should
538
+ # have, namely a --help switch, and then delegates actual processing to its
539
+ # subclasses.
540
+ #
541
+ # Create subclasses using {Pry::CommandSet#create_command}, and override the
542
+ # `options(opt)` method to set up an instance of Pry::Slop, and the `process`
543
+ # method to actually run the command. If necessary, you can also override
544
+ # `setup` which will be called before `options`, for example to require any
545
+ # gems your command needs to run, or to set up state.
546
+ class ClassCommand < Command
547
+ class << self
548
+ # Ensure that subclasses inherit the options, description and
549
+ # match from a ClassCommand super class.
550
+ def inherited(klass)
551
+ klass.match match
552
+ klass.description description
553
+ klass.command_options options
554
+ end
555
+
556
+ def source
557
+ source_object.source
558
+ end
559
+
560
+ def doc
561
+ new.help
562
+ end
563
+
564
+ def source_location
565
+ source_object.source_location
566
+ end
567
+
568
+ def source_file
569
+ source_object.source_file
570
+ end
571
+ alias_method :file, :source_file
572
+
573
+ def source_line
574
+ source_object.source_line
575
+ end
576
+ alias_method :line, :source_line
577
+
578
+ private
579
+
580
+ # The object used to extract the source for the command.
581
+ #
582
+ # This should be a `Pry::Method(block)` for a command made with `create_command`
583
+ # and a `Pry::WrappedModule(self)` for a command that's a standard class.
584
+ # @return [Pry::WrappedModule, Pry::Method]
585
+ def source_object
586
+ @source_object ||= if name =~ /^[A-Z]/
587
+ Pry::WrappedModule(self)
588
+ else
589
+ Pry::Method(block)
590
+ end
591
+ end
592
+ end
593
+
594
+ attr_accessor :opts
595
+ attr_accessor :args
596
+
597
+ # Set up `opts` and `args`, and then call `process`.
598
+ #
599
+ # This method will display help if necessary.
600
+ #
601
+ # @param [Array<String>] args The arguments passed
602
+ # @return [Object] The return value of `process` or VOID_VALUE
603
+ def call(*args)
604
+ setup
605
+
606
+ self.opts = slop
607
+ self.args = self.opts.parse!(args)
608
+
609
+ if opts.present?(:help)
610
+ output.puts slop.help
611
+ void
612
+ else
613
+ process(*correct_arg_arity(method(:process).arity, args))
614
+ end
615
+ end
616
+
617
+ # Return the help generated by Pry::Slop for this command.
618
+ def help
619
+ slop.help
620
+ end
621
+
622
+ # Return an instance of Pry::Slop that can parse either subcommands or the
623
+ # options that this command accepts.
624
+ def slop
625
+ Pry::Slop.new do |opt|
626
+ opt.banner(unindent(self.class.banner))
627
+ subcommands(opt)
628
+ options(opt)
629
+ opt.on :h, :help, 'Show this message.'
630
+ end
631
+ end
632
+
633
+ # Generate shell completions
634
+ # @param [String] search The line typed so far
635
+ # @return [Array<String>] the words to complete
636
+ def complete(search)
637
+ slop.flat_map do |opt|
638
+ [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
639
+ end.compact + super
640
+ end
641
+
642
+ # A method called just before `options(opt)` as part of `call`.
643
+ #
644
+ # This method can be used to set up any context your command needs to run,
645
+ # for example requiring gems, or setting default values for options.
646
+ #
647
+ # @example
648
+ # def setup
649
+ # require 'gist'
650
+ # @action = :method
651
+ # end
652
+ def setup; end
653
+
654
+ # A method to setup Pry::Slop commands so it can parse the subcommands your
655
+ # command expects. If you need to set up default values, use `setup`
656
+ # instead.
657
+ #
658
+ # @example A minimal example
659
+ # def subcommands(cmd)
660
+ # cmd.command :download do |opt|
661
+ # description 'Downloads a content from a server'
662
+ #
663
+ # opt.on :verbose, 'Use verbose output'
664
+ #
665
+ # run do |options, arguments|
666
+ # ContentDownloader.download(options, arguments)
667
+ # end
668
+ # end
669
+ # end
670
+ #
671
+ # @example Define the invokation block anywhere you want
672
+ # def subcommands(cmd)
673
+ # cmd.command :download do |opt|
674
+ # description 'Downloads a content from a server'
675
+ #
676
+ # opt.on :verbose, 'Use verbose output'
677
+ # end
678
+ # end
679
+ #
680
+ # def process
681
+ # # Perform calculations...
682
+ # opts.fetch_command(:download).run do |options, arguments|
683
+ # ContentDownloader.download(options, arguments)
684
+ # end
685
+ # # More calculations...
686
+ # end
687
+ def subcommands(cmd); end
688
+
689
+ # A method to setup Pry::Slop so it can parse the options your command expects.
690
+ #
691
+ # @note Please don't do anything side-effecty in the main part of this
692
+ # method, as it may be called by Pry at any time for introspection reasons.
693
+ # If you need to set up default values, use `setup` instead.
694
+ #
695
+ # @example
696
+ # def options(opt)
697
+ # opt.banner "Gists methods or classes"
698
+ # opt.on(:c, :class, "gist a class") do
699
+ # @action = :class
700
+ # end
701
+ # end
702
+ def options(opt); end
703
+
704
+ # The actual body of your command should go here.
705
+ #
706
+ # The `opts` mehod can be called to get the options that Pry::Slop has passed,
707
+ # and `args` gives the remaining, unparsed arguments.
708
+ #
709
+ # The return value of this method is discarded unless the command was
710
+ # created with `:keep_retval => true`, in which case it is returned to the
711
+ # repl.
712
+ #
713
+ # @example
714
+ # def process
715
+ # if opts.present?(:class)
716
+ # gist_class
717
+ # else
718
+ # gist_method
719
+ # end
720
+ # end
721
+ def process; raise CommandError, "command '#{command_name}' not implemented" end
722
+ end
723
+ end