Class: MasterView::AutoCopy

Inherits:
Object
  • Object
show all
Defined in:
lib/masterview/extras/auto_copy.rb

Overview

Usage: Can be used as an isolated instance by doing the following autocopy = AutoCopy.new(src_mio, dst_mio, [‘*.gif’, ‘*.png’]) pathnames_copied = autocopy.copy_updated_files

or

Register the the autocopy directories and patterns with AutoCopy and invoke to copy all AutoCopy.clear_registrations # clears out existing registrations AutoCopy.register(src_mio, dst_mio, [‘*.gif’, ‘*.png’]) AutoCopy.register(another_src_mio, another_dst_mio, [‘*.css’]) pathnames_copied = AutoCopy.copy_all_updated_files

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_mio_tree, dest_mio_tree, filename_patterns = ['*']) ⇒ AutoCopy

inialize an autocopy instance

source_mio_tree = tree where we are copying from dest_path = tree where we are copying to filename_patterns = array of glob style pattern of files to copy (defaults to [‘*’] all files and subdirectories) (see File.fnmatch)



23
24
25
26
27
28
# File 'lib/masterview/extras/auto_copy.rb', line 23

def initialize(source_mio_tree, dest_mio_tree, filename_patterns = ['*'])
  @source_mio_tree = source_mio_tree
  @dest_mio_tree = dest_mio_tree
  @filename_patterns = filename_patterns
  @mtimes = {}
end

Class Method Details

.clear_registrationsObject

clear all registrations



58
59
60
# File 'lib/masterview/extras/auto_copy.rb', line 58

def self.clear_registrations
  @@src_dst_registrations = []
end

.copy_all_updated_filesObject

perform autocopy on all registered src_dst’s returns array of paths that were copied



70
71
72
73
74
# File 'lib/masterview/extras/auto_copy.rb', line 70

def self.copy_all_updated_files
  paths_copied = []
  self.registrations.each { |src_dst| paths_copied << src_dst.copy_updated_files }
  paths_copied.flatten
end

.register(source_mio_tree, dest_mio_tree, filename_patterns = ['*']) ⇒ Object

register a source, destination, and array of filename patterns with AutoCopy



52
53
54
55
# File 'lib/masterview/extras/auto_copy.rb', line 52

def self.register(source_mio_tree, dest_mio_tree, filename_patterns = ['*'])
  @@src_dst_registrations ||= []
  @@src_dst_registrations << AutoCopy.new(source_mio_tree, dest_mio_tree, filename_patterns)
end

.registrationsObject

accessor for registrations



63
64
65
66
# File 'lib/masterview/extras/auto_copy.rb', line 63

def self.registrations
  @@src_dst_registrations ||= []
  @@src_dst_registrations
end

Instance Method Details

#copy_updated_filesObject

Auto copy files like stylesheets, images, javascript from a source directory into the Rails runtime location (usually under public folder). Checks modified time to see if different than last check and if so then copies file. Returns array of paths that were copied



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/masterview/extras/auto_copy.rb', line 35

def copy_updated_files
  paths_copied = []
  @filename_patterns.each do |filename_pattern|
    @source_mio_tree.find(:pattern => filename_pattern) do |mio|
      mtime = mio.mtime
      unless @mtimes[mio.pathname.to_s] == mtime
        @dest_mio_tree.path(mio.pathname).write(mio.read)
        @mtimes[mio.pathname.to_s] = mtime
        paths_copied << mio.pathname.to_s
      end
    end
  end
  paths_copied
end