Class: Vop::Plugin
- Inherits:
-
ThingWithParams
- Object
- ThingWithParams
- Vop::Plugin
- Defined in:
- lib/vop/objects/plugin.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#dependencies ⇒ Object
Returns the value of attribute dependencies.
-
#description ⇒ Object
Returns the value of attribute description.
-
#external_dependencies ⇒ Object
Returns the value of attribute external_dependencies.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#op ⇒ Object
readonly
Returns the value of attribute op.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Attributes inherited from ThingWithParams
Instance Method Summary collapse
- #auto_load? ⇒ Boolean
- #call_hook(name, *args) ⇒ Object
- #has_hook?(name) ⇒ Boolean
- #hook(name, &block) ⇒ Object
- #init ⇒ Object
-
#initialize(op, plugin_name, plugin_path, options = {}) ⇒ Plugin
constructor
A new instance of Plugin.
- #inject_helpers(target, sub_type_name = nil) ⇒ Object
- #inspect ⇒ Object
- #load ⇒ Object
- #load_code_from_dir(type_name) ⇒ Object
- #load_commands ⇒ Object
- #load_config ⇒ Object
- #load_default_config ⇒ Object
- #load_entities ⇒ Object
- #load_filters ⇒ Object
- #load_gem_dependencies ⇒ Object
- #load_helpers ⇒ Object
- #plugin_dir(name) ⇒ Object
- #template(name) ⇒ Object
- #template_path(name_or_sym) ⇒ Object
- #to_s ⇒ Object
- #write_config ⇒ Object
Methods inherited from ThingWithParams
Constructor Details
#initialize(op, plugin_name, plugin_path, options = {}) ⇒ Plugin
Returns a new instance of Plugin.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/vop/objects/plugin.rb', line 26 def initialize(op, plugin_name, plugin_path, = {}) super() @op = op @name = plugin_name @path = plugin_path defaults = { auto_load: true } @options = defaults.merge() @description = nil @state = {} @config_file_name = File.join(op.plugin_config_path, plugin_name + ".json") @config = {} @dependencies = [] @external_dependencies = Hash.new { |h, k| h[k] = [] } @hooks = {} end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
17 18 19 |
# File 'lib/vop/objects/plugin.rb', line 17 def commands @commands end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
21 22 23 |
# File 'lib/vop/objects/plugin.rb', line 21 def config @config end |
#dependencies ⇒ Object
Returns the value of attribute dependencies.
23 24 25 |
# File 'lib/vop/objects/plugin.rb', line 23 def dependencies @dependencies end |
#description ⇒ Object
Returns the value of attribute description.
15 16 17 |
# File 'lib/vop/objects/plugin.rb', line 15 def description @description end |
#external_dependencies ⇒ Object
Returns the value of attribute external_dependencies.
24 25 26 |
# File 'lib/vop/objects/plugin.rb', line 24 def external_dependencies @external_dependencies end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/vop/objects/plugin.rb', line 14 def name @name end |
#op ⇒ Object (readonly)
Returns the value of attribute op.
12 13 14 |
# File 'lib/vop/objects/plugin.rb', line 12 def op @op end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/vop/objects/plugin.rb', line 16 def @options end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
19 20 21 |
# File 'lib/vop/objects/plugin.rb', line 19 def sources @sources end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
20 21 22 |
# File 'lib/vop/objects/plugin.rb', line 20 def state @state end |
Instance Method Details
#auto_load? ⇒ Boolean
51 52 53 |
# File 'lib/vop/objects/plugin.rb', line 51 def auto_load? @options[:auto_load] end |
#call_hook(name, *args) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/vop/objects/plugin.rb', line 225 def call_hook(name, *args) result = nil if @hooks.has_key? name begin result = @hooks[name].call(self, *args) rescue => e $logger.error "hit a problem while running hook #{name} on #{self.name}" raise e end end result end |
#has_hook?(name) ⇒ Boolean
221 222 223 |
# File 'lib/vop/objects/plugin.rb', line 221 def has_hook?(name) ! @hooks[name.to_sym].nil? end |
#hook(name, &block) ⇒ Object
217 218 219 |
# File 'lib/vop/objects/plugin.rb', line 217 def hook(name, &block) @hooks[name.to_sym] = block end |
#init ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/vop/objects/plugin.rb', line 80 def init # TODO proceed only if auto_load call_hook :init load_entities load_commands load_filters #@op.call_global_hook :plugin_loaded, self end |
#inject_helpers(target, sub_type_name = nil) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/vop/objects/plugin.rb', line 186 def inject_helpers(target, sub_type_name = nil) type_name = 'helpers' plugins_to_load_helpers_from = [ self ] self.dependencies.each do |name| other = @op.plugin(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| helper_sources = other_plugin.sources[type_name] next if helper_sources.size == 0 helper_module = Module.new() helper_sources.each do |name, helper| begin helper_module.class_eval helper[:code] rescue Exception => e $stderr.puts("could not read helper #{name} : #{e.}") raise e end end target.extend helper_module end end |
#inspect ⇒ Object
59 60 61 62 63 64 |
# File 'lib/vop/objects/plugin.rb', line 59 def inspect { name: name, path: @path }.to_json() end |
#load ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vop/objects/plugin.rb', line 66 def load $logger.debug "plugin init : #{@name}" @sources = Hash.new { |h, k| h[k] = {} } @config = {} load_helpers load_default_config load_config load_gem_dependencies unless ENV["VOP_IGNORE_PLUGINS"] end |
#load_code_from_dir(type_name) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/vop/objects/plugin.rb', line 140 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].join(".") $logger.debug(" #{type_name} << #{full_name}") @sources[type_name][full_name] = { :file_name => file_name, :code => File.read(file_name) } end end end |
#load_commands ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/vop/objects/plugin.rb', line 165 def load_commands loader = CommandLoader.new(self) load_code_from_dir :commands @commands = loader.read_sources @sources[:commands] @op << @commands unless @commands.empty? end |
#load_config ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/vop/objects/plugin.rb', line 103 def load_config $logger.debug "looking for config at #{@config_file_name}" if File.exists? @config_file_name raw = nil begin raw = IO.read(@config_file_name) config_from_file = JSON.parse(raw) @config.merge! config_from_file $logger.debug "plugin config loaded from #{@config_file_name}" rescue => e $logger.error "could not read JSON config from #{@config_file_name} (#{e.}), ignoring:\n#{raw}" end end end |
#load_default_config ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/vop/objects/plugin.rb', line 95 def load_default_config params.each do |param| if param..has_key?(:default) @config[param.name] = param.[:default] end end end |
#load_entities ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/vop/objects/plugin.rb', line 157 def load_entities loader = EntityLoader.new(self) load_code_from_dir :entities @entities = loader.read_sources @sources[:entities] @op << @entities unless @entities.empty? end |
#load_filters ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/vop/objects/plugin.rb', line 173 def load_filters loader = FilterLoader.new(self) load_code_from_dir :filters @filters = loader.read_sources @sources[:filters] @op << @filters unless @filters.empty? end |
#load_gem_dependencies ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/vop/objects/plugin.rb', line 128 def load_gem_dependencies gems = @external_dependencies[:gem] return unless gems.any? $logger.debug "loading gem dependencies : #{gems}" gems.each do |g| gem, = g gem_name = [:require] || gem require gem_name end end |
#load_helpers ⇒ Object
181 182 183 184 |
# File 'lib/vop/objects/plugin.rb', line 181 def load_helpers #load_code_from_dir :helpers load_code_from_dir("helpers") # TODO unify (symbol vs. string) with load_commands above end |
#plugin_dir(name) ⇒ Object
91 92 93 |
# File 'lib/vop/objects/plugin.rb', line 91 def plugin_dir(name) File.join(@path, name.to_s) end |
#template(name) ⇒ Object
244 245 246 |
# File 'lib/vop/objects/plugin.rb', line 244 def template(name) @op.read_template(template_path(name)) end |
#template_path(name_or_sym) ⇒ Object
238 239 240 241 242 |
# File 'lib/vop/objects/plugin.rb', line 238 def template_path(name_or_sym) name = name_or_sym.to_s name += ".erb" unless name.end_with? ".erb" File.join(plugin_dir(:templates), name) end |
#to_s ⇒ Object
55 56 57 |
# File 'lib/vop/objects/plugin.rb', line 55 def to_s "Vop::Plugin #{name}" end |
#write_config ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/vop/objects/plugin.rb', line 118 def write_config $logger.info "writing config into #{@op.plugin_config_path}" unless Dir.exists?(@op.plugin_config_path) FileUtils.mkdir_p @op.plugin_config_path end File.open(@config_file_name, "w") do |file| file.write @config.to_json() end end |