Class: Eaco::DSL::Actor
- Defined in:
- lib/eaco/dsl/actor.rb,
lib/eaco/dsl/actor/designators.rb
Overview
Parses the Actor DSL, that describes how to harvest Eaco::Designators from an Actor and how to identify it as an admin, or superuser.
actor User do
admin do |user|
user.admin?
end
designators do
authenticated from: :class
user from: :id
group from: :group_ids
end
end
Defined Under Namespace
Classes: Designators
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.all_designators ⇒ Hash
private
A registry of all the defined designators.
-
.find_designator(name) ⇒ Class
Looks up the given designator implementation by its
name. -
.register_designators(new_designators) ⇒ Hash
Saves the given designators in the global designators registry.
Instance Method Summary collapse
-
#admin(&block)
Defines the boolean logic that determines whether an Actor is an admin.
-
#designators(&block) ⇒ Object
Defines the designators that apply to this Actor.
-
#initialize ⇒ Actor
constructor
Makes an application model a valid Actor.
Methods inherited from Base
Constructor Details
Class Method Details
.all_designators ⇒ Hash (private)
135 136 137 |
# File 'lib/eaco/dsl/actor.rb', line 135 def all_designators @_all_designators ||= {} end |
.find_designator(name) ⇒ Class
Looks up the given designator implementation by its name.
113 114 115 116 117 118 |
# File 'lib/eaco/dsl/actor.rb', line 113 def find_designator(name) all_designators.fetch(name.intern) rescue KeyError raise Malformed, "Designator not found: #{name.inspect}" end |
.register_designators(new_designators) ⇒ Hash
Saves the given designators in the global designators registry.
127 128 129 |
# File 'lib/eaco/dsl/actor.rb', line 127 def register_designators(new_designators) all_designators.update(new_designators) end |
Instance Method Details
#admin(&block)
This method returns an undefined value.
Defines the boolean logic that determines whether an Eaco::DSL::Actor is an admin. Usually you’ll have an admin? method on your model, that you can call from here. Or, feel free to just return false to disable this functionality.
Example:
actor User do
admin do |user|
user.admin?
end
end
97 98 99 100 101 |
# File 'lib/eaco/dsl/actor.rb', line 97 def admin(&block) target_eval do @_admin_logic = block end end |
#designators(&block) ⇒ Object
Defines the designators that apply to this Eaco::DSL::Actor.
Example:
actor User do
designators do
authenticated from: :class
user from: :id
group from: :group_ids
end
end
Eaco::Designator names are collected using method_missing, and are named after the method name. Implementations are looked up in a Designators module in the Eaco::DSL::Actor‘s class.
Each designator implementation is expected to be named after the designator’s name, camelized, and inherit from Eaco::Designator.
TODO all designators share the same namespace. This is due to the fact that designator string representations aren’t scoped by the Actor model they belong to. As such when instantiating a designator from Eaco::Designator.make the registry is consulted to find the designator implementation.
72 73 74 75 76 77 78 |
# File 'lib/eaco/dsl/actor.rb', line 72 def designators(&block) new_designators = target_eval do @_designators = Designators.eval(self, &block).result.freeze end Actor.register_designators(new_designators) end |