Module: MusicIds::Id

Included in:
GRid, ISRC
Defined in:
lib/music_ids/id.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



45
46
47
# File 'lib/music_ids/id.rb', line 45

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#==(other) ⇒ Object



70
71
72
# File 'lib/music_ids/id.rb', line 70

def ==(other)
  to_s == other.to_s
end

#as(format) ⇒ String

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.

Parameters:

  • format (:data, :full, :prefixed) —

    the output format to use

Returns:

  • (String)


80
81
82
83
84
85
86
87
88
89
90
91
# 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 ⇒ Object



98
99
100
# File 'lib/music_ids/id.rb', line 98

def as_json(*)
  to_s
end

#initialize(id_string, opts = {ok: true}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • id_string (String) —

    The ISRC string

  • opts (Hash) (defaults to: {ok: true})

Options Hash (opts):

  • :ok (true, false) — default: true —

    Whether the ISRC is well-formed or not



53
54
55
56
# 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

#ok? ⇒ true, false

Is this a well-formed ID?

Returns:

  • (true, false)


60
61
62
# File 'lib/music_ids/id.rb', line 60

def ok?
  @ok
end

#prefix ⇒ Object

Return the prefix for the identifier



94
95
96
# File 'lib/music_ids/id.rb', line 94

def prefix
  self.class.prefix
end

#to_s ⇒ String

return the ID as a normalised string

Returns:

  • (String)


66
67
68
# File 'lib/music_ids/id.rb', line 66

def to_s
  @id_string.dup
end