I have an interesting problem where I am trying to implement a ruby duplication mechanism similar to MAC OS Finder. I need to try to do the following. I have a ruby object with the following fields:
song.title = 'RocketMan'
song.slug = 'rocket-man'
With the help of Mongoid Slug, if I were to .clone
the above object I would get the following below. The slug is set by mongoid-slug gem functionality.
song.title = 'RocketMan'
song.slug = 'rocket-man-1'
What I'm having trouble is trying to implement a Mac type duplication mechanism where I would mimic the following algorithm below. What is important to note with MAC OS Finder is that whichever file I were to duplicate produces the next increment.
file.jpg
file copy.jpg
file copy 1.jpg
file copy 2.jpg
So if I were to duplicate any of the files above, the proceeding file would be file copy 3.jpg
Now for the problem, I am essentially, trying to duplicate this with ruby.For the example below, please note the 'original object', it plays an important part in this problem.
##### ORIGINAL #####
song.title = 'RocketMan'
song.slug = 'rocket-man'
#.clone
song.title = 'RocketMan'
song.slug = 'rocket-man-1'
If I were to clone
the original ruby object, I get the following:
song.title = 'RocketMan'
song.slug = 'rocket-man-2'
If I were to clone
the object with slug rocket-man-1
, the following is produced.
song.title = 'RocketMan'
song.slug = 'rocket-man-1-1'
then if cloned again, I would get 'rocket-man-1-2' etc.
Is there some kind of gem or algorithm that I could potentially use to mimic the ability of MAC OS Finder? It seems to me that no matter what file or its copy I use to duplicate in mac os, I get the right increment. With ruby and mongoid-slug, if I wanted to mimic the Finder, I would have to only clone the original object. A user in this project I'm working on can clone the original and/or the clone. If they were to duplicate the copy, then the whole naming convention doesn't work.
If anyone has some insight I would greatly appreciate it. Also, just to clarify, the slug generation wouldn't matter. What Im most interested in is trying to create the following titles from copying:
Rocket Man
Rocket Man Copy
Rocket Man Copy 2
Rocket Man Copy 3
etc.
question from:
https://stackoverflow.com/questions/65877584/how-to-mimic-finder-mac-os-duplication-in-ruby