Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

How to mimic Finder/Mac OS duplication in ruby?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use regular expressions for matching the given title and generating the successor:

def next_title(title)
  case title
  when /(.*) Copy (d*[1-9]d*)z/
    "#{$1} Copy #{$2.to_i.succ}"
  when /(.*) Copyz/
    "#{$1} Copy 2"
  else
    "#{title} Copy"
  end
end

next_title("Rocket Man")         #=> "Rocket Man Copy"
next_title("Rocket Man Copy")    #=> "Rocket Man Copy 2"
next_title("Rocket Man Copy 2")  #=> "Rocket Man Copy 3"

It also handles several edge cases the way Finder does:

next_title("Rocket Man Copy 1")   #=> "Rocket Man Copy 2"
next_title("Rocket Man Copy 02")  #=> "Rocket Man Copy 3"
next_title("Rocket Man Copy 0")   #=> "Rocket Man Copy 0 Copy"
next_title("Rocket Man Copy -1")  #=> "Rocket Man Copy -1 Copy"
next_title("Copy")                #=> "Copy Copy"
next_title("Copy 2")              #=> "Copy 2 Copy"

To ensure unique titles, you have to call next_title repeatedly until you find one that doesn't already exist, e.g.:

title = "Rocket Man"
title = next_title(title) while Song.exists?(title: title)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...