Class: Entitlements::Util::Override

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements/util/override.rb

Constant Summary collapse

C =
::Contracts

Class Method Summary collapse

Methods included from Contracts::Core

common, extended, included

Class Method Details

.override_hash_from_plugin(plugin, group, ldap) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/entitlements/util/override.rb', line 18

def self.override_hash_from_plugin(plugin, group, ldap)
  return unless plugin

  # Plugin hash should consist of a Hash with 2 keys: "file" the absolute path to the file or relative path compared to
  # the entitlements configuration file, and "class" the class name that's contained within the file. If the filename
  # has no "/" then it is treated as a built-in plugin, under the "plugins" directory within this gem.
  unless plugin.key?("file")
    raise ArgumentError, "plugin configuration hash must contain 'file' key"
  end

  file = if plugin["file"] !~ %r{/}
    File.expand_path(File.join("../plugins", plugin["file"]), File.dirname(__FILE__))
  elsif plugin["file"].start_with?("/")
    plugin["file"]
  else
    File.expand_path(plugin["file"], File.dirname(Entitlements.config_file))
  end

  unless File.file?(file)
    raise ArgumentError, "Could not locate plugin for #{plugin['file'].inspect} at #{file.inspect}"
  end

  unless plugin.key?("class")
    raise ArgumentError, "plugin configuration hash must contain 'class' key"
  end

  require file

  clazz = Kernel.const_get("Entitlements::Plugins::#{plugin['class']}")

  unless clazz.respond_to?(:loaded?) && clazz.loaded?
    raise ArgumentError, "Plugin Entitlements::Plugins::#{plugin['class']} should inherit Entitlements::Plugins"
  end

  unless clazz.respond_to?(:override_hash)
    raise ArgumentError, "Plugin Entitlements::Plugins::#{plugin['class']} must implement override_hash method"
  end

  override_hash = clazz.override_hash(group, plugin, ldap)
  return override_hash if override_hash.is_a?(Hash)

  type = override_hash.class
  raise ArgumentError, "Plugin Entitlements::Plugins::#{plugin['class']}.override_hash must return hash, not #{type}"
end