Guest User

Untitled

a guest
Oct 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. class NuclearPowerPlantHealthSummaryGenerator
  2. def generate(region)
  3. PiecePipe::Pipeline.new.
  4. source([{region: region}]).
  5. step(FetchPowerPlantsByRegion).
  6. step(FindWorstReactor).
  7. step(DetermineStatusClass).
  8. step(BuildPlantHealthSummary).
  9. step(SortByRadiationLevelsDescending).
  10. collect(:plant_health_summary).
  11. to_enum
  12. end
  13.  
  14. # Custom Step, overriding #generate_sequence to produce a sequence of power plants for a given region.
  15. # Each power plant is "produced" as a Hash with one key (so far) heading down the pipeline.
  16. class FetchPowerPlantsByRegion < PiecePipe::Step
  17. def generate_sequence(inputs)
  18. # The expected interface of any Step's soure is that it supports #to_enum.
  19. source.to_enum.each do |inputs|
  20. inputs[:region].power_plants.those_still_open.each do |power_plant|
  21. produce power_plant: power_plant # Each call to #produce sends another object down the pipe
  22. end
  23. end
  24. enb
  25. end
  26.  
  27. # For any given power plant, determine the worst reactor.
  28. # Implemented as an AssemblyStep that analyzes inputs[:power_plant] from the prior Step,
  29. # and installs a new key/val pair for :worst_reactor.
  30. class FindWorstReactor < PiecePipe::AssemblyStep
  31. def receive(inputs)
  32. # Figure out which reactor has the highest radiation levels.
  33. # "install" works a lot like "produce", but rather than take responsibility for the totality
  34. # of the produced object, we're just saying "add :worst_reactor to whatever's there and pass it on".
  35. install worst_reactor: inputs[:power_plant].reactors.reject(&:offline?).max_by(:&radiation)
  36. end
  37. end
  38.  
  39. # Figure out which CSS class corresonds to the radiation from the worst reactor.
  40. # (At this point, the inputs Hash has keys :region, :power_plant, and :worst_reactor.)
  41. class DetermineStatusClass < PiecePipe::AssemblyStep
  42. def receive(inputs)
  43. install highlight_css_class: StatusFormatters.determine_css_class(inputs[:worst_reactor].radiation)
  44. end
  45. end
  46.  
  47. # Composite our details into a line-item structure for our report.
  48. # Even though we consume most of the interesting values that arrive in the inputs Hash,
  49. # we're letting them ride as we simply install one more key, :plant_health_summary.
  50. # (This comes in handy, as we intend to sort these structures in a later step, using values
  51. # that are present in our transient input Hash, but NOT actually available in the
  52. # report structure.)
  53. class BuildPlantHealthSummary < PiecePipe::AssemblyStep
  54. def receive(inputs)
  55. power_plant = inputs[:power_plant]
  56. worst_reactor = inputs[:worst_reactor]
  57. install plant_health_summary: PlantHealthSummary.new(
  58. power_plant_id: power_plant.id,
  59. supervisor: power_plant.supervisor,
  60. reactor_name: worst_reactor.name,
  61. radiation: StatusFormatters.format_radiation(worst_reactor.radiation),
  62. css_class: inputs[:highlight_css_class]
  63. )
  64. end
  65. end
  66.  
  67. # Sort all the values that come through the pipe based on the radiation of the worst reactor
  68. # in each power_plant.
  69. # Notice this is not an AssemblyStep, and we're overriding #generate_sequence again, this time
  70. # because we're implementing a sink. The resulting downstream objects have the same structure
  71. # they arrived with.
  72. class SortByRadiationLevelsDescending < PiecePipe::Step
  73. def generate_sequence
  74. source.to_enum.sort_by do |inputs|
  75. inputs[:worst_reactor].radiation
  76. end.each do |inputs|
  77. produce inputs
  78. end
  79. end
  80. end
  81.  
  82. #... and that's it. Recall that the pipeline terminates with .collect(:plant_health_summary), which
  83. # is shorthand for a special Step that accepts Hashes and uses #produce to spit out only the specified
  84. # objects. Downstream of our #collect, only the PlantHealthSummary remains.
  85.  
  86. end
Add Comment
Please, Sign In to add comment