Class: Tagmv::Filesystem
- Inherits:
-
Object
- Object
- Tagmv::Filesystem
- Defined in:
- lib/tagmv/filesystem.rb
Class Attribute Summary collapse
-
.root ⇒ Object
Returns the value of attribute root.
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#reorder ⇒ Object
readonly
Returns the value of attribute reorder.
-
#tag_order ⇒ Object
readonly
Returns the value of attribute tag_order.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#top_level_tags ⇒ Object
readonly
Returns the value of attribute top_level_tags.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Filesystem
constructor
A new instance of Filesystem.
- #move_files ⇒ Object
- #prepare_dir ⇒ Object
- #scrub_files ⇒ Object
- #scrub_tags(tags) ⇒ Object
- #tag_dirs ⇒ Object
- #tags_in_order ⇒ Object
- #target_dir ⇒ Object
- #transfer ⇒ Object
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 = (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
.root ⇒ Object
Returns the value of attribute root.
7 8 9 |
# File 'lib/tagmv/filesystem.rb', line 7 def root @root end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
10 11 12 |
# File 'lib/tagmv/filesystem.rb', line 10 def files @files end |
#reorder ⇒ Object (readonly)
Returns the value of attribute reorder.
10 11 12 |
# File 'lib/tagmv/filesystem.rb', line 10 def reorder @reorder end |
#tag_order ⇒ Object (readonly)
Returns the value of attribute tag_order.
10 11 12 |
# File 'lib/tagmv/filesystem.rb', line 10 def tag_order @tag_order end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
10 11 12 |
# File 'lib/tagmv/filesystem.rb', line 10 def @tags end |
#top_level_tags ⇒ Object (readonly)
Returns the value of attribute top_level_tags.
10 11 12 |
# File 'lib/tagmv/filesystem.rb', line 10 def @top_level_tags end |
Instance Method Details
#move_files ⇒ Object
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, ) rescue ArgumentError end |
#prepare_dir ⇒ Object
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, ) end @@prepare_dir[target_dir] end |
#scrub_files ⇒ Object
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.(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 () # only keep legit file characters & remove trailing periods, remove duplicates after bad_chars = /^[\-]|[^0-9A-Za-z\.\-\_]|[\.]+$/ .map {|t| t.gsub(bad_chars, '') }.uniq end |
#tag_dirs ⇒ Object
44 45 46 |
# File 'lib/tagmv/filesystem.rb', line 44 def tag_dirs .map {|x| x.gsub(/$/, '-') } end |
#tags_in_order ⇒ Object
38 39 40 41 42 |
# File 'lib/tagmv/filesystem.rb', line 38 def return unless reorder ( | tag_order) & end |
#target_dir ⇒ Object
48 49 50 |
# File 'lib/tagmv/filesystem.rb', line 48 def target_dir File.join(Filesystem.root, *tag_dirs) end |
#transfer ⇒ Object
67 68 69 |
# File 'lib/tagmv/filesystem.rb', line 67 def transfer prepare_dir && move_files end |