Skip to content

Instantly share code, notes, and snippets.

@roktas
Created July 29, 2023 23:45
Show Gist options
  • Save roktas/54f5a420afd32e1d5565394864832f9f to your computer and use it in GitHub Desktop.
Save roktas/54f5a420afd32e1d5565394864832f9f to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module Mahjong
module SVG
Dim = Data.define(:width, :height, :unit) do
def initialize(width:, height:, unit: "mm") = super(width: Float(width), height: Float(height), unit:)
def shortest = deconstruct.min
def longest = deconstruct.max
end
Margin = Data.define(:top, :right, :bottom, :left) do
def initialize(top:, right: nil, bottom: nil, left: nil)
case [top, right, bottom, left]
in Numeric, Numeric, Numeric, Numeric then # nop
in Numeric, Numeric, Numeric, NilClass then left = right
in Numeric, Numeric, NilClass, NilClass then bottom, left = top, right
in Numeric, NilClass, NilClass, NilClass then bottom, left, right = top, top, top
end
super(top: Float(top), right: Float(right), bottom: Float(bottom), left: Float(left))
end
class << self
def zero = (@zero ||= self[0.0, 0.0, 0.0, 0.0])
end
end
Paper = F.pedantichash(
"paper",
a3: Dim[297.0, 420.0],
a4: Dim[210.0, 297.0],
a5: Dim[148.5, 210.0],
a6: Dim[105.0, 148.5],
a7: Dim[74.25, 105.0],
b3: Dim[353.0, 500.0],
b4: Dim[250.0, 353.0],
b5: Dim[176.5, 250.0],
b6: Dim[125.0, 176.5],
b7: Dim[88.25, 125.0],
large: Dim[130.0, 210.0],
passport: Dim[88.0, 125.0],
pocket: Dim[90.0, 140.0],
travelers: Dim[110.0, 210.0],
us: Dim[215.9, 279.4],
xlarge: Dim[190.0, 250.0],
).tap { def _1.default = self[:a4] }.freeze
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment