class Eaco::DSL::Resource

Parses the Resource definition DSL.

Example:

authorize Document do
  roles :owner, :editor, :reader

  role :owner, 'Author'

  permissions do
    reader   :read
    editor   reader, :edit
    owner    editor, :destroy
  end
end

The DSL installs authorization in your Document model, defining three access roles.

The owner role is given a label of “Author”.

Each role has then different abilities, defined in the permissions block.

@see DSL::Resource::Permissions

Public Class Methods

new(*) click to toggle source

Sets up an authorized resource. The only required API is accessible_by. For available implementations, see the {Adapters} module.

@see Resource

Calls superclass method Eaco::DSL::Base::new
# File lib/eaco/dsl/resource.rb, line 41
def initialize(*)
  super

  target_eval do
    include Eaco::Resource

    def permissions
      @_permissions
    end

    def roles
      @_roles || []
    end

    def roles_priority
      @_roles_priority ||= {}.tap do |priorities|
        roles.each_with_index {|role, idx| priorities[role] = idx }
      end.freeze
    end

    def roles_with_labels
      @_roles_with_labels ||= roles.inject({}) do |labels, role|
        labels.update(role => role.to_s.humanize)
      end
    end

    # Reset memoizations when this method is called on the target class,
    # so that reloading the authorizations configuration file will
    # refresh the models' configuration.
    @_roles_priority = nil
    @_roles_with_labels = nil
  end
end

Public Instance Methods

permissions() click to toggle source
# File lib/eaco/dsl/resource.rb, line 47
def permissions
  @_permissions
end
role(role, label) click to toggle source

Sets the given label on the given role.

TODO rename this method, or use it to pass options to improve readability of the DSL and to store more metadata with each role for future extensibility.

@param role [Symbol] @param label [String]

@return [void]

# File lib/eaco/dsl/resource.rb, line 121
def role(role, label)
  target_eval do
    roles_with_labels[role] = label
  end
end
roles() click to toggle source
# File lib/eaco/dsl/resource.rb, line 51
def roles
  @_roles || []
end
roles_priority() click to toggle source
# File lib/eaco/dsl/resource.rb, line 55
def roles_priority
  @_roles_priority ||= {}.tap do |priorities|
    roles.each_with_index {|role, idx| priorities[role] = idx }
  end.freeze
end
roles_with_labels() click to toggle source
# File lib/eaco/dsl/resource.rb, line 61
def roles_with_labels
  @_roles_with_labels ||= roles.inject({}) do |labels, role|
    labels.update(role => role.to_s.humanize)
  end
end