Class: AstroSubframeOrganizer::FilenameParser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/astro_subframe_organizer/filename_parser.rb

Overview

Base class for parsing astrophotography filenames.

This class uses the Strategy pattern to handle format-specific filename parsing. The factory method ‘for_file` creates the appropriate parser subclass based on file extension.

Subclasses must implement the ‘parse` method, which returns a hash of metadata keys extracted from the filename.

Usage:

parser = FilenameParser.for_file('/path/to/Light_M42_1.0s_Bin1_T7_ISO100_20220508-120000_-10.0C_0001.fit')
 = parser.parse

See FitsFilenameParser and CR2FilenameParser for format-specific implementations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FilenameParser

Returns a new instance of FilenameParser.



40
41
42
43
# File 'lib/astro_subframe_organizer/filename_parser.rb', line 40

def initialize(path)
  @path = path
  @filename = File.basename(path)
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



22
23
24
# File 'lib/astro_subframe_organizer/filename_parser.rb', line 22

def filename
  @filename
end

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/astro_subframe_organizer/filename_parser.rb', line 22

def path
  @path
end

#resultObject (readonly)

Returns the value of attribute result.



22
23
24
# File 'lib/astro_subframe_organizer/filename_parser.rb', line 22

def result
  @result
end

Class Method Details

.for_file(path, use_headers: true) ⇒ FitsFilenameParser, CR2FilenameParser

Factory method to create the appropriate parser for a file.

Determines the correct parser based on file extension (.fit or .cr2). Case-insensitive for file extensions.

Parameters:

  • path (String)

    Full path to the image file

Returns:

  • (FitsFilenameParser, CR2FilenameParser)

    Appropriate parser instance

Raises:

  • (ArgumentError)

    If file format is not supported



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/astro_subframe_organizer/filename_parser.rb', line 63

def self.for_file(path, use_headers: true)
  ext = File.extname(path).downcase
  if Config.fits_extensions.include?(ext)
    if use_headers
      FilenameParsers::FitsHeaderParser.new(path)
    else
      FilenameParsers::FitsFilenameParser.new(path)
    end
  elsif Config.raw_extensions.include?(ext)
    FilenameParsers::CR2FilenameParser.new(path)
  else
    raise ArgumentError, "Unsupported file format: #{path}"
  end
end

Instance Method Details

#parseFileMetadata

Returns a hash of parsed metadata.

Subclasses must implement this method.

Returns:

  • (FileMetadata)

    Parsed metadata with attributes like :type, :target, :exposure, etc.

Raises:

  • (NotImplementedError)

    If not implemented by subclass



51
52
53
# File 'lib/astro_subframe_organizer/filename_parser.rb', line 51

def parse
  raise NotImplementedError, 'Subclasses must implement #parse'
end