Class: Bard::Plugin
- Inherits:
-
Object
- Object
- Bard::Plugin
- Defined in:
- lib/bard/plugin.rb
Class Attribute Summary collapse
-
.registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Attribute Summary collapse
-
#cli_modules ⇒ Object
readonly
Returns the value of attribute cli_modules.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .[](name) ⇒ Object
- .all ⇒ Object
- .load_all! ⇒ Object
- .register(name, &block) ⇒ Object
- .reset! ⇒ Object
Instance Method Summary collapse
-
#apply! ⇒ Object
Apply plugin to the system (non-CLI parts).
- #apply_to_cli(cli_class) ⇒ Object
- #cli(mod, require: nil) ⇒ Object
- #config_method(name, &block) ⇒ Object
-
#initialize(name) ⇒ Plugin
constructor
A new instance of Plugin.
-
#require_file(path) ⇒ Object
DSL methods for defining plugins.
- #target_method(name, &block) ⇒ Object
Constructor Details
#initialize(name) ⇒ Plugin
Returns a new instance of Plugin.
35 36 37 38 39 40 41 42 |
# File 'lib/bard/plugin.rb', line 35 def initialize(name) @name = name.to_sym @cli_modules = [] @cli_requires = [] @target_methods = {} @config_methods = {} @requires = [] end |
Class Attribute Details
.registry ⇒ Object (readonly)
Returns the value of attribute registry.
6 7 8 |
# File 'lib/bard/plugin.rb', line 6 def registry @registry end |
Instance Attribute Details
#cli_modules ⇒ Object (readonly)
Returns the value of attribute cli_modules.
33 34 35 |
# File 'lib/bard/plugin.rb', line 33 def cli_modules @cli_modules end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
33 34 35 |
# File 'lib/bard/plugin.rb', line 33 def name @name end |
Class Method Details
.[](name) ⇒ Object
14 15 16 |
# File 'lib/bard/plugin.rb', line 14 def [](name) @registry[name.to_sym] end |
.all ⇒ Object
18 19 20 |
# File 'lib/bard/plugin.rb', line 18 def all @registry.values end |
.load_all! ⇒ Object
22 23 24 25 26 |
# File 'lib/bard/plugin.rb', line 22 def load_all! Dir[File.join(__dir__, "plugins", "*.rb")].sort.each { |f| require f } Dir[File.join(Dir.pwd, "lib", "bard", "plugins", "*.rb")].sort.each { |f| require f } all.each(&:apply!) end |
.register(name, &block) ⇒ Object
8 9 10 11 12 |
# File 'lib/bard/plugin.rb', line 8 def register(name, &block) plugin = new(name) plugin.instance_eval(&block) if block @registry[name.to_sym] = plugin end |
.reset! ⇒ Object
28 29 30 |
# File 'lib/bard/plugin.rb', line 28 def reset! @registry = {} end |
Instance Method Details
#apply! ⇒ Object
Apply plugin to the system (non-CLI parts)
64 65 66 67 68 |
# File 'lib/bard/plugin.rb', line 64 def apply! @requires.each { |path| require path } apply_target_methods! apply_config_methods! end |
#apply_to_cli(cli_class) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/bard/plugin.rb', line 70 def apply_to_cli(cli_class) @cli_requires.each { |path| require path } @cli_modules.each do |mod| mod = resolve_constant(mod) if mod.is_a?(String) mod.setup(cli_class) end end |
#cli(mod, require: nil) ⇒ Object
50 51 52 53 |
# File 'lib/bard/plugin.rb', line 50 def cli(mod, require: nil) @cli_requires << require if require @cli_modules << mod end |
#config_method(name, &block) ⇒ Object
59 60 61 |
# File 'lib/bard/plugin.rb', line 59 def config_method(name, &block) @config_methods[name] = block end |
#require_file(path) ⇒ Object
DSL methods for defining plugins
46 47 48 |
# File 'lib/bard/plugin.rb', line 46 def require_file(path) @requires << path end |
#target_method(name, &block) ⇒ Object
55 56 57 |
# File 'lib/bard/plugin.rb', line 55 def target_method(name, &block) @target_methods[name] = block end |