Class: Vop::Plugin

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, plugin_name, plugin_path) ⇒ Plugin

Returns a new instance of Plugin.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vop/plugin.rb', line 17

def initialize(op, plugin_name, plugin_path)
  @op = op
  @name = plugin_name
  @path = plugin_path
  @config = {}
  @dependencies = []

  # all plugins depend on 'core' (unless they are core or some murky dummy)
  independents = %w|core __root__|
  @dependencies << 'core' unless independents.include? plugin_name

  @state = {}
  @hooks = {}

  @sources = Hash.new { |h, k| h[k] = {} }
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



11
12
13
# File 'lib/vop/plugin.rb', line 11

def commands
  @commands
end

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/vop/plugin.rb', line 12

def config
  @config
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



13
14
15
# File 'lib/vop/plugin.rb', line 13

def dependencies
  @dependencies
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/vop/plugin.rb', line 8

def name
  @name
end

#opObject (readonly)

Returns the value of attribute op.



7
8
9
# File 'lib/vop/plugin.rb', line 7

def op
  @op
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/vop/plugin.rb', line 9

def path
  @path
end

#stateObject (readonly)

Returns the value of attribute state.



15
16
17
# File 'lib/vop/plugin.rb', line 15

def state
  @state
end

Instance Method Details

#call_hook(name, *args) ⇒ Object



42
43
44
45
46
47
# File 'lib/vop/plugin.rb', line 42

def call_hook(name, *args)
  if @hooks.has_key? name
    $logger.debug "plugin #{self.name} calling hook #{name}"
    @hooks[name].call(self, *args)
  end
end

#command_source(command_name) ⇒ Object



95
96
97
# File 'lib/vop/plugin.rb', line 95

def command_source(command_name)
  @sources[:commands][command_name]
end

#helper_sources(type_name = 'helpers') ⇒ Object



89
90
91
92
93
# File 'lib/vop/plugin.rb', line 89

def helper_sources(type_name = 'helpers')
  @sources[type_name].map do |name, source|
    source[:code]
  end
end

#hook(name, &block) ⇒ Object



38
39
40
# File 'lib/vop/plugin.rb', line 38

def hook(name, &block)
  @hooks[name.to_sym] = block
end

#initObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/vop/plugin.rb', line 49

def init
  $logger.debug "plugin init #{@name}"
  call_hook :preload
  load_helpers
  load_config
  # TODO we might want to activate/register only plugins with enough config
  call_hook :init
  load_commands
  call_hook :activate
end

#inject_helpers(target, sub_type_name = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vop/plugin.rb', line 99

def inject_helpers(target, sub_type_name = nil)
  type_name = 'helpers'
  if sub_type_name
    type_name += '/' + sub_type_name
  end

  plugins_to_load_helpers_from = [ self ]

  self.dependencies.each do |name|
    other = @op.plugins[name]
    raise "can not resolve plugin dependency #{name}" unless other
    plugins_to_load_helpers_from << other
  end

  plugins_to_load_helpers_from.each do |other_plugin|
    next if other_plugin.helper_sources(type_name).size == 0
    #$logger.debug "loading helper from #{other_plugin.name} into #{target} : #{other_plugin.helper_sources.size}"

    helper_module = Module.new()
    other_plugin.helper_sources(type_name).each do |source|
      helper_module.class_eval source
    end
    target.extend helper_module
  end
end

#inspectObject



34
35
36
# File 'lib/vop/plugin.rb', line 34

def inspect
  "Vop::Plugin #{@name}"
end

#load_code_from_dir(type_name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vop/plugin.rb', line 64

def load_code_from_dir(type_name)
  dir = plugin_dir type_name
  if File.exists?(dir)
    Dir.glob(File.join(dir, '*.rb')).each do |file_name|
      name_from_file = /#{dir}\/(.+).rb$/.match(file_name).captures.first
      full_name = @name + '.' + name_from_file
      $logger.debug("  #{type_name} << #{full_name}")

      code = File.read(file_name)
      @sources[type_name][full_name] = {
        :file_name => file_name,
        :code => code
      }
    end
  else
    #$logger.debug "no #{type_name} dir found - checked for #{dir}"
  end
end

#load_commandsObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/vop/plugin.rb', line 125

def load_commands
  load_code_from_dir :commands

  loader = CommandLoader.new(self)
  @commands = loader.read_sources @sources[:commands]
  @commands.each do |name, command|
    # TODO might want to warn/debug about overrides here
    @op.eat(command)
  end
end

#load_configObject



136
137
# File 'lib/vop/plugin.rb', line 136

def load_config
end

#load_helpersObject



83
84
85
86
87
# File 'lib/vop/plugin.rb', line 83

def load_helpers
  load_code_from_dir 'helpers'
  load_code_from_dir 'helpers/command_loader'
  load_code_from_dir 'helpers/plugin_loader'
end

#plugin_dir(name) ⇒ Object



60
61
62
# File 'lib/vop/plugin.rb', line 60

def plugin_dir(name)
  @path + '/' + name.to_s
end