Sha256: 9ea21d9d02001f386a8d452272c3d58c9aa266f9fbd6ccde021d3b90b7ce5082

Contents?: true

Size: 982 Bytes

Versions: 4

Compression:

Stored size: 982 Bytes

Contents

class Animal
  credentials do |animal|
    animal.can :clean, :self
  end
  
  attr_accessor :species, :hungry, :fast
  
  def initialize(species = nil, hungry = false, fast = false)
    @species = species
    @hungry = hungry
    @fast = fast
  end
  
  def edible?
    false
  end
  
  def hungry?
    !!hungry
  end
  
  def fast?
    !!fast
  end
  
  alias :to_s :species
end

class Prey < Animal
  def edible?; true; end
end

class Bird < Prey
  credentials do |bird|
    bird.can :clean, Animal # they are good like that
  end
end

class Carnivore < Animal
  credentials do |carnivore|
    carnivore.can :eat, Animal, :if => lambda { |predator, prey| prey.edible? }
    carnivore.cannot :eat, Animal, :unless => :hungry?
    carnivore.cannot :eat, Bird, :unless => [ :hungry?, :fast? ]
  end
end

class Man < Animal
  credentials :default => :allow do |man|
  end
end

class WhiteMan < Man
  credentials :default => :allow do |white_man|
    white_man.cannot :jump
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
credentials-2.2.3 spec/domain.rb
credentials-2.2.2 spec/domain.rb
credentials-2.2.1 spec/domain.rb
credentials-2.2.0 spec/domain.rb