Class: Basket::Base

Inherits:
Object show all
Includes:
FileUtils, HasLogger
Defined in:
lib/basket.rb

Constant Summary collapse

INBOX =
"inbox"
PENDING =
"pending"
ARCHIVE =
"archive"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasLogger

included

Constructor Details

#initialize(root, opts = {}) ⇒ Base

Returns a new instance of Base.



59
60
61
62
63
64
65
66
67
# File 'lib/basket.rb', line 59

def initialize(root, opts={})
  @root = root
  @inbox   = opts.delete(:inbox)   || INBOX
  @pending = opts.delete(:pending) || PENDING
  @archive = opts.delete(:archive) || ARCHIVE
  @other   = opts.delete(:other)   || []
  @logdev  = opts.delete(:logdev)  || "/dev/null"
  @opts    = opts
end

Instance Attribute Details

#inboxObject

Returns the value of attribute inbox.



52
53
54
# File 'lib/basket.rb', line 52

def inbox
  @inbox
end

#otherObject

Returns the value of attribute other.



54
55
56
# File 'lib/basket.rb', line 54

def other
  @other
end

#pendingObject

Returns the value of attribute pending.



53
54
55
# File 'lib/basket.rb', line 53

def pending
  @pending
end

#rootObject

Returns the value of attribute root.



51
52
53
# File 'lib/basket.rb', line 51

def root
  @root
end

Instance Method Details

#process(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/basket.rb', line 69

def process(&block)
  create_directories 
  files = Dir[@root/@inbox/"*"]
  logger.debug(["#{files.size} files in", @root/@inbox/"*"])
  send_args = @opts[:workers] ? [:forkoff!, {:processes => @opts[:workers]}] : [:each]
  i = 0
  files.send(*send_args) do |file|
    pending_file = @root/@pending/File.basename(file)
    logger.info [:mv, file, pending_file]
    mv file, pending_file

    to_mix = mixin
    pending_file.meta_eval { include to_mix }
    result = block.arity > 1 ? block.call(pending_file, i) : block.call(pending_file)

    if @opts[:conditional]
      destination = result ? @root/"success" : @root/"fail"
      logger.info [:mv, pending_file, destination]
      mv pending_file, destination
    elsif @other.size > 0
      # don't mv anything
    else
      logger.info [:mv, pending_file, @root/@archive]
      mv pending_file, @root/@archive
    end
    i += 1
  end
end