Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Polly Want A Message (DeconstructConf)

Polly Want A Message (DeconstructConf)

Object-oriented languages have opinions about how best to arrange code. These opinions lead OO to naturally offer certain _affordances_. Just as round doorknobs expect to be grasped and rotated, OO expects messages, polymorphism, loose-coupling and factories. The key to creating intuitive and maintainable OO applications is to understand and embrace these built-in affordances.

Sandi Metz

May 21, 2018
Tweet

More Decks by Sandi Metz

Other Decks in Programming

Transcript

  1. @sandimetz May 2018 #1 Design Stamina Hypothesis #2 Procedures vs

    OO #3 Churn vs Complexity #4 What matters, suffers
  2. @sandimetz May 2018 class GitCmd attr_accessor :repository, :tagname, :filename def

    show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end
  3. @sandimetz May 2018 class GitCmd attr_accessor :repository, :tagname, :filename def

    show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end
  4. @sandimetz May 2018 class GitCmd attr_accessor :repository, :tagname, :filename def

    show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end
  5. @sandimetz May 2018 class GitCmd attr_accessor :repository, :tagname, :filename def

    show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end
  6. @sandimetz May 2018 if git_cmd git_cmd.repository = repository git_cmd.tagname =

    tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end
  7. @sandimetz May 2018 if git_cmd git_cmd.repository = repository git_cmd.tagname =

    tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end
  8. @sandimetz May 2018 if git_cmd git_cmd.repository = repository git_cmd.tagname =

    tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end
  9. @sandimetz May 2018 def lines if git_cmd git_cmd.repository = repository

    git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end
  10. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end
  11. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end
  12. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end
  13. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end
  14. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end
  15. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end # ... end
  16. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end # ... end
  17. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end # ... end
  18. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end # ... end
  19. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end # ... end
  20. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end
  21. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end New Conditional Here
  22. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end
  23. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end
  24. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_lines else File.read(filename).split("\n") end end def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end
  25. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_lines else File.read(filename).split("\n") end end
  26. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_lines else File.read(filename).split("\n") end end
  27. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_lines else File.read(filename).split("\n") end end
  28. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_lines else File.read(filename).split("\n") end end def file_lines File.read(filename).split("\n") end
  29. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_lines else file_lines end end def file_lines File.read(filename).split("\n") end
  30. @sandimetz May 2018 class Listing # ... def lines if

    git_cmd git_lines else file_lines end end
  31. @sandimetz May 2018 class Listing # ... def lines all_lines

    = if git_cmd git_lines else file_lines end end
  32. @sandimetz May 2018 class Listing # ... def lines all_lines

    = if git_cmd git_lines else file_lines end end
  33. @sandimetz May 2018 class Listing # ... def lines all_lines

    = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end
  34. @sandimetz May 2018 class Listing # ... def lines all_lines

    = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end
  35. @sandimetz May 2018 # "1, 3-4, 15, 37-50" def lines_to_print(all_lines)

    specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  36. @sandimetz May 2018 # "1, 3-4, 15, 37-50" def lines_to_print(all_lines)

    specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  37. @sandimetz May 2018 # "1, 3-4, 15, 37-50" def lines_to_print(all_lines)

    specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  38. @sandimetz May 2018 # "1, 3-4, 15, 37-50" def lines_to_print(all_lines)

    specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  39. @sandimetz May 2018 # "1, 3-4, 15, 37-50" def lines_to_print(all_lines)

    specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  40. @sandimetz May 2018 # "1, 3-4, 15, 37-50" def lines_to_print(all_lines)

    specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end Ouch
  41. @sandimetz May 2018 # "1, 3-4, 15, 37-50" def lines_to_print(all_lines)

    specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  42. @sandimetz May 2018 # "1, 3-4, 15, 37-50" def lines_to_print(all_lines)

    specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  43. @sandimetz May 2018 # "1, #4, 3-4, #6, 15, 37-50"

    def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  44. @sandimetz May 2018 # "1, #4, 3-4, #6, 15, 37-50"

    def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  45. @sandimetz May 2018 # "1, #4, 3-4, #6, 15, 37-50"

    def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end New Conditional Here
  46. @sandimetz May 2018 # "1, #4, 3-4, #6, 15, 37-50"

    def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end New Conditional Here
  47. @sandimetz May 2018 # "1, #4, 3-4, #6, 15, 37-50"

    def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  48. @sandimetz May 2018 # "1, #4, 3-4, #6, 15, 37-50"

    def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end
  49. @sandimetz May 2018 # "1, #4, 3-4, #6, 15, 37-50"

    def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') else edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end
  50. @sandimetz May 2018 # "1, #4, 3-4, #6, 15, 37-50"

    def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end
  51. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end # ...
  52. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end # ...
  53. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end # ...
  54. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end # ...
  55. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end # ...
  56. @sandimetz May 2018 class Listing # ... def lines all_lines

    = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end
  57. @sandimetz May 2018 class Listing # ... def lines all_lines

    = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end Yup
  58. @sandimetz May 2018 def lines all_lines = if git_cmd git_lines

    else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end
  59. @sandimetz May 2018 def lines all_lines = if git_cmd git_lines

    else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end
  60. @sandimetz May 2018 def lines all_lines = if git_cmd git_lines

    else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end
  61. @sandimetz May 2018 def lines all_lines = if git_cmd git_lines

    else file_lines end if line_numbers lines_to_print(all_lines) else all_lines end end
  62. @sandimetz May 2018 def lines all_lines = if git_cmd git_lines

    else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end end
  63. @sandimetz May 2018 def lines all_lines = if git_cmd git_lines

    else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end end
  64. @sandimetz May 2018 def lines all_lines = if git_cmd git_lines

    else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  65. @sandimetz May 2018 def lines all_lines = if git_cmd git_lines

    else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  66. @sandimetz May 2018 def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || ""}

    end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end
  67. @sandimetz May 2018 def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || ""}

    end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end
  68. @sandimetz May 2018 def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || ""}

    end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end
  69. @sandimetz May 2018 def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || ""}

    end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end Ouch
  70. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 1 Lines: 29
  71. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 1
  72. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 1 Execution Paths: 2
  73. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 1
  74. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 2 Lines: 50
  75. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 2 Execution Paths:
  76. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 2 Execution Paths: 4
  77. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end class Listing attr_reader :filename, :line_numbers, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 1 2
  78. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 3 Lines: 55
  79. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 3 Execution Paths:
  80. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :repository, :tag,

    :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 3 Execution Paths: 8
  81. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end class Listing attr_reader :filename, :line_numbers, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 1 2 class Listing attr_reader :filename, :line_numbers, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 3
  82. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling 4 Lines: 83
  83. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling 4 Execution Paths:
  84. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling 4 Execution Paths: 16
  85. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end class Listing attr_reader :filename, :line_numbers, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 1 2 class Listing attr_reader :filename, :line_numbers, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 3 class Listing attr_reader :filename, :line_numbers, :left_just, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling 4
  86. @sandimetz May 2018 "Doorknob" by photonooner CC BY-NC-ND "Door Handle"

    by www.trek.today CC BY-NC-ND "pull" by various brennemans CC BY-SA
  87. @sandimetz May 2018 "pull" by greenkozi CC BY-NC-ND I'm a

    loser and lost the attribution. Sorry.
  88. @sandimetz May 2018 "pull" by greenkozi CC BY-NC-ND "door push

    plate" by stu_spivack CC BY-SA I'm a loser and lost the attribution. Sorry.
  89. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  90. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end 2
  91. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end 2 2x2
  92. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end 2 2x2 2x2x2
  93. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#"). (" " * num_spaces) + "# ..." else edges = spec.split('-').collec range_of_line_numbers = (edges range_of_line_numbers.collect end }.flatten.compact end 2 2x2 2x2x2
  94. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#"). (" " * num_spaces) + "# ..." else edges = spec.split('-').collec range_of_line_numbers = (edges range_of_line_numbers.collect end }.flatten.compact end 2 2x2 2x2x2 2x2x2x2
  95. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  96. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end If you know enough to inject this
  97. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end Inject a smarter thing
  98. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end 1 File or Git Tag
  99. @sandimetz May 2018 class Listing def lines all_lines = if

    git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end 1 Role = Source
  100. @sandimetz May 2018 class FileSource def lines end # ...

    end class GitTagSource def lines end # ... end S O U R C E S
  101. @sandimetz May 2018 class FileSource def lines end # ...

    end class GitTagSource def lines end # ... end S O U R C E S
  102. @sandimetz May 2018 class FileSource def lines end # ...

    end class GitTagSource def lines end # ... end S O U R C E S Polymorphism
  103. @sandimetz May 2018 class FileSource def lines File.read(filename).split("\n") end #

    ... end class GitTagSource def lines end # ... end S O U R C E S
  104. @sandimetz May 2018 class FileSource def lines File.read(filename).split("\n") end #

    ... end class GitTagSource def lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end # ... end S O U R C E S
  105. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ... def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end
  106. @sandimetz May 2018 def git_lines git_cmd.repository = repository git_cmd.tagname =

    tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end v v v class Listing attr_reader :filename, :line_numbers, :left_just, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  107. @sandimetz May 2018 class Listing attr_reader :line_numbers, :left_just def initialize(

    line_numbers: nil, left_justify: false) @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  108. @sandimetz May 2018 class Listing attr_reader :line_numbers, :left_just def initialize(

    line_numbers: nil, left_justify: false) @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  109. @sandimetz May 2018 class Listing attr_reader :line_numbers, :left_just def initialize(

    line_numbers: nil, left_justify: false) @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  110. @sandimetz May 2018 class Listing attr_reader :line_numbers, :left_just def initialize(

    line_numbers: nil, left_justify: false) @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ... Inject a Source
  111. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  112. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  113. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  114. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  115. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  116. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = if git_cmd git_lines else file_lines end subset = # ...
  117. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = # ...
  118. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = # ...
  119. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = # ... Execution Paths: 16?
  120. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = # ... Execution Paths: 8
  121. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset =
  122. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  123. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  124. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end 2 All or Some Lines
  125. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end 2 Subset
  126. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end Inject a smarter thing
  127. @sandimetz May 2018 module Subset class Everything; end class LineNumber

    # ... def lines_to_print(possibilities) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ / specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..ed range_of_line_numbers.collect {|i| all_line end }.flatten.compact end end S U B S E T
  128. @sandimetz May 2018 module Subset class Everything; end class LineNumber

    # ... def lines(possibilities) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ / specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..ed range_of_line_numbers.collect {|i| all_line end }.flatten.compact end end S U B S E T
  129. @sandimetz May 2018 module Subset class Everything; end class LineNumber

    # ... def lines(possibilities) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ / specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..ed range_of_line_numbers.collect {|i| all_line end }.flatten.compact end end S U B S E T ?
  130. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end def lines_to_print(all_lines) specs = line_numbers.gsub(/[ specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete (" " * num_spaces) + "# else edges = spec.split('-'). range_of_line_numbers = range_of_line_numbers.co end }.flatten.compact end
  131. @sandimetz May 2018 class Listing attr_reader :source, :line_numbers, :left_just def

    initialize(source:, line_numbers: nil, left_justify: false) @source = source @line_numbers = line_numbers @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end def lines_to_print(all_lines) specs = line_numbers.gsub(/[ specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete (" " * num_spaces) + "# else edges = spec.split('-'). range_of_line_numbers = range_of_line_numbers.co end }.flatten.compact end
  132. @sandimetz May 2018 class Listing attr_reader :source, :left_just def initialize(source:,

    left_justify: false) @source = source @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  133. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  134. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  135. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  136. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  137. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  138. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines all_lines = source.lines subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end
  139. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines all_lines = source.lines subset = subsetter.lines(all_lines) if left_just return justify(subset) end subset end
  140. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines all_lines = source.lines subset = subsetter.lines(all_lines) if left_just return justify(subset) end subset end
  141. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end
  142. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end Execution Paths: 4
  143. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end
  144. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end
  145. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end 4 Raw or Left Just
  146. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end 4 Justifier
  147. @sandimetz May 2018 module Justification class None def justify(lines) lines

    end end class BlockLeft def justify(lines) lines.map {|line| line.slice(num_leading_spaces end def num_leading_spaces_to_remove; end def num_leading_spaces(line); end end end J U S T I F I E R
  148. @sandimetz May 2018 module Justification class None def justify(lines) lines

    end end class BlockLeft def justify(lines) lines.map {|line| line.slice(num_leading_spaces end def num_leading_spaces_to_remove; end def num_leading_spaces(line); end end end J U S T I F I E R
  149. @sandimetz May 2018 module Justification class None def justify(lines) lines

    end end class BlockLeft def justify(lines) lines.map {|line| line.slice(num_leading_spaces end def num_leading_spaces_to_remove; end def num_leading_spaces(line); end end end J U S T I F I E R
  150. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end end
  151. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :left_just def

    initialize(source:, subsetter:, left_justify: false) @source = source @subsetter = subsetter @left_just = left_justify end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end end
  152. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end end
  153. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines subset = subsetter.lines(source.lines) if left_just return justify(subset) end subset end end
  154. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines subset = subsetter.lines(source.lines) justifier.justify(subset) end end
  155. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines subset = subsetter.lines(source.lines) justifier.justify(subset) end end
  156. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines subset = subsetter.lines(source.lines) justifier.justify(subsetter.lines(source.lines)) end end
  157. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end
  158. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end
  159. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end 3 Code Line or Comment
  160. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end module Subset class LineNumber def lines(possibilities) specs = line_numbers.gsub(/[‘|’]/, " specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to (" " * num_spaces) + "# ..." else edges = spec.split('-').collect( range_of_line_numbers = (edges.m range_of_line_numbers.collect {| end }.flatten.compact end 3 Code Line or Comment
  161. @sandimetz May 2018 class Listing attr_reader :source, :subsetter, :justifier def

    initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end module Subset class LineNumber def lines(possibilities) specs = line_numbers.gsub(/[‘|’]/, " specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to (" " * num_spaces) + "# ..." else edges = spec.split('-').collect( range_of_line_numbers = (edges.m range_of_line_numbers.collect {| end }.flatten.compact end 3 Clump
  162. @sandimetz May 2018 class Clump # initialize with spec and

    input class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.c end def expand_clump(spec) # ... end end end C L U M P
  163. @sandimetz May 2018 class Clump # initialize with spec and

    input class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.c end def expand_clump(spec) # ... end end end C L U M P
  164. @sandimetz May 2018 class Clump # initialize with spec and

    input class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.c end def expand_clump(spec) # ... end end end C L U M P
  165. @sandimetz May 2018 class Clump # initialize with spec and

    input class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.c end def expand_clump(spec) # ... end end end C L U M P
  166. @sandimetz May 2018 class Clump # initialize with spec and

    input class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.c end def expand_clump(spec) # ... end end end C L U M P
  167. @sandimetz May 2018 class Clump # initialize with spec and

    input class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.c end def expand_clump(spec) # ... end end end C L U M P
  168. @sandimetz May 2018 class Clump # initialize with spec and

    input class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.c end def expand_clump(spec) # ... end end end C L U M P
  169. @sandimetz May 2018 class Clump # initialize with spec and

    input class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.c end def expand_clump(spec) # ... end end end C L U M P
  170. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end S U B S E T
  171. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end S U B S E T
  172. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') else end }.flatten.compact end S U B S E T
  173. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') else end }.flatten.compact end S U B S E T
  174. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else end }.flatten.compact end S U B S E T
  175. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end S U B S E T
  176. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end Polymorphic S U B S E T
  177. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end Role-playing S U B S E T
  178. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end Rule ?? S U B S E T
  179. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end Tightly-coupled S U B S E T
  180. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end S U B S E T
  181. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end Factory! S U B S E T
  182. @sandimetz May 2018 class Clump def self.for(spec:, possibilities: []) if

    spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end # ... end C L U M P
  183. @sandimetz May 2018 class Clump def self.for(spec:, possibilities: []) if

    spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end # ... end C L U M P
  184. @sandimetz May 2018 class Clump def self.for(spec:, possibilities: []) if

    spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end # ... end C L U M P
  185. @sandimetz May 2018 class Clump def self.lines(spec:, possibilities: []) self.for(spec:

    spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end # ... end C L U M P
  186. @sandimetz May 2018 class Clump def self.lines(spec:, possibilities: []) self.for(spec:

    spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end # ... end C L U M P Factory-created
  187. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end S U B S E T
  188. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') Clump::Comment.new(...).lines else Clump::LineNumber.new(...).lines end }.flatten.compact end S U B S E T
  189. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| }.flatten.compact end S U B S E T
  190. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| Clump.lines(spec: spec, possibilities: possibilities) }.flatten.compact end S U B S E T
  191. @sandimetz May 2018 module Subset class LineNumber def lines(possibilities) specs

    = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| Clump.lines(spec: spec, possibilities: possibilities) }.flatten.compact end S U B S E T
  192. @sandimetz May 2018 class Listing attr_reader :filename, :repository, :tag, :git_cmd

    def initialize(filename:, repository: nil, tag: nil, git_cmd: nil) @filename = filename @repository = repository @tag = tag @git_cmd = git_cmd end def lines if git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") else File.read(filename).split("\n") end end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end class Listing attr_reader :filename, :line_numbers, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| edges = spec.split('-').collect(&:to_i) individual_numbers = (edges.min.to_i..edges.max.to_i).to_a individual_numbers.collect {|i| all_lines[i - 1]}.compact }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 1 2 class Listing attr_reader :filename, :line_numbers, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end if line_numbers return lines_to_print(all_lines) end all_lines end private def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end 3 class Listing attr_reader :filename, :line_numbers, :left_just, :repository, :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling 4
  193. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling Lines: 83 Execution Paths: 16 Procedure
  194. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling Lines: 83 Execution Paths: 16 Procedure OO
  195. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling Lines: 83 Execution Paths: 16 Procedure module Source class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end Source: File GitTag OO
  196. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling Lines: 83 Execution Paths: 16 Procedure module Source class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Everything LineNumber Source:
  197. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling Lines: 83 Execution Paths: 16 Procedure module Source class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: Comment LineNumber
  198. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling Lines: 83 Execution Paths: 16 Procedure module Source class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_spec(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: None BlockLeft
  199. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling Lines: 83 Execution Paths: 16 Procedure module Source class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end
  200. @sandimetz May 2018 class Listing attr_reader :filename, :line_numbers, :left_just, :repository,

    :tag, :git_cmd def initialize(filename:, line_numbers: nil, left_justify: false, repository: nil, tag: nil, git_cmd: nil) @filename = filename @line_numbers = line_numbers @left_just = left_justify @repository = repository @tag = tag @git_cmd = git_cmd end def lines all_lines = if git_cmd git_lines else file_lines end subset = if line_numbers lines_to_print(all_lines) else all_lines end if left_just return justify(subset) end subset end private #################### # Reading #################### def git_lines git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename git_cmd.show.split("\n") end def file_lines File.read(filename).split("\n") end #################### # Subsetting #################### def lines_to_print(all_lines) specs = line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") specs.collect {|spec| if spec.include?('#') num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." else edges = spec.split('-').collect(&:to_i) range_of_line_numbers = (edges.min.to_i..edges.max.to_i).to_a range_of_line_numbers.collect {|i| all_lines[i - 1]}.compact end }.flatten.compact end #################### # Justification #################### def justify(lines) lines.map {|line| line.slice(num_leading_spaces_to_remove(lines)..-1) || "" } end def num_leading_spaces_to_remove(lines) @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end private def git_dir %Q[--git-dir="#{repository}"] end end # plus 90+ lines of error handling Lines: 83 Execution Paths: 16 Procedure module Source class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories
  201. @sandimetz May 2018 Lines: 83 Execution Paths: 16 Procedure module

    Source class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories
  202. @sandimetz May 2018 Lines: 83 Execution Paths: 16 module Source

    class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories Procedure
  203. @sandimetz May 2018 Lines: 83 Execution Paths: 16 module Source

    class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories Procedure Total Lines: 134
  204. @sandimetz May 2018 Lines: 83 Execution Paths: 16 module Source

    class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories Procedure Total Lines: 134 Execution Paths: 1
  205. @sandimetz May 2018 Lines: 83 Execution Paths: 16 module Source

    class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories Procedure Total Lines: 134 Execution Paths: 1
  206. @sandimetz May 2018 Lines: 83 Execution Paths: 16 module Source

    class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories Procedure Classes: 9 Total Lines: 134 Execution Paths: 1
  207. @sandimetz May 2018 Lines: 83 Execution Paths: 16 module Source

    class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories Procedure Classes: 9 Biggest: 18 loc Total Lines: 134 Execution Paths: 1
  208. @sandimetz May 2018 Lines: 83 Execution Paths: 16 module Source

    class File attr_reader :filename def initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories Procedure
  209. @sandimetz May 2018 class Clump def self.for(spec:, possibilities: []) if

    spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end # ... end
  210. @sandimetz May 2018 class Clump def self.for(spec:, possibilities: []) if

    spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end # ... end Factories are shared Factories are isolated Factories are simple Factories are ignorable
  211. @sandimetz May 2018 module Source class File attr_reader :filename def

    initialize(filename:) @filename = filename end def lines ::File.read(filename).split("\n") end end class GitTag def self.git_cmd GitCmd.new end attr_reader :filename, :tag, :repository, :git_cmd def initialize(filename:, repository:, tag:, git_cmd: self.class.git_cmd) @git_cmd = git_cmd git_cmd.repository = repository git_cmd.tagname = tag git_cmd.filename = filename end def lines git_cmd.show.split("\n") end class GitCmd attr_accessor :repository, :tagname, :filename def show `git #{git_dir} show #{tagname}:#{filename}` end def git_dir %Q[--git-dir="#{repository}"] end end end end OO module Subset class Everything def lines(everything) everything end end class LineNumber attr_reader :line_numbers def initialize(line_numbers:) @line_numbers = line_numbers end def lines(possibilities) clump_specs.collect {|spec| clump_for(spec, possibilities) }.flatten.compact end def clump_specs line_numbers.gsub(/[‘|’]/, "").gsub(/ /,'').split(",") end def clump_for(spec, possibilities) Clump.lines(spec: spec, possibilities: possibilities) end end end Subset: Source: class Clump def self.lines(spec:, possibilities: []) self.for(spec: spec, possibilities: possibilities).lines end def self.for(spec:, possibilities: []) if spec.include?('#') Clump::Comment else Clump::LineNumber end.new(spec: spec, input: possibilities) end attr_reader :spec, :input def initialize(spec:, input: []) @spec = spec @input = input end class LineNumber < Clump def lines expand_clump(spec).collect {|i| input[i - 1]}.compact end def expand_clump(spec) edges = spec.split('-').collect(&:to_i) (edges.min.to_i..edges.max.to_i).to_a end end class Comment < Clump def lines num_spaces = spec.delete("#").to_i (" " * num_spaces) + "# ..." end end end Clump: module Justification class None def self.justify(lines) lines end end class BlockLeft def self.justify(lines) new(lines).justify end attr_reader :lines def initialize(lines) @lines = lines end def justify lines.map {|line| line.slice(num_leading_spaces_to_remove..-1) || "" } end private def num_leading_spaces_to_remove @num ||= lines.reduce(999999) {|current_min, line| line.empty? ? current_min : [current_min, num_leading_spaces(line)].min } end def num_leading_spaces(line) line[/\A */].size end end end Justification: class Listing attr_reader :source, :subsetter, :justifier def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end Factories
  212. @sandimetz May 2018 OO class Listing attr_reader :source, :subsetter, :justifier

    def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end
  213. @sandimetz May 2018 OO class Listing attr_reader :source, :subsetter, :justifier

    def initialize(source:, subsetter:, justifier:) @source = source @subsetter = subsetter @justifier = justifier end def lines justifier.justify(subsetter.lines(source.lines)) end end