module MotherBrain::Gear
The base module for defining new Gears for a plugin. Any class including this module and registered with {MotherBrain} as a {Gear} will be available for use in the plugin DSL.
Gears represent the proverbial knobs and levers that can be used to manipulate a {Component}.
@example Defining a new Gear
class Twitter < MB::Gear::Base register_gear :twitter end
@example Plugin
DSL usage
component "news_post" do twitter do ... end end
Constants
- RESERVED_KEYWORDS
Public Class Methods
Return a Set containing all of the registered Gears that can be used within a MotherBrain
plugin.
@return [Set<MotherBrain::Gear>]
# File lib/mb/gear.rb, line 85 def all @all ||= Set.new end
Clears all of the registered Gears.
@return [Set]
an empty Set
# File lib/mb/gear.rb, line 109 def clear! @all = Set.new end
# File lib/mb/gear.rb, line 120 def element_name(klass) klass.keyword end
@param [Symbol] keyword
@return [MotherBrain::Gear, nil]
# File lib/mb/gear.rb, line 116 def find_by_keyword(keyword) all.find { |klass| klass.keyword == keyword } end
# File lib/mb/gear.rb, line 124 def get_fun(klass) element_name(klass) end
Registers a given Class as a Gear
to be used within MotherBrain
plugins. This function is automatically called when {MotherBrain::Gear} is included into a Class. You probably don’t want to call this function directly.
@api private
@param [~MotherBrain::Gear] klass
# File lib/mb/gear.rb, line 75 def register(klass) validate_keyword(klass.keyword) all.add(klass) end
Clears all of the registered Gears and then traverses the ObjectSpace to find all classes which include {MotherBrain::Gear} and calls {::register} with each.
@return [Set<MotherBrain::Gear>]
the Set of registered Gears
# File lib/mb/gear.rb, line 94 def reload! clear! ObjectSpace.each_object(::Module).each do |mod| if mod < MB::Gear register(mod) end end all end
Private Class Methods
Determine if the given keyword is valid
@param [Symbol] keyword
@raise [ReservedGearKeyword] if the given keyword is reserved @raise [DuplicateGearKeyword] if the given keyword has already been registered
@return [Boolean]
# File lib/mb/gear.rb, line 138 def validate_keyword(keyword) if RESERVED_KEYWORDS.include?(keyword) reserved = RESERVED_KEYWORDS.collect { |key| "'#{key}'" } raise ReservedGearKeyword, "'#{keyword}' is a reserved keyword. Reserved Keywords: #{reserved.join(', ')}." end culprit = find_by_keyword(keyword) unless culprit.nil? raise DuplicateGearKeyword, "Keyword '#{keyword}' already used by #{culprit}" end true end