Class: Fusuma::Plugin::Base

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

Overview

Create a Plugin Class with extending this class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(subclass) ⇒ Object

when inherited from subclass



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fusuma/plugin/base.rb', line 12

def self.inherited(subclass)
  super

  locations = Kernel.caller_locations(1..1)
  if locations.nil? || locations.empty?
    raise "Plugin class #{subclass.name} must be defined in a file."
  end

  subclass_path = locations.first.path
  Manager.add(plugin_class: subclass, plugin_path: subclass_path)
end

.pluginsArray<Class>

get subclasses

Returns:

  • (Array<Class>)


26
27
28
# File 'lib/fusuma/plugin/base.rb', line 26

def self.plugins
  Manager.plugins[name]
end

Instance Method Details

#config_indexObject

: () -> Fusuma::Config::Index



72
73
74
# File 'lib/fusuma/plugin/base.rb', line 72

def config_index
  @config_index ||= Config::Index.new(self.class.name.gsub("Fusuma::", "").underscore.split("/"))
end

#config_param_typesHash

config parameter name and Type of the value of parameter : () -> Hash[Symbol, Array | Class]

Returns:

Raises:

  • (NotImplementedError)


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

def config_param_types
  raise NotImplementedError, "override #{self.class.name}##{__method__}"
end

#config_params(key = nil) ⇒ Object

: (?Symbol?) -> (String | Hash[untyped, untyped] | Float | bool)?

Parameters:

Returns:

  • (Object)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fusuma/plugin/base.rb', line 46

def config_params(key = nil)
  @config_params ||= {}
  if @config_params["#{config_index.cache_key},#{key}"]
    return @config_params["#{config_index.cache_key},#{key}"]
  end

  params = Config.instance.fetch_config_params(key, config_index)

  return params unless key

  @config_params["#{config_index.cache_key},#{key}"] =
    params.fetch(key, nil).tap do |val|
      next if val.nil?

      # NOTE: Type checking for config.yml
      param_types = Array(config_param_types.fetch(key))

      next if param_types.any? { |klass| val.is_a?(klass) }

      MultiLogger.error("Please fix config.yml")
      MultiLogger.error("`#{config_index.keys.join(".")}.#{key}` should be #{param_types.join(" OR ")}.")
      exit 1
    end
end

#shutdownObject

This method is abstract.

override ‘#shutdown` to implement

: () -> nil



32
33
# File 'lib/fusuma/plugin/base.rb', line 32

def shutdown
end