Module: Farscape::Plugins
- Included in:
- Farscape
- Defined in:
- lib/plugins/plugins.rb
Class Method Summary collapse
- .collect_values(hashes) ⇒ Object
- .construct_stack(plugins) ⇒ Object
- .disable(name_or_type, disabling_rules, plugins) ⇒ Object
- .disabled?(plugins, disabling_rules, options) ⇒ Boolean
- .disabled_plugins(plugins) ⇒ Object
- .enable(name_or_type, disabling_rules, plugins) ⇒ Object
- .enabled?(plugins, disabling_rules, options) ⇒ Boolean
- .enabled_plugins(plugins) ⇒ Object
- .extensions(plugins) ⇒ Object
- .find_attr_intersect(master_hash, selector_hash) ⇒ Object
-
.includes_middleware?(list, middleware) ⇒ Boolean
Search a list for a given middleware by either its class or the type of its originating plugin.
- .normalize_selector(name_or_type) ⇒ Object
-
.order_middleware(mw_1, mw_2) ⇒ Object
Used by PartiallyOrderedList to implement the before: and after: options.
- .set_plugin_states(name_or_type, condition, plugins) ⇒ Object
-
.why_disabled(plugins, disabling_rules, options) ⇒ Object
If the middleware has been disabled by name, return the name Else if by type, return the type.
Class Method Details
.collect_values(hashes) ⇒ Object
99 100 101 |
# 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) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ Object
32 33 34 35 36 |
# 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) ⇒ Boolean
22 23 24 25 26 |
# File 'lib/plugins/plugins.rb', line 22 def self.disabled?(plugins, disabling_rules, ) = normalize_selector() return plugins[[:name]][:enabled] if .include?([:name]) why_disabled(plugins, disabling_rules, ).any? end |
.disabled_plugins(plugins) ⇒ Object
8 9 10 |
# 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) ⇒ Object
38 39 40 41 42 |
# 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) ⇒ Boolean
28 29 30 |
# File 'lib/plugins/plugins.rb', line 28 def self.enabled?(plugins, disabling_rules, ) !self.disabled?(plugins, disabling_rules, ) end |
.enabled_plugins(plugins) ⇒ Object
4 5 6 |
# File 'lib/plugins/plugins.rb', line 4 def self.enabled_plugins(plugins) plugins.select { |plugin| plugins[plugin][:enabled] } end |
.extensions(plugins) ⇒ Object
94 95 96 97 |
# 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) ⇒ Object
82 83 84 85 86 |
# 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) ⇒ Boolean
Search a list for a given middleware by either its class or the type of its originating plugin
89 90 91 92 |
# 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) ⇒ Object
64 65 66 |
# 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) ⇒ Object
Used by PartiallyOrderedList to implement the before: and after: options
69 70 71 72 73 74 75 76 77 78 79 80 |
# 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) ⇒ Object
44 45 46 47 48 49 |
# 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) ⇒ Object
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
15 16 17 18 19 20 |
# File 'lib/plugins/plugins.rb', line 15 def self.why_disabled(plugins, disabling_rules, ) maybe = disabling_rules.map { |hash| hash.select { |k,v| k if v == [k] } } maybe |= [disabled_plugins(plugins)[[:name]]] maybe |= [:default_state] if [:default_state] == :disabled maybe.compact end |