diff --git a/CHANGELOG.md b/CHANGELOG.md index dac3da6..f88544e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Skymap::Catalog::ConstellationNames` gives the English name of each of the 88 IAU constellations +### Changed + +- `Skymap::Canvas` raises an `ArgumentError` when the padding is negative or + leaves no room for the sky + +### Fixed + +- The catalogs give an enumerator when `each` is called without a block, + instead of raising a `LocalJumpError` + ## [0.2.0] - 2026-10-27 ### Added diff --git a/lib/skymap/canvas.rb b/lib/skymap/canvas.rb index 40cdcd9..c64a12d 100644 --- a/lib/skymap/canvas.rb +++ b/lib/skymap/canvas.rb @@ -2,6 +2,15 @@ module Skymap Canvas = Data.define(:size, :padding) do + def initialize(size:, padding:) + unless padding >= 0 && padding < size / 2.0 + raise ArgumentError, + "padding must not be negative and must be less than half the size" + end + + super + end + def center Point.new(x: size / 2.0, y: size / 2.0) end diff --git a/lib/skymap/catalog/constellation_boundaries.rb b/lib/skymap/catalog/constellation_boundaries.rb index b50da15..6d7282d 100644 --- a/lib/skymap/catalog/constellation_boundaries.rb +++ b/lib/skymap/catalog/constellation_boundaries.rb @@ -13,6 +13,8 @@ class ConstellationBoundaries ) def each + return enum_for(:each) unless block_given? + CSV.foreach(PATH, headers: true).chunk_while do |previous, row| previous["border"] == row["border"] end.each do |rows| diff --git a/lib/skymap/catalog/constellation_lines.rb b/lib/skymap/catalog/constellation_lines.rb index f4abab9..036df7f 100644 --- a/lib/skymap/catalog/constellation_lines.rb +++ b/lib/skymap/catalog/constellation_lines.rb @@ -13,6 +13,8 @@ class ConstellationLines ) def each + return enum_for(:each) unless block_given? + CSV.foreach(PATH, headers: true) do |row| yield line_from(row) end diff --git a/lib/skymap/catalog/stars.rb b/lib/skymap/catalog/stars.rb index 38dd1a3..80f6f83 100644 --- a/lib/skymap/catalog/stars.rb +++ b/lib/skymap/catalog/stars.rb @@ -10,6 +10,8 @@ class Stars PATH = File.expand_path("../../../data/stars.csv", __dir__) def each + return enum_for(:each) unless block_given? + CSV.foreach(PATH, headers: true) do |row| yield star_from(row) end diff --git a/spec/skymap/canvas_spec.rb b/spec/skymap/canvas_spec.rb index 57d7ac4..0a7afbf 100644 --- a/spec/skymap/canvas_spec.rb +++ b/spec/skymap/canvas_spec.rb @@ -1,6 +1,24 @@ # frozen_string_literal: true RSpec.describe Skymap::Canvas do + describe ".new" do + it "accepts no padding" do + canvas = described_class.new(size: 400, padding: 0) + + expect(canvas.radius).to eq(200) + end + + it "rejects a negative padding" do + expect { described_class.new(size: 400, padding: -1) } + .to raise_error(ArgumentError, /padding/) + end + + it "rejects a padding that leaves no room for the sky" do + expect { described_class.new(size: 400, padding: 200) } + .to raise_error(ArgumentError, /padding/) + end + end + describe "#center" do it "is the middle of the canvas" do canvas = described_class.new(size: 400, padding: 1.5) diff --git a/spec/skymap/catalog/constellation_boundaries_spec.rb b/spec/skymap/catalog/constellation_boundaries_spec.rb index 78cff61..905606c 100644 --- a/spec/skymap/catalog/constellation_boundaries_spec.rb +++ b/spec/skymap/catalog/constellation_boundaries_spec.rb @@ -9,6 +9,14 @@ expect(count).to eq(266) end + it "gives an enumerator without a block" do + catalog = described_class.new + + first = catalog.each.next + + expect(first).to be_a(Skymap::ConstellationBoundary) + end + it "reads the two constellations a border separates" do catalog = described_class.new diff --git a/spec/skymap/catalog/constellation_lines_spec.rb b/spec/skymap/catalog/constellation_lines_spec.rb index ba800a3..5aec685 100644 --- a/spec/skymap/catalog/constellation_lines_spec.rb +++ b/spec/skymap/catalog/constellation_lines_spec.rb @@ -9,6 +9,14 @@ expect(count).to eq(752) end + it "gives an enumerator without a block" do + catalog = described_class.new + + first = catalog.each.next + + expect(first).to be_a(Skymap::ConstellationLine) + end + it "reads the constellation, ends and weight of each line" do catalog = described_class.new diff --git a/spec/skymap/catalog/stars_spec.rb b/spec/skymap/catalog/stars_spec.rb index a75ffae..75e96d6 100644 --- a/spec/skymap/catalog/stars_spec.rb +++ b/spec/skymap/catalog/stars_spec.rb @@ -9,6 +9,14 @@ expect(count).to eq(9096) end + it "gives an enumerator without a block" do + catalog = described_class.new + + first = catalog.each.next + + expect(first.hr).to eq(1) + end + it "reads the identifier, J2000 coordinates and magnitude of each star" do catalog = described_class.new