# Container class for helper methods.
class USPSFlags::Helpers
# Valid options for flag generation.
#
# @param [Symbol] type Specify subset of flags.
# @option type [Symbol] :all All flags
# @option type [Symbol] :officer Officer flags
# @option type [Symbol] :insignia Insignia-eligible officer flags (no past officers)
# @option type [Symbol] :squadron Squadron-level officer flags
# @option type [Symbol] :district District-level officer flags
# @option type [Symbol] :national National-level officer flags
# @option type [Symbol] :special Special flags
# @option type [Symbol] :us US flag
# @return [Array] Valid options for flag generation (based on the provided type).
def self.valid_flags(type = :all)
squadron = %w[
PLTC
PC
PORTCAP
FLEETCAP
LT
FLT
1LT
LTC
CDR
]
district = %w[
PDLTC
PDC
DLT
DAIDE
DFLT
D1LT
DLTC
DC
]
national = %w[
PSTFC
PRC
PVC
PCC
NAIDE
NFLT
STFC
RC
VC
CC
]
past = %w[
PLTC
PC
PDLTC
PDC
PSTFC
PRC
PVC
PCC
]
special = %w[
CRUISE
OIC
ENSIGN
WHEEL
]
us = %w[
US
]
case type
when :all
squadron + district + national + special + us
when :officer
squadron + district + national
when :insignia
squadron + district + national - past
when :squadron
squadron
when :district
district
when :national
national
when :special
special
when :us
us
end
end
# Displays an overlay grid with regularly spaced locator markers.
#
# This is useful for adjusting or creating new SVG data generators, but should not otherwise need to be called.
# @private
def self.grid(width: USPSFlags::Config::BASE_FLY, height: USPSFlags::Config::BASE_HOIST)
<<~SVG
SVG
end
# Displays an overlay indicator of concentric circles and radiating lines.
#
# This is useful for adjusting or creating new SVG data generators, but should not otherwise need to be called.
# @private
def self.locator
<<~SVG
SVG
end
# Creates a vertical arrow for the trident spec sheet.
#
# This is used USPSFlags::Core.trident_spec, and should never need to be called directly.
# @private
def self.v_arrow(x, top, bottom, pointer_top = nil, pointer_bottom = nil, label: nil, label_offset: (USPSFlags::Config::BASE_FLY/120), label_offset_y: 0, label_align: "left", color: "#CCCCCC", stroke_width: (USPSFlags::Config::BASE_FLY/600), stroke_dash: "10, 10", font_size: (USPSFlags::Config::BASE_FLY/60), arrow_size: (USPSFlags::Config::BASE_FLY/120), fly: USPSFlags::Config::BASE_FLY, unit: nil)
label = bottom - top if label.nil?
label = label.to_i if label - label.to_i == 0
label = Rational(label) * fly / USPSFlags::Config::BASE_FLY
if label == label.to_i
label = label.to_i
label_fraction = ""
else
label, label_fraction = label.to_simplified_a
end
svg = ""
unless pointer_top.nil?
svg << <<~SVG
SVG
end
unless pointer_bottom.nil?
svg << <<~SVG
SVG
end
svg << <<~SVG
#{label} #{label_fraction} #{unit}
SVG
svg
end
# Creates a horizontal arrow for the trident spec sheet.
#
# This is used USPSFlags::Core.trident_spec, and should never need to be called directly.
# @private
def self.h_arrow(y, left, right, pointer_left = nil, pointer_right = nil, label: nil, label_offset: (USPSFlags::Config::BASE_FLY/45), label_offset_x: 0, label_align: "middle", color: "#CCCCCC", stroke_width: (USPSFlags::Config::BASE_FLY/600), stroke_dash: "10, 10", font_size: (USPSFlags::Config::BASE_FLY/60), arrow_size: (USPSFlags::Config::BASE_FLY/120), fly: USPSFlags::Config::BASE_FLY, unit: nil)
label = right - left if label.nil?
label = label.to_i if label - label.to_i == 0
label = Rational(label) * fly / USPSFlags::Config::BASE_FLY
if label == label.to_i
label = label.to_i
label_fraction = ""
else
label, label_fraction = label.to_simplified_a
end
svg = ""
unless pointer_left.nil?
svg << <<~SVG
SVG
end
unless pointer_right.nil?
svg << <<~SVG
SVG
end
svg << <<~SVG
#{label} #{label_fraction} #{unit}
SVG
svg
end
# Prints message(s) to the console and logs them.
#
# This should never need to be called directly.
# @private
def self.log(*messages)
::FileUtils.mkdir_p(USPSFlags::Config.log_path)
outputs = [STDOUT]
begin
log_file = File.open("#{USPSFlags::Config.log_path}/flag.log", 'a')
outputs << log_file
rescue Errno::EACCES => e
puts "\nError accessing log file." unless USPSFlags::Config.log_fail_quietly
end
messages.each do |message|
outputs.each { |f| f.write(message) }
end
messages
ensure
log_file.close if log_file.is_a?(File)
end
end