Module: Id3Taginator::Frames::Frameable

Instance Method Summary collapse

Instance Method Details

#find_frame(frame_id) ⇒ Id3v2Frame?

finds the frame with the given frame id, or nil of not present



16
17
18
# File 'lib/id3taginator/frames/frameable.rb', line 16

def find_frame(frame_id)
  @frames.find { |f| f.frame_id == frame_id }
end

#find_frames(frame_id) ⇒ Array<Id3v2Frame>?

finds the frames with the given frame id, or nil of not present



25
26
27
# File 'lib/id3taginator/frames/frameable.rb', line 25

def find_frames(frame_id)
  @frames.select { |f| f.frame_id == frame_id }
end

#set_frame_fields(frame, fields, *arguments) ⇒ Object

sets the given arguments to the appropriate field of the given frame. The field order must be same as the argument order. So e.g. fields a, b, c and arguments a_v, b_v, c_v will lead to a = a_v, b = b_v and c = c_v If the frame already exists, it will be updated. Otherwise it will be created.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/id3taginator/frames/frameable.rb', line 36

def set_frame_fields(frame, fields, *arguments)
  existing_frame = find_frame(frame.frame_id(@major_version, @options))
  unless existing_frame.nil?
    fields.each_with_index { |field, index| existing_frame.instance_variable_set(field, arguments[index]) }
    return
  end

  new_frame = frame.build_frame(*arguments, @options, id3_version: @major_version)
  new_frame.options = @options
  @frames << new_frame
end

#set_frame_fields_by_selector(frame, fields, selector_lambda, *arguments) ⇒ Object

sets the given arguments to the appropriate field of the given frame(s). The field order must be same as the argument order. So e.g. fields a, b, c and arguments a_v, b_v, c_v will lead to a = a_v, b = b_v and c = c_v If the frame already exists, it will be updated. Otherwise it will be created. The field will be determined by the given selector_lambda, so e.g. ->(f) { f.descriptor == ‘a value’ } will be updated, if the frame.descriptor is ‘a value’



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/id3taginator/frames/frameable.rb', line 59

def set_frame_fields_by_selector(frame, fields, selector_lambda, *arguments)
  existing_frames = find_frames(frame.frame_id(@major_version, @options))

  existing_frame = existing_frames&.find { |f| selector_lambda.call(f) }

  unless existing_frame.nil?
    fields.each_with_index { |field, index| existing_frame.instance_variable_set(field, arguments[index]) }
    return
  end

  new_frame = frame.build_frame(*arguments, @options, id3_version: @major_version)
  new_frame.options = @options
  @frames << new_frame
end

#unsupported_frame(frame_name, version) ⇒ Object

raises an error that the given frame is not available for the id3v2 version



7
8
9
# File 'lib/id3taginator/frames/frameable.rb', line 7

def unsupported_frame(frame_name, version)
  raise Errors::Id3TagError, "#{frame_name} is not supported in Id3v2.#{version}"
end