module MusicIds::Id::ClassMethods

Public Instance Methods

parse(input, opts = {}) click to toggle source

Parse an identifier string into an instance

@param input [String] The input identifier string to parse @param opts [Hash] Parsing options @option opts [true, false] :relaxed (false) Whether to parse in relaxed mode @return [ISRC] the ISRC instance

# File lib/music_ids/id.rb, line 10
def parse(input, opts = {})
  opts[:relaxed] ? relaxed(input) : parse_strict(input)
end
relaxed(input) click to toggle source
# File lib/music_ids/id.rb, line 14
def relaxed(input)
  parse_string(input) { |input|
    new(input, ok: false) unless input.nil?
  }
end

Private Instance Methods

parse_strict(input) click to toggle source
# File lib/music_ids/id.rb, line 28
def parse_strict(input)
  parse_string(input) { |input|
    raise ArgumentError, "'#{input}' is not the right length to be a #{self.name}"
  }
end
parse_string(input) { |input| ... } click to toggle source
# File lib/music_ids/id.rb, line 34
def parse_string(input)
  return yield(input) if input.nil?
  normalised = input.to_s.upcase
  if match = well_formed_id_matcher.match(normalised)
    new(match[1].gsub('-', ''))
  else
    yield(input)
  end
end
well_formed_id_matcher() click to toggle source
# File lib/music_ids/id.rb, line 22
def well_formed_id_matcher
  @matcher ||= begin
    Regexp.compile("\\A(?:#{prefix}:)?(#{id_blocks.join('-')}|#{id_blocks.join('')})\\Z")
  end
end