Class: Inkpen::Extensions::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/inkpen/extensions/base.rb

Overview

This class is abstract.

Subclass and override #name and #to_config to implement a custom extension.

Base class for all Inkpen TipTap extensions.

Extensions follow the TipTap extension pattern and are configured through this Ruby PORO layer before being passed to the JavaScript TipTap editor.

Examples:

Creating a custom extension

class MyExtension < Inkpen::Extensions::Base
  def name
    :my_extension
  end

  def to_config
    { custom_option: true }
  end
end

Author:

  • Inkpen Team

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Base

Initialize a new extension with optional configuration.

Parameters:

  • options (Hash)

    configuration options for the extension

Options Hash (**options):

  • :enabled (Boolean)

    whether the extension is enabled (default: true)

Since:

  • 0.2.0



40
41
42
# File 'lib/inkpen/extensions/base.rb', line 40

def initialize(**options)
  @options = default_options.merge(options)
end

Instance Attribute Details

#optionsHash (readonly)

Returns the options passed during initialization.

Returns:

  • (Hash)

    the options passed during initialization

Since:

  • 0.2.0



32
33
34
# File 'lib/inkpen/extensions/base.rb', line 32

def options
  @options
end

Instance Method Details

#enabled?Boolean

Whether this extension is currently enabled.

Returns:

  • (Boolean)

    true if enabled

Since:

  • 0.2.0



60
61
62
# File 'lib/inkpen/extensions/base.rb', line 60

def enabled?
  options.fetch(:enabled, true)
end

#nameSymbol

This method is abstract.

Subclasses must override this method.

The unique name of the extension.

Returns:

  • (Symbol)

    the extension name

Raises:

  • (NotImplementedError)

    if not overridden in subclass

Since:

  • 0.2.0



51
52
53
# File 'lib/inkpen/extensions/base.rb', line 51

def name
  raise NotImplementedError, "#{self.class} must implement #name"
end

#to_configHash

Convert the extension configuration to a hash for JSON serialization.

This hash is passed to the JavaScript TipTap extension.

Returns:

  • (Hash)

    configuration hash

Since:

  • 0.2.0



71
72
73
# File 'lib/inkpen/extensions/base.rb', line 71

def to_config
  {}
end

#to_hHash

Full configuration including name and enabled status.

Returns:

  • (Hash)

    complete extension configuration

Since:

  • 0.2.0



80
81
82
83
84
85
86
# File 'lib/inkpen/extensions/base.rb', line 80

def to_h
  {
    name: name,
    enabled: enabled?,
    config: to_config
  }
end

#to_json(*args) ⇒ String

JSON representation of the extension.

Returns:

  • (String)

    JSON string

Since:

  • 0.2.0



93
94
95
# File 'lib/inkpen/extensions/base.rb', line 93

def to_json(*args)
  to_h.to_json(*args)
end