Class: Eaco::DSL::Actor

Inherits:
Base
  • Object
show all
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

#options, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

eval, #target_eval

Constructor Details

#initializeActor

Makes an application model a valid Actor.

See Also:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/eaco/dsl/actor.rb', line 28

def initialize(*)
  super

  target_eval do
    include Eaco::Actor

    def designators
      @_designators
    end

    def admin_logic
      @_admin_logic
    end
  end
end

Class Method Details

.all_designatorsHash (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.

Raises:



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.

See Also:



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