Class: Vop::Plugin

Inherits:
ThingWithParams show all
Defined in:
lib/vop/objects/plugin.rb

Instance Attribute Summary collapse

Attributes inherited from ThingWithParams

#params

Instance Method Summary collapse

Methods inherited from ThingWithParams

#param

Constructor Details

#initialize(op, plugin_name, plugin_path, options = {}) ⇒ Plugin

Returns a new instance of Plugin.


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vop/objects/plugin.rb', line 22

def initialize(op, plugin_name, plugin_path, options = {})
  super()

  @op = op
  @name = plugin_name
  @path = plugin_path

  defaults = {
    auto_load: true
  }
  @options = defaults.merge(options)

  @description = nil

  @state = {}

  @config_file_name = File.join(op.plugin_config_path, plugin_name + ".json")
  @config = {}

  @dependencies = []

  @hooks = {}
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands


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

def commands
  @commands
end

#configObject (readonly)

Returns the value of attribute config


19
20
21
# File 'lib/vop/objects/plugin.rb', line 19

def config
  @config
end

#dependenciesObject

Returns the value of attribute dependencies


20
21
22
# File 'lib/vop/objects/plugin.rb', line 20

def dependencies
  @dependencies
end

#descriptionObject

Returns the value of attribute description


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

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

#opObject (readonly)

Returns the value of attribute op


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

def op
  @op
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

#sourcesObject (readonly)

Returns the value of attribute sources


17
18
19
# File 'lib/vop/objects/plugin.rb', line 17

def sources
  @sources
end

#stateObject (readonly)

Returns the value of attribute state


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

def state
  @state
end

Instance Method Details

#call_hook(name, *args) ⇒ Object


189
190
191
192
193
194
195
# File 'lib/vop/objects/plugin.rb', line 189

def call_hook(name, *args)
  result = nil
  if @hooks.has_key? name
    result = @hooks[name].call(self, *args)
  end
  result
end

#hook(name, &block) ⇒ Object


185
186
187
# File 'lib/vop/objects/plugin.rb', line 185

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

#initObject


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vop/objects/plugin.rb', line 50

def init
  $logger.debug "plugin init : #{@name}"

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

  @config = {}

  # call_hook :preload ?
  load_helpers
  load_default_config
  load_config

  # 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


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/vop/objects/plugin.rb', line 154

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.message}")
        raise e
      end
    end

    target.extend helper_module
  end
end

#load_code_from_dir(type_name) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vop/objects/plugin.rb', line 108

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_commandsObject


133
134
135
136
137
138
139
# File 'lib/vop/objects/plugin.rb', line 133

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_configObject


83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vop/objects/plugin.rb', line 83

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.message}), ignoring:\n#{raw}"
    end
  end
end

#load_default_configObject


75
76
77
78
79
80
81
# File 'lib/vop/objects/plugin.rb', line 75

def load_default_config
  params.each do |param|
    if param.options.has_key?(:default)
      @config[param.name] = param.options[:default]
    end
  end
end

#load_entitiesObject


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

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_filtersObject


141
142
143
144
145
146
147
# File 'lib/vop/objects/plugin.rb', line 141

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_helpersObject


149
150
151
152
# File 'lib/vop/objects/plugin.rb', line 149

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


71
72
73
# File 'lib/vop/objects/plugin.rb', line 71

def plugin_dir(name)
  File.join(@path, name.to_s)
end

#template(name) ⇒ Object


202
203
204
# File 'lib/vop/objects/plugin.rb', line 202

def template(name)
  @op.read_template(template_path(name))
end

#template_path(name) ⇒ Object


197
198
199
200
# File 'lib/vop/objects/plugin.rb', line 197

def template_path(name)
  name += ".erb" unless name.end_with? ".erb"
  File.join(plugin_dir(:templates), name)
end

#to_sObject


46
47
48
# File 'lib/vop/objects/plugin.rb', line 46

def to_s
  "Vop::Plugin #{name}"
end

#write_configObject


98
99
100
101
102
103
104
105
106
# File 'lib/vop/objects/plugin.rb', line 98

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