From b795db0ff7cd09de33cb3b4133563c7971e2fbc2 Mon Sep 17 00:00:00 2001 From: a14m Date: Thu, 10 Dec 2020 21:35:07 +0100 Subject: Refactor input generator (and update docs) --- CLI/ruby/lib/game_of_life/generators/input.rb | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/CLI/ruby/lib/game_of_life/generators/input.rb b/CLI/ruby/lib/game_of_life/generators/input.rb index 79626d4..bb43e3d 100644 --- a/CLI/ruby/lib/game_of_life/generators/input.rb +++ b/CLI/ruby/lib/game_of_life/generators/input.rb @@ -14,15 +14,27 @@ module GameOfLife # @option options [Numeric] "width" (floored) and used as the width of the universe # @option options [Numeric] "height" (floored) and used as the height of the universe # @return [GameOfLife::Universe] populated with the parsed input + # @see ::GameOfLife::Generators::Input.generate_input_data # @see ::GameOfLife::Generators::Input.populate def new(options) + width = options["width"].to_i + height = options["height"].to_i + uri = options["input"] + input = generate_input_data(uri: uri) + populate(input: input, width: width, height: height) + end + + # Generate the seeding data for populating the {GameOfLife::Universe} from input URI + # @param uri [String] inputh path to local file or URL to open + # @return [Array>] covering the defined size of universe + # @see ::GameOfLife::Generators::Input.populate + private def generate_input_data(uri:) # By design, it's intentional that we use URI.open to to allow seamless file and url access - raw = URI.open(options["input"]).read.split("\n") # rubocop:disable Security/Open + raw = URI.open(uri).read.split("\n") # rubocop:disable Security/Open # Parse the file and convert it to a boolean 2D array # where any alpha numeric character is considered a living cell (true) raw.map! { |row| row.chars.map { |char| char.match?(/[[:alnum:]]/) } } - populate(parsed_input: raw, width: options["width"].to_i, height: options["height"].to_i) rescue ::OpenURI::HTTPError, ::SocketError raise GameOfLife::Error, "URL isn't avaialable, please check it again and make sure the URL is correct" rescue ::Errno::ENOENT @@ -31,16 +43,17 @@ module GameOfLife end # Populate the {GameOfLife::Universe} with reference to the parsed input 2D array (True/False) - # @param parsed_input [Array>] + # @param input [Array>] # @param width [Integer] width of the generated cyclic universe # @param height [Integer] height of the generated cyclic universe # @return [GameOfLife::Universe] populated with the parsed input - private def populate(parsed_input:, width:, height:) + # @see ::GameOfLife::Generators::Input.generate_input_data + private def populate(input:, width:, height:) # Generate a universe based on the parsed raw file and fill it with the cells universe = GameOfLife::Universe.new(height: height, width: width) (0...height).each do |i| (0...width).each do |j| - cell = { x: j, y: i, alive: parsed_input.fetch(i, nil)&.fetch(j, nil) } + cell = { x: j, y: i, alive: input.fetch(i, nil)&.fetch(j, nil) } universe[i][j] = ::GameOfLife::Cell.new(cell) end end -- cgit v1.2.3