Class: Avo::Plugin

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, priority:, registered_from: nil, &block) ⇒ Plugin

Returns a new instance of Plugin.



9
10
11
12
13
# File 'lib/avo/plugin.rb', line 9

def initialize(*, name:, priority:, registered_from: nil, **, &block)
  @name = name
  @priority = priority
  @registered_from = registered_from
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/avo/plugin.rb', line 3

def name
  @name
end

#priorityObject (readonly)

Returns the value of attribute priority.



4
5
6
# File 'lib/avo/plugin.rb', line 4

def priority
  @priority
end

#registered_fromObject (readonly)

Returns the value of attribute registered_from.



5
6
7
# File 'lib/avo/plugin.rb', line 5

def registered_from
  @registered_from
end

Class Method Details

.engineObject



51
52
53
# File 'lib/avo/plugin.rb', line 51

def engine
  "#{namespace}::Engine".safe_constantize
end

.gemspecObject



61
62
63
64
65
66
67
# File 'lib/avo/plugin.rb', line 61

def gemspec
  return if engine.blank?

  gemspec_path = Dir["#{engine.root}/*.gemspec"].first

  Gem::Specification.load(gemspec_path)
end

.nameObject



41
42
43
44
45
# File 'lib/avo/plugin.rb', line 41

def name
  return gemspec.name if gemspec.present?

  to_s.split("::").first
end

.namespaceObject



55
56
57
58
59
# File 'lib/avo/plugin.rb', line 55

def namespace
  modules = to_s.split("::")
  modules.pop
  modules.join("::")
end

.versionObject



47
48
49
# File 'lib/avo/plugin.rb', line 47

def version
  "#{namespace}::VERSION".safe_constantize
end

Instance Method Details

#gem_nameObject

The real gem name this plugin ships in (e.g. ‘avo-rhino_field`), as opposed to the nickname it registered under (e.g. `:rhino`). Resolved by matching the file that called `register` against the gems in the bundle, so it works no matter how the plugin registered and even when the gem isn’t ‘avo-*`. Returns nil when the owning gem can’t be found in the bundle.



24
25
26
27
28
# File 'lib/avo/plugin.rb', line 24

def gem_name
  return @gem_name if defined?(@gem_name)

  @gem_name = registered_gem&.name
end

#to_sObject



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

def to_s
  "#{name}-#{version}"
end