module Farscape::Plugins

Public Class Methods

collect_values(hashes) click to toggle source
# File lib/plugins/plugins.rb, line 99
def self.collect_values(hashes)
  hashes.reduce({}) { |h1, h2| h1.merge(h2) { |k, l1, l2| l1+l2 } }
end
construct_stack(plugins) click to toggle source
# File lib/plugins/plugins.rb, line 51
def self.construct_stack(plugins)
  stack = PartiallyOrderedList.new { |m,n| order_middleware(m,n) }
  plugins.each do |_, plugin|
    [*plugin[:middleware]].each do |middleware|
      middleware = {class: middleware} unless middleware.is_a?(Hash)
      middleware[:type] = plugin[:type]
      middleware[:plugin] = plugin[:name]
      stack.add(middleware)
    end
  end
  stack
end
disable(name_or_type, disabling_rules, plugins) click to toggle source
# File lib/plugins/plugins.rb, line 32
def self.disable(name_or_type, disabling_rules, plugins)
  name_or_type = self.normalize_selector(name_or_type)
  plugins = set_plugin_states(name_or_type, false, plugins)
  [disabling_rules << name_or_type, plugins]      
end
disabled?(plugins, disabling_rules, options) click to toggle source
# File lib/plugins/plugins.rb, line 22
def self.disabled?(plugins, disabling_rules, options)
  options = normalize_selector(options)
  return plugins[options[:name]][:enabled] if options.include?([:name])
  why_disabled(plugins, disabling_rules, options).any?
end
disabled_plugins(plugins) click to toggle source
# File lib/plugins/plugins.rb, line 8
def self.disabled_plugins(plugins)
  plugins.reject { |plugin| plugins[plugin][:enabled] }
end
enable(name_or_type, disabling_rules, plugins) click to toggle source
# File lib/plugins/plugins.rb, line 38
def self.enable(name_or_type, disabling_rules, plugins)
  name_or_type = normalize_selector(name_or_type)
  plugins = set_plugin_states(name_or_type, true, plugins)
  [disabling_rules.reject {|k| k == name_or_type}, plugins]      
end
enabled?(plugins, disabling_rules, options) click to toggle source
# File lib/plugins/plugins.rb, line 28
def self.enabled?(plugins, disabling_rules, options)
  !self.disabled?(plugins, disabling_rules, options)
end
enabled_plugins(plugins) click to toggle source
# File lib/plugins/plugins.rb, line 4
def self.enabled_plugins(plugins)
  plugins.select { |plugin| plugins[plugin][:enabled] }
end
extensions(plugins) click to toggle source
# File lib/plugins/plugins.rb, line 94
def self.extensions(plugins)
  plugs = plugins.map { |_, hash| hash[:extensions] }.compact
  collect_values(plugs)
end
find_attr_intersect(master_hash, selector_hash) click to toggle source
# File lib/plugins/plugins.rb, line 82
def self.find_attr_intersect(master_hash, selector_hash)
  master_hash.map do |mkey, mval|
    selector_hash.map { |skey, sval| mkey if mval[skey] == sval }
  end.flatten.compact
end
includes_middleware?(list, middleware) click to toggle source

Search a list for a given middleware by either its class or the type of its originating plugin

# File lib/plugins/plugins.rb, line 89
def self.includes_middleware?(list, middleware)
  list = [*list]
  list.map(&:to_s).include?(middleware[:class].to_s) || list.include?(middleware[:type])
end
normalize_selector(name_or_type) click to toggle source
# File lib/plugins/plugins.rb, line 64
def self.normalize_selector(name_or_type)
  name_or_type.is_a?(Hash) ? name_or_type : { name: name_or_type, type: name_or_type}
end
order_middleware(mw_1, mw_2) click to toggle source

Used by PartiallyOrderedList to implement the before: and after: options

# File lib/plugins/plugins.rb, line 69
def self.order_middleware(mw_1, mw_2)
  case
  when includes_middleware?(mw_1[:before],mw_2)
    -1
  when includes_middleware?(mw_1[:after],mw_2)
    1
  when includes_middleware?(mw_2[:before],mw_1)
    1
  when includes_middleware?(mw_2[:after],mw_1)
    -1
  end
end
set_plugin_states(name_or_type, condition, plugins) click to toggle source
# File lib/plugins/plugins.rb, line 44
def self.set_plugin_states(name_or_type, condition, plugins)
  plugins = Marshal.load( Marshal.dump(plugins) ) # TODO: This is super inefficient, figure out a good deep_dup
  selected_plugins = find_attr_intersect(plugins, name_or_type)
  selected_plugins.each { |plugin| plugins[plugin][:enabled] = condition }
  plugins
end
why_disabled(plugins, disabling_rules, options) click to toggle source

If the middleware has been disabled by name, return the name Else if by type, return the type. Else if :default_state was passed in return :default_state

# File lib/plugins/plugins.rb, line 15
def self.why_disabled(plugins, disabling_rules, options)
  maybe = disabling_rules.map { |hash| hash.select { |k,v| k if v == options[k] } }
  maybe |= [disabled_plugins(plugins)[options[:name]]]
  maybe |= [:default_state] if options[:default_state] == :disabled
  maybe.compact
end