class MovieMasher::Service

abstraction layer for initing, upload, download, queue services

Constants

SERVICES_DIRECTORY

Attributes

__configuration[RW]
__instances[RW]
__services[RW]

Public Class Methods

__create_service(name, kind = :queue) click to toggle source
# File lib/service.rb, line 43
def self.__create_service(name, kind = :queue)
  s = nil
  return s if Service.__configuration[:disable_local] && 'file' == name.to_s
  service_config = services(kind).find{ |s| s[:name] == name.to_s }
  if service_config
    class_sym = service_config[:sym]
    unless MovieMasher.const_defined?(class_sym)
      path = service_config[:path]
      require path if File.exist?(path)
    end
    if MovieMasher.const_defined?(class_sym)
      s = MovieMasher.const_get(class_sym).new
    end
  end
  s
end
__scan_for_services(kind) click to toggle source
# File lib/service.rb, line 59
def self.__scan_for_services(kind)
  kind_fragment = "_#{kind.id2name}"
  Dir["#{SERVICES_DIRECTORY}/*/*#{kind_fragment}.rb"].map do |path|
    name = File.basename(path, '.rb')
    name[kind_fragment] = ''
    {
      name: name, path: path,
      sym: "#{name.capitalize}#{kind.id2name.capitalize}Service".to_sym
    }
  end
end
configure_services(config) click to toggle source
# File lib/service.rb, line 14
def self.configure_services(config)
  Service.__configuration = config
end
downloader(type) click to toggle source
# File lib/service.rb, line 17
def self.downloader(type)
  instance(:download, type)
end
initer(type) click to toggle source
# File lib/service.rb, line 20
def self.initer(type)
  instance(:init, type)
end
instance(kind, type) click to toggle source
# File lib/service.rb, line 70
def self.instance(kind, type)
  Service.__instances[kind] ||= {}
  if Service.__instances[kind][type].nil?
    s = __create_service(type, kind)
    Service.__instances[kind][type] =
      if s && s.configure(Service.__configuration)
        s
      else
        false
      end
  end
  Service.__instances[kind][type]
end
queues() click to toggle source
# File lib/service.rb, line 23
def self.queues
  array = []
  services(:queue).each do |hash|
    type = hash[:name]
    s = instance(:queue, type)
    array << s if s
  end
  array
end
services(kind = nil) click to toggle source
# File lib/service.rb, line 32
def self.services(kind = nil)
  kind_nil = kind.nil?
  kinds = kind_nil ? [:queue, :upload, :download, :init] : [kind.to_sym]
  kinds.each do |kind_sym|
    Service.__services[kind_sym] ||= __scan_for_services(kind_sym)
  end
  kind_nil ? Service.__services : Service.__services[kind.to_sym]
end
uploader(type) click to toggle source
# File lib/service.rb, line 40
def self.uploader(type)
  instance(:upload, type)
end

Public Instance Methods

configuration() click to toggle source
# File lib/service.rb, line 83
def configuration
  Service.__configuration
end
configure(*) click to toggle source
# File lib/service.rb, line 86
def configure(*)
  true
end