Skip to content

Commit

Permalink
Exposed registered natures and formats
Browse files Browse the repository at this point in the history
  • Loading branch information
CostinTanasoiu committed Aug 24, 2023
1 parent 9d9df1a commit 13ee6ab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 13 additions & 0 deletions lib/format_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,22 @@ def self.register_parser(callable_parser, formats:, natures:, priority: LEAST_PR
end
@parser_priorities ||= {}
@parser_priorities[callable_parser] = priority

@registered_natures ||= []
@registered_natures |= parser_provided_natures
@registered_formats ||= []
@registered_formats |= parser_provided_formats
end
end

def self.registered_natures
@registered_natures
end

def self.registered_formats
@registered_formats
end

# Deregister a parser object (makes FormatParser forget this parser existed). Is mostly used in
# tests, but can also be used to forcibly disable some formats completely.
#
Expand Down
14 changes: 4 additions & 10 deletions spec/format_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,10 @@
end

it 'ensures that MP3 parser is the last one among all' do
parsers = FormatParser.parsers_for(
[:audio, :image, :document, :text, :video, :archive],
[
:aac, :aiff, :arw, :bmp, :cr2, :cr3, :dpx, :fdx, :flac, :gif, :heif, :heic,
:jpg, :json, :m3u, :mov, :mp3, :mp4, :m4a, :m4b, :m4p, :m4r, :m4v, :mpg,
:mpeg, :nef, :ogg, :pdf, :png, :psd, :rw2, :tif, :wav, :webp, :zip
]
)

parser_class_names = parsers.map { |parser| parser.class.name }
natures = FormatParser.registered_natures
formats = FormatParser.registered_formats
prioritised_parsers = FormatParser.parsers_for(natures, formats)
parser_class_names = prioritised_parsers.map { |parser| parser.class.name }
expect(parser_class_names.last).to eq 'FormatParser::MP3Parser'
end
end
Expand Down

0 comments on commit 13ee6ab

Please sign in to comment.