Class: ID3Tag::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/id3tag/tag.rb

Constant Summary collapse

MultipleFrameError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, scope = :all) ⇒ Tag

Returns a new instance of Tag.



11
12
13
# File 'lib/id3tag/tag.rb', line 11

def initialize(source, scope = :all)
  @source, @scope = source, Scope.new(scope)
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



15
16
17
# File 'lib/id3tag/tag.rb', line 15

def scope
  @scope
end

#sourceObject (readonly)

Returns the value of attribute source.



15
16
17
# File 'lib/id3tag/tag.rb', line 15

def source
  @source
end

Class Method Details

.read(source, scope = :all) ⇒ Object



6
7
8
# File 'lib/id3tag/tag.rb', line 6

def read(source, scope = :all)
  new(source, scope)
end

Instance Method Details

#all_frames_by_id(*ids) ⇒ Object



44
45
46
47
48
# File 'lib/id3tag/tag.rb', line 44

def all_frames_by_id(*ids)
  ids.inject([]) do |frames, id|
    frames += get_frames(id)
  end
end

#audio_fileObject



87
88
89
# File 'lib/id3tag/tag.rb', line 87

def audio_file
  @audio_file ||= AudioFile.new(@source)
end

#content_of_first_frame(name) ⇒ Object



23
24
25
26
# File 'lib/id3tag/tag.rb', line 23

def content_of_first_frame(name)
  frame = first_frame_by_id(*possible_frame_ids_by_name(name))
  frame && frame.content
end

#content_of_first_frame_with_language(name, lang) ⇒ Object



34
35
36
37
# File 'lib/id3tag/tag.rb', line 34

def content_of_first_frame_with_language(name, lang)
  frame = all_frames_by_id(*possible_frame_ids_by_name(name)).find { |x| x.language == lang.to_s }
  frame && frame.content
end

#first_frame_by_id(*ids) ⇒ Object



39
40
41
42
# File 'lib/id3tag/tag.rb', line 39

def first_frame_by_id(*ids)
  first_existing_id = ids.find { |id| frame_ids.include?(id) }
  first_existing_id && get_frames(first_existing_id).first
end

#frame_idsObject



63
64
65
# File 'lib/id3tag/tag.rb', line 63

def frame_ids
  @frame_ids ||= frames.map { |frame| frame.id }
end

#framesObject



67
68
69
# File 'lib/id3tag/tag.rb', line 67

def frames
  @frames ||= v2_frames + v1_frames
end

#get_frame(frame_id) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/id3tag/tag.rb', line 50

def get_frame(frame_id)
  frames = get_frames(frame_id)
  if frames.count > 1
    raise MultipleFrameError, "Could not return only one frame with id: #{frame_id}. Tag has #{frames.count} of them. Try #get_frames to get all of them"
  else
    frames.first
  end
end

#get_frames(frame_id) ⇒ Object



59
60
61
# File 'lib/id3tag/tag.rb', line 59

def get_frames(frame_id)
  frames.select { |frame| frame.id == frame_id }
end

#v1_framesObject



79
80
81
82
83
84
85
# File 'lib/id3tag/tag.rb', line 79

def v1_frames
  if should_and_could_read_v1_frames?
    ID3V1FrameParser.new(audio_file.v1_tag_body).frames
  else
    []
  end
end

#v2_framesObject



71
72
73
74
75
76
77
# File 'lib/id3tag/tag.rb', line 71

def v2_frames
  if should_and_could_read_v2_frames?
    ID3V2FrameParser.new(audio_file.v2_tag_body, audio_file.v2_tag_major_version_number).frames
  else
    []
  end
end