class Alexandria::BookProviders

FIXME: Use delegation instead of inheritance.

Constants

SEARCH_BY_KEYWORD

Attributes

abstract_classes[R]

Public Class Methods

abstract_classes() click to toggle source
# File lib/alexandria/book_providers.rb, line 372
def self.abstract_classes
  instance.abstract_classes
end
list() click to toggle source
# File lib/alexandria/book_providers.rb, line 368
def self.list
  instance
end
new() click to toggle source
# File lib/alexandria/book_providers.rb, line 310
def initialize
  @prefs = Alexandria::Preferences.instance
  @abstract_classes = []
  update_priority
end

Public Instance Methods

update_priority() click to toggle source
# File lib/alexandria/book_providers.rb, line 316
def update_priority
  # This is weird code that sorts through the list of classes brought
  # in by requires and sorts through whether they are 'Abstract' or not,
  # adding their names to @prefs.

  @abstract_classes.clear
  providers = {}
  self.class.constants.each do |constant|
    md = /(.+)Provider$/.match(constant)
    next unless md

    klass = self.class.module_eval(constant.to_s)
    if klass < AbstractProvider &&
        (klass != GenericProvider) &&
        (klass != WebsiteBasedProvider)

      if klass.abstract?
        @abstract_classes << klass
      else
        providers[md[1]] = klass.instance
      end
    end
  end
  if (ary = @prefs.get_variable :abstract_providers)
    ary.each do |name|
      md = /^(.+)_/.match(name)
      next unless md

      klass_name = md[1] + "Provider"
      klass = @abstract_classes.find { |x| x.name.include?(klass_name) }
      next unless klass

      fullname = @prefs.send(name.downcase + "_name")
      next unless fullname

      instance = klass.new
      instance.name = name
      instance.fullname = fullname
      instance.prefs.read
      providers[name] = instance
    end
  end
  clear
  rejig_providers_priority
  priority = (@prefs.providers_priority || [])
  priority.map!(&:strip)
  rest = providers.keys - priority
  priority.each { |pname| self << providers[pname] }
  rest.sort.each { |pname| self << providers[pname] }
  compact!
end

Private Instance Methods

rejig_providers_priority() click to toggle source
# File lib/alexandria/book_providers.rb, line 378
def rejig_providers_priority
  priority = (@prefs.providers_priority || [])
  return if priority.empty?

  changed = false

  if (ecs_index = priority.index("AmazonECS"))
    priority[ecs_index] = "Amazon" # replace legacy "AmazonECS" name
    priority.uniq! # remove any other "Amazon" from the list
    changed = true
  end
  if (worldcat_index = priority.index("Worldcat"))
    priority[worldcat_index] = "WorldCat"
    changed = true
  end
  if (adlibris_index = priority.index("Adlibris"))
    priority[adlibris_index] = "AdLibris"
    changed = true
  end
  @prefs.providers_priority = priority if changed
end