Module: Id3Taginator::Frames::CustomFrames

Includes:
Frameable
Included in:
Id3v2Tag
Defined in:
lib/id3taginator/frames/custom_frames.rb

Instance Method Summary collapse

Methods included from Frameable

#find_frame, #find_frames, #set_frame_fields, #set_frame_fields_by_selector, #unsupported_frame

Instance Method Details

#add_custom_frame(frame_id, content, selector_lambda = nil) ⇒ Object

adds a custom frame Multiple ones can be added, the selector must be given to determine if a frame should be updated or added

Parameters:

  • frame_id (Symbol)

    the frame id

  • content (String)

    the frame content’s data bytes represented as a String (str.bytes)

  • selector_lambda (Proc) (defaults to: nil)

    the lambda to find matching frames to update



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/id3taginator/frames/custom_frames.rb', line 23

def add_custom_frame(frame_id, content, selector_lambda = nil)
  if selector_lambda.nil?
    existing_frame = find_frame(frame_id)
  else
    existing_frames = find_frames(frame_id)
    existing_frame = existing_frames&.find { |f| selector_lambda.call(f) }
  end

  unless existing_frame.nil?
    existing_frame.content = content
    return
  end

  new_frame = CustomFrame.build_frame(content, @options, id3_version: @major_version)
  new_frame.options = @options
  @frames << new_frame
end

#custom_frame(frame_id) ⇒ String?

extracts a custom frame for the given id

Parameters:

  • frame_id (Symbol)

    the frame id

Returns:

  • (String, nil)

    returns the frame’s content data bytes represented as a String (str.bytes)



13
14
15
# File 'lib/id3taginator/frames/custom_frames.rb', line 13

def custom_frame(frame_id)
  find_frame(frame_id)&.content
end

#remove_custom_frame(frame_id, selector_lambda = nil) ⇒ Object

removes the frame with the given id or a specific one with the given selector

Parameters:

  • frame_id (String)

    the frame id to remove

  • selector_lambda (Proc) (defaults to: nil)

    the lambda to find matching frames to update



45
46
47
48
49
50
51
# File 'lib/id3taginator/frames/custom_frames.rb', line 45

def remove_custom_frame(frame_id, selector_lambda = nil)
  if selector_lambda.nil?
    @frames.delete_if { |f| f.frame_id == frame_id }
  else
    @frames.delete_if { |f| f.frame_id == frame_id && selector_lambda.call(f) }
  end
end