Class: LcpRuby::Actions::ActionRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/actions/action_registry.rb

Class Method Summary collapse

Class Method Details

.action_for(key) ⇒ Object



5
6
7
# File 'lib/lcp_ruby/actions/action_registry.rb', line 5

def action_for(key)
  registry[key.to_s]
end

.clear!Object



36
37
38
# File 'lib/lcp_ruby/actions/action_registry.rb', line 36

def clear!
  @registry = {}
end

.discover!(base_path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lcp_ruby/actions/action_registry.rb', line 17

def discover!(base_path)
  actions_path = File.join(base_path, "actions")
  return unless File.directory?(actions_path)

  Dir[File.join(actions_path, "**", "*.rb")].sort.each do |file|
    require file

    relative = file.sub("#{actions_path}/", "").sub(/\.rb$/, "")
    class_name = "LcpRuby::HostActions::#{relative.split('/').map(&:camelize).join('::')}"

    begin
      action_class = class_name.constantize
      register(relative, action_class)
    rescue NameError => e
      Rails.logger.warn("[LcpRuby] Could not register action #{class_name}: #{e.message}")
    end
  end
end

.register(key, action_class) ⇒ Object



9
10
11
# File 'lib/lcp_ruby/actions/action_registry.rb', line 9

def register(key, action_class)
  registry[key.to_s] = action_class
end

.registered?(key) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/lcp_ruby/actions/action_registry.rb', line 13

def registered?(key)
  registry.key?(key.to_s)
end