Class: SurveillanceAuthority::Sanction

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/surveillance_authority.rb

Constant Summary collapse

VALID_HOOKS =
[:validation, :validation_on_create, :save, :create]
@@plugins =
[]

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/surveillance_authority.rb', line 52

def method_missing( method_name, *args )
  @plugins_methods ||= {}

  if @plugins_methods[method_name.to_sym] || plugins_methods[method_name.to_sym]
    plugins_methods[method_name.to_sym].call( *args )
  else
    raise "no method called #{method_name}"
  end
end

Class Method Details

.default_config(options = {}) ⇒ Object



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

def self.default_config(options = {})
  self.instance.config = options
  self.instance.validate_configuration
end

.inherited(c) ⇒ Object

register plugins (== SurveillanceAuthority::Sanction Sub-Classes) automatically when they get defined



32
33
34
35
36
37
38
# File 'lib/surveillance_authority.rb', line 32

def self.inherited(c)
  c.class_eval do
    include Singleton
  end

  @@plugins << c
end

Instance Method Details

#configObject



20
21
22
# File 'lib/surveillance_authority.rb', line 20

def config
  @config ||= {}
end

#config=(options = {}) ⇒ Object



14
15
16
17
18
# File 'lib/surveillance_authority.rb', line 14

def config=(options = {})
  @config ||= {}
  @config = @config.merge(options)
  validate_configuration
end

#plugins_methodsObject

build a hash containing all available public plugin methods



41
42
43
44
45
46
47
48
49
50
# File 'lib/surveillance_authority.rb', line 41

def plugins_methods
  # caching with @plugins_methods ||= ... does not work here
  @plugins_methods = @@plugins.inject({}) do |hash, plugin_class|
    plugin_class.instance_methods(false).each do |method_name|
      raise "plugin method name clash ... \"#{method_name}\" is defined in \"#{plugin_class.name}\" and \"#{hash[method_name.to_sym].owner.name}\"!" if hash[method_name.to_sym]
      hash[method_name.to_sym] = plugin_class.instance.method( method_name )
    end
    hash
  end
end

#validate_configurationObject



24
25
26
# File 'lib/surveillance_authority.rb', line 24

def validate_configuration
  @config
end