Class: Tagmv::Filesystem

Inherits:
Object
  • Object
show all
Defined in:
lib/tagmv/filesystem.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Filesystem

Returns a new instance of Filesystem.



11
12
13
14
15
16
17
18
# File 'lib/tagmv/filesystem.rb', line 11

def initialize(opts={})
  @tags =  scrub_tags(opts[:tags])
  @files = opts[:files]
  @dry_run = opts[:dry_run]
  @reorder = opts[:reorder]
  @tag_order = opts[:tag_order]
  @top_level_tags = opts[:top_level_tags]
end

Class Attribute Details

.rootObject

Returns the value of attribute root.



7
8
9
# File 'lib/tagmv/filesystem.rb', line 7

def root
  @root
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



10
11
12
# File 'lib/tagmv/filesystem.rb', line 10

def files
  @files
end

#reorderObject (readonly)

Returns the value of attribute reorder.



10
11
12
# File 'lib/tagmv/filesystem.rb', line 10

def reorder
  @reorder
end

#tag_orderObject (readonly)

Returns the value of attribute tag_order.



10
11
12
# File 'lib/tagmv/filesystem.rb', line 10

def tag_order
  @tag_order
end

#tagsObject (readonly)

Returns the value of attribute tags.



10
11
12
# File 'lib/tagmv/filesystem.rb', line 10

def tags
  @tags
end

#top_level_tagsObject (readonly)

Returns the value of attribute top_level_tags.



10
11
12
# File 'lib/tagmv/filesystem.rb', line 10

def top_level_tags
  @top_level_tags
end

Instance Method Details

#move_filesObject



59
60
61
62
63
64
65
# File 'lib/tagmv/filesystem.rb', line 59

def move_files
  # skip duplicate moves
  return if reorder && scrub_files.size == 1 && (scrub_files.first.sub(target_dir + '/','') !=~ /\//)

  FileUtils.mv(scrub_files, target_dir, options)
rescue ArgumentError
end

#prepare_dirObject



52
53
54
55
56
57
# File 'lib/tagmv/filesystem.rb', line 52

def prepare_dir
  @@prepare_dir ||= Hash.new do |h, key|
    h[key] = FileUtils.mkdir_p(key, options)
  end
  @@prepare_dir[target_dir]
end

#scrub_filesObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tagmv/filesystem.rb', line 26

def scrub_files
  files.select do |file|
    path = File.expand_path(file)
    if File.exist?(path)
      path
    else
      puts "tmv: rename #{file} to #{target_dir}/#{File.basename(file)}: #{Errno::ENOENT.exception}"
      false
    end
  end
end

#scrub_tags(tags) ⇒ Object



20
21
22
23
24
# File 'lib/tagmv/filesystem.rb', line 20

def scrub_tags(tags)
  # only keep legit file characters & remove trailing periods, remove duplicates after
  bad_chars =  /^[\-]|[^0-9A-Za-z\.\-\_]|[\.]+$/
  tags.map {|t| t.gsub(bad_chars, '') }.uniq
end

#tag_dirsObject



44
45
46
# File 'lib/tagmv/filesystem.rb', line 44

def tag_dirs
  tags_in_order.map {|x| x.gsub(/$/, '-') }
end

#tags_in_orderObject



38
39
40
41
42
# File 'lib/tagmv/filesystem.rb', line 38

def tags_in_order
  return tags unless reorder

  (top_level_tags | tag_order) & tags
end

#target_dirObject



48
49
50
# File 'lib/tagmv/filesystem.rb', line 48

def target_dir
  File.join(Filesystem.root, *tag_dirs)
end

#transferObject



67
68
69
# File 'lib/tagmv/filesystem.rb', line 67

def transfer
  prepare_dir && move_files
end