Class: CanTango::Configuration::Engines

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Singleton
Defined in:
lib/cantango/configuration/engines.rb,
lib/cantango/configuration/engines/cache.rb,
lib/cantango/configuration/engines/store.rb,
lib/cantango/configuration/engines/engine.rb,
lib/cantango/configuration/engines/permit.rb,
lib/cantango/configuration/engines/user_ac.rb,
lib/cantango/configuration/engines/permission.rb

Defined Under Namespace

Classes: Cache, Engine, Permission, Permit, Store, UserAc

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_availableObject



62
63
64
# File 'lib/cantango/configuration/engines.rb', line 62

def self.default_available
  [:cache, :permission , :permit, :user_ac]
end

Instance Method Details

#activeObject



100
101
102
# File 'lib/cantango/configuration/engines.rb', line 100

def active
  available.select {|engine| send(engine).on? if respond_to?(engine) }
end

#active?(name) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/cantango/configuration/engines.rb', line 96

def active? name
  active.include? name.to_sym
end

#all(state) ⇒ Object



78
79
80
# File 'lib/cantango/configuration/engines.rb', line 78

def all state
  available.each {|engine| send(engine).set state }
end

#any?(state) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/cantango/configuration/engines.rb', line 82

def any? state
  available.any? {|engine| send(engine).send(:"#{state}?") if respond_to?(engine) }
end

#availableObject



70
71
72
# File 'lib/cantango/configuration/engines.rb', line 70

def available
  (registered.keys + default_available).uniq
end

#available?(name) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/cantango/configuration/engines.rb', line 74

def available? name
  available.include? name.to_sym
end

#clear!Object



86
87
88
89
90
# File 'lib/cantango/configuration/engines.rb', line 86

def clear!
  each {|engine| engine.reset! }
  @registered = nil
  @execution_order = nil
end

#default_availableObject



66
67
68
# File 'lib/cantango/configuration/engines.rb', line 66

def default_available
  self.class.default_available
end

#default_registerObject



31
32
33
# File 'lib/cantango/configuration/engines.rb', line 31

def default_register
  {:permit => CanTango::PermitEngine, :permission => CanTango::PermissionEngine, :user_ac => CanTango::UserAcEngine }
end

#eachObject



92
93
94
# File 'lib/cantango/configuration/engines.rb', line 92

def each
  available.each {|engine| yield send(engine) if respond_to?(engine) }
end

#execute_after(existing, name) ⇒ Object



53
54
55
56
# File 'lib/cantango/configuration/engines.rb', line 53

def execute_after existing, name
  index = execution_order.index(existing)
  index ? execution_order.insert(index +1, name) : execute_last(name)
end

#execute_before(existing, name) ⇒ Object



48
49
50
51
# File 'lib/cantango/configuration/engines.rb', line 48

def execute_before existing, name
  index = execution_order.index(existing) || 0
  execution_order.insert(index, name)
end

#execute_first(name) ⇒ Object



40
41
42
# File 'lib/cantango/configuration/engines.rb', line 40

def execute_first name
  execution_order.insert(0, name)
end

#execute_last(name) ⇒ Object



44
45
46
# File 'lib/cantango/configuration/engines.rb', line 44

def execute_last name
  execution_order.insert(-1, name)
end

#execution_orderObject



58
59
60
# File 'lib/cantango/configuration/engines.rb', line 58

def execution_order
  @execution_order ||= (self.class.default_available - [:cache])
end

#execution_order=(*names) ⇒ Object

defines the order of execution of engine in ability



36
37
38
# File 'lib/cantango/configuration/engines.rb', line 36

def execution_order= *names
  @execution_order = names.to_symbols.select {|name| available? name }
end

#register(hash) ⇒ Object

engine registry is a simple hash



12
13
14
15
16
17
18
# File 'lib/cantango/configuration/engines.rb', line 12

def register hash
  hash.each_pair do |name, engine_class|
    raise "Class must implement the CanTango Engine API. You can start by sublclassing CanTango::Engine" if !engine? engine_class
    raise "Name of engine must be a String or Symbol" if !name.kind_of_label?
    registered[name.to_sym] = engine_class
  end
end

#registeredObject

engine factories ? :cache => Cantango::Ability::Cache



22
23
24
# File 'lib/cantango/configuration/engines.rb', line 22

def registered
  @registered ||= default_register
end

#unregister(name) ⇒ Object



26
27
28
29
# File 'lib/cantango/configuration/engines.rb', line 26

def unregister name
  @registered = {} if name == :all
  @registered.delete(name)
end