Class: ROM::Plugin

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/plugin.rb

Overview

Plugin is a simple object used to store plugin configurations

Constant Summary collapse

INTERNAL_OPTS =

These opts are excluded when passing to mod's apply

%i[enabled applied target].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterSymbol (readonly)

Returns plugin adapter.

Returns:

  • (Symbol)

    plugin adapter



32
# File 'lib/rom/plugin.rb', line 32

option :adapter, optional: true

#configSymbol (readonly)

Returns Plugin optional config.

Returns:

  • (Symbol)

    Plugin optional config



36
# File 'lib/rom/plugin.rb', line 36

option :config, default: -> { ROM::OpenStruct.new }

#dslModule? (readonly)

Returns Optional DSL extensions.

Returns:

  • (Module, nil)

    Optional DSL extensions



40
# File 'lib/rom/plugin.rb', line 40

option :dsl, default: -> { mod.const_defined?(:DSL) ? mod.const_get(:DSL) : nil }

#modModule (readonly)

Returns a module representing the plugin.

Returns:

  • (Module)

    a module representing the plugin



27
# File 'lib/rom/plugin.rb', line 27

option :mod

#nameSymbol (readonly)

Returns plugin name.

Returns:

  • (Symbol)

    plugin name



22
# File 'lib/rom/plugin.rb', line 22

option :name

#typeSymbol (readonly)

Returns plugin type.

Returns:

  • (Symbol)

    plugin type



17
# File 'lib/rom/plugin.rb', line 17

option :type

Instance Method Details

#applied?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


96
97
98
# File 'lib/rom/plugin.rb', line 96

def applied?
  config.key?(:applied) && config.applied == true
end

#applyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rom/plugin.rb', line 78

def apply
  if enabled?
    apply_to(config.target, **plugin_options)
    config.applied = true
    config.freeze
    freeze
    self
  else
    raise "Cannot apply a plugin because it was not enabled for any target"
  end
end

#configure(**options) {|plugin.config| ... } ⇒ Object

Configure plugin

Yields:



57
58
59
60
61
62
# File 'lib/rom/plugin.rb', line 57

def configure(**options)
  plugin = dup
  plugin.config.update(options)
  yield(plugin.config) if block_given?
  plugin
end

#dupObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
# File 'lib/rom/plugin.rb', line 65

def dup
  with(config: ROM::OpenStruct.new(config.to_h.clone))
end

#enable(target) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
74
75
# File 'lib/rom/plugin.rb', line 70

def enable(target)
  target.extend(dsl) if dsl
  config.enabled = true
  config.target = target
  self
end

#enabled?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


91
92
93
# File 'lib/rom/plugin.rb', line 91

def enabled?
  config.key?(:enabled) && config.enabled == true
end

#keyString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Plugin registry key

Returns:

  • (String)


50
51
52
# File 'lib/rom/plugin.rb', line 50

def key
  [adapter, type, name].compact.join(".")
end

#plugin_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



101
102
103
# File 'lib/rom/plugin.rb', line 101

def plugin_options
  (opts = config.to_h).slice(*(opts.keys - INTERNAL_OPTS))
end