Grognard

A lightweight cancan-like authorization library.

Usage


# person.rb

class Customer
  def self.allowed_to(customer, food)
      abilities = []
      abilities << :drink_scotch if customer.has_paid_for_scotch and food.type == "scotch"
      abilities << :grumble unless food.is_ok

      ability
  end
end

# attendant.rb

class Attendant
  def self.allowed_to(attendant, food)
    abilities = []
    abilities << :free_deliver if food.kind_of? Food

    abilities
  end
end


# meanwhile in the console

guard = Grognard::Guard.new

guard << Customer # learn the abilities of a customer
guard << Attendant # learn the abilities of an attendant

guard.be_a_customer # to be a customer
guard.allowed_to_drink_scotch? @drunker, @scotch # => true
guard.allowed_to_grumble? :everyone, @shit # => true
guard.allowed_to_grumble? @drunker, @peri_peri # => false

guard.be_a_attendant # to be a attendant
guard.allowed_to_free_deliver @attendant, @mussels # => true
guard.allowed_to_free_deliver @attendant, @perfume # => false

# role checking
guard.can_be_a_customer? # => true
guard.can_be_a_soldier? # => false

guard.remove_role? :customer # is not a customer anymore
guard.can_be_a_customer? # => false