module MusicIds::Id

Public Class Methods

included(base) click to toggle source
# File lib/music_ids/id.rb, line 45
def self.included(base)
  base.extend(ClassMethods)
end
new(id_string, opts = {ok: true}) click to toggle source

@api private @param id_string [String] The ISRC string @param opts [Hash] @option opts [true, false] :ok (true) Whether the ISRC is well-formed or not

# File lib/music_ids/id.rb, line 53
def initialize(id_string, opts = {ok: true})
  @id_string = id_string.dup.freeze
  @ok = opts[:ok] ? true : false
end

Public Instance Methods

==(other) click to toggle source
# File lib/music_ids/id.rb, line 70
def ==(other)
  to_s == other.to_s
end
as(format) click to toggle source

Returns the ID as a string, either as the normalised canonical string (:data) or the format specified for ‘full’ display (:full). Note that a badly-formed ID will simply return the original string whichever format you ask for. @param format [:data, :full, :prefixed] the output format to use @return [String]

# File lib/music_ids/id.rb, line 80
def as(format)
  case format
  when :data
    to_s
  when :full
    ok? ? as_full : to_s
  when :prefixed
    "#{prefix}:#{as_full}"
  else
    raise ArgumentError, "format must be one of [:data, :full, :prefixed], but it was #{format.inspect}"
  end
end
as_json(*) click to toggle source
# File lib/music_ids/id.rb, line 98
def as_json(*)
  to_s
end
ok?() click to toggle source

Is this a well-formed ID? @return [true,false]

# File lib/music_ids/id.rb, line 60
def ok?
  @ok
end
prefix() click to toggle source

Return the prefix for the identifier

# File lib/music_ids/id.rb, line 94
def prefix
  self.class.prefix
end
to_s() click to toggle source

return the ID as a normalised string @return [String]

# File lib/music_ids/id.rb, line 66
def to_s
  @id_string.dup
end

Private Instance Methods

fetch(ivar) { |id_string| ... } click to toggle source
# File lib/music_ids/id.rb, line 104
def fetch(ivar)
  return unless ok?
  return instance_variable_get(ivar) if instance_variable_defined?(ivar)
  instance_variable_set(ivar, yield(@id_string).freeze)
end