Class: Rivendell::Import::Base
- Inherits:
-
Object
- Object
- Rivendell::Import::Base
- Defined in:
- lib/rivendell/import/base.rb
Constant Summary collapse
- @@default_to_prepare =
nil
Instance Attribute Summary collapse
-
#file_patterns ⇒ Object
Returns the value of attribute file_patterns.
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
-
#to_prepare ⇒ Object
Returns the value of attribute to_prepare.
-
#workers ⇒ Object
readonly
Returns the value of attribute workers.
Instance Method Summary collapse
- #create_task(file) ⇒ Object
- #directory(directory) ⇒ Object
- #file(path, base_directory = nil) ⇒ Object
- #ignore?(path) ⇒ Boolean
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #listen(directory, options = {}) ⇒ Object
- #prepare_task(task) ⇒ Object
- #process(*paths) ⇒ Object
Constructor Details
Instance Attribute Details
#file_patterns ⇒ Object
Returns the value of attribute file_patterns.
48 49 50 |
# File 'lib/rivendell/import/base.rb', line 48 def file_patterns @file_patterns end |
#tasks ⇒ Object (readonly)
Returns the value of attribute tasks.
6 7 8 |
# File 'lib/rivendell/import/base.rb', line 6 def tasks @tasks end |
#to_prepare ⇒ Object
Returns the value of attribute to_prepare.
16 17 18 |
# File 'lib/rivendell/import/base.rb', line 16 def to_prepare @to_prepare end |
#workers ⇒ Object (readonly)
Returns the value of attribute workers.
6 7 8 |
# File 'lib/rivendell/import/base.rb', line 6 def workers @workers end |
Instance Method Details
#create_task(file) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/rivendell/import/base.rb', line 73 def create_task(file) Rivendell::Import.logger.debug "Create task for #{file}" tasks.create(file) do |task| prepare_task task end end |
#directory(directory) ⇒ Object
67 68 69 70 71 |
# File 'lib/rivendell/import/base.rb', line 67 def directory(directory) Dir[::File.join(directory, "**/*")].each do |file| file(file, directory) if ::File.file?(file) end end |
#file(path, base_directory = nil) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/rivendell/import/base.rb', line 59 def file(path, base_directory = nil) return if ignore? path path = ::File.(path, base_directory) file = Rivendell::Import::File.new path, :base_directory => base_directory create_task file end |
#ignore?(path) ⇒ Boolean
53 54 55 56 57 |
# File 'lib/rivendell/import/base.rb', line 53 def ignore?(path) not file_patterns.any? do |file_pattern| ::File.fnmatch file_pattern, path, ::File::FNM_PATHNAME | ::File::FNM_CASEFOLD end end |
#listen(directory, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rivendell/import/base.rb', line 21 def listen(directory, = {}) workers << Worker.new(self).start unless [:dry_run] Rivendell::Import.logger.info "Listen files in #{directory}" callback = Proc.new do |modified, added, removed| # Rivendell::Import.logger.debug [modified, added, removed].inspect added.each do |file| begin Rivendell::Import.logger.debug "Detected file '#{file}'" file(file, directory) rescue Exception => e Rivendell::Import.logger.error "Task creation failed : #{e}" Rivendell::Import.logger.debug e.backtrace.join("\n") end end end Listen.to(directory, latency: 1, wait_for_delay: 5).change(&callback).start! end |
#prepare_task(task) ⇒ Object
80 81 82 |
# File 'lib/rivendell/import/base.rb', line 80 def prepare_task(task) task.prepare(&to_prepare) if to_prepare end |
#process(*paths) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/rivendell/import/base.rb', line 41 def process(*paths) paths.flatten.each do |path| method = ::File.directory?(path) ? :directory : :file send method, path end end |