Class: Bard::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/bard/plugin.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Plugin

Returns a new instance of Plugin.



34
35
36
37
38
39
40
41
# File 'lib/bard/plugin.rb', line 34

def initialize(name)
  @name = name.to_sym
  @cli_modules = []
  @cli_requires = []
  @target_methods = {}
  @config_methods = {}
  @requires = []
end

Class Attribute Details

.registryObject (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_modulesObject (readonly)

Returns the value of attribute cli_modules.



32
33
34
# File 'lib/bard/plugin.rb', line 32

def cli_modules
  @cli_modules
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/bard/plugin.rb', line 32

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

.allObject



18
19
20
# File 'lib/bard/plugin.rb', line 18

def all
  @registry.values
end

.load_all!Object



22
23
24
25
# File 'lib/bard/plugin.rb', line 22

def load_all!
  Dir[File.join(__dir__, "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



27
28
29
# File 'lib/bard/plugin.rb', line 27

def reset!
  @registry = {}
end

Instance Method Details

#apply!Object

Apply plugin to the system (non-CLI parts)



63
64
65
66
67
# File 'lib/bard/plugin.rb', line 63

def apply!
  @requires.each { |path| require path }
  apply_target_methods!
  apply_config_methods!
end

#apply_to_cli(cli_class) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/bard/plugin.rb', line 69

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



49
50
51
52
# File 'lib/bard/plugin.rb', line 49

def cli(mod, require: nil)
  @cli_requires << require if require
  @cli_modules << mod
end

#config_method(name, &block) ⇒ Object



58
59
60
# File 'lib/bard/plugin.rb', line 58

def config_method(name, &block)
  @config_methods[name] = block
end

#require_file(path) ⇒ Object

DSL methods for defining plugins



45
46
47
# File 'lib/bard/plugin.rb', line 45

def require_file(path)
  @requires << path
end

#target_method(name, &block) ⇒ Object



54
55
56
# File 'lib/bard/plugin.rb', line 54

def target_method(name, &block)
  @target_methods[name] = block
end