Class: BBortcodes::Registry

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/bbortcodes/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



9
10
11
12
# File 'lib/bbortcodes/registry.rb', line 9

def initialize
  super
  @shortcodes = {}
end

Instance Method Details

#allHash

Get all registered shortcode classes

Returns:

  • (Hash)

    Hash of tag_name => shortcode_class



58
59
60
61
62
# File 'lib/bbortcodes/registry.rb', line 58

def all
  synchronize do
    @shortcodes.dup
  end
end

#clearObject

Clear all registered shortcodes



81
82
83
84
85
# File 'lib/bbortcodes/registry.rb', line 81

def clear
  synchronize do
    @shortcodes.clear
  end
end

#countInteger

Get the count of registered shortcodes

Returns:

  • (Integer)


89
90
91
92
93
# File 'lib/bbortcodes/registry.rb', line 89

def count
  synchronize do
    @shortcodes.size
  end
end

#find(tag_name) ⇒ Class?

Find a shortcode class by tag name

Parameters:

  • tag_name (String)

    The tag name to look up

Returns:

  • (Class, nil)

    The shortcode class or nil if not found



41
42
43
44
45
# File 'lib/bbortcodes/registry.rb', line 41

def find(tag_name)
  synchronize do
    @shortcodes[tag_name.to_s]
  end
end

#register(shortcode_class) ⇒ Object

Register a shortcode class

Parameters:

  • shortcode_class (Class)

    A class that inherits from Shortcode



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bbortcodes/registry.rb', line 16

def register(shortcode_class)
  synchronize do
    unless shortcode_class < Shortcode
      raise RegistryError, "#{shortcode_class} must inherit from BBortcodes::Shortcode"
    end

    tag_name = shortcode_class.tag_name

    if @shortcodes.key?(tag_name)
      if BBortcodes.config.allow_shortcode_overwrite
        @shortcodes.delete(tag_name)
      else
        raise RegistryError,
          "Shortcode '#{tag_name}' is already registered by #{@shortcodes[tag_name].name}. " \
          "Set allow_shortcode_overwrite: true in config to override, or call unregister('#{tag_name}') first."
      end
    end

    @shortcodes[tag_name] = shortcode_class
  end
end

#registered?(tag_name) ⇒ Boolean

Check if a shortcode is registered

Parameters:

  • tag_name (String)

    The tag name to check

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/bbortcodes/registry.rb', line 50

def registered?(tag_name)
  synchronize do
    @shortcodes.key?(tag_name.to_s)
  end
end

#tag_namesArray<String>

Get all registered tag names

Returns:

  • (Array<String>)


66
67
68
69
70
# File 'lib/bbortcodes/registry.rb', line 66

def tag_names
  synchronize do
    @shortcodes.keys
  end
end

#unregister(tag_name) ⇒ Object

Unregister a shortcode

Parameters:

  • tag_name (String)

    The tag name to unregister



74
75
76
77
78
# File 'lib/bbortcodes/registry.rb', line 74

def unregister(tag_name)
  synchronize do
    @shortcodes.delete(tag_name.to_s)
  end
end