Class: Bali::Ruler

Inherits:
Object
  • Object
show all
Defined in:
lib/bali/ruler.rb

Overview

This class represents all roles, and its rules, for a resource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ Ruler

Returns a new instance of Ruler.



13
14
15
16
17
# File 'lib/bali/ruler.rb', line 13

def initialize(model_class)
  @model_class = model_class
  @roles = {}
  @roles[nil] = Bali::Role.new(nil)
end

Instance Attribute Details

#rolesObject

Returns the value of attribute roles.



4
5
6
# File 'lib/bali/ruler.rb', line 4

def roles
  @roles
end

Class Method Details

.for(record_class) ⇒ Object



8
9
10
11
# File 'lib/bali/ruler.rb', line 8

def self.for(record_class)
  rule_class = Bali::Rules.for(record_class)
  rule_class.ruler if rule_class
end

Instance Method Details

#<<(role) ⇒ Object



19
20
21
# File 'lib/bali/ruler.rb', line 19

def << role
  @roles[role.name] = role
end

#[](role) ⇒ Object



23
24
25
26
# File 'lib/bali/ruler.rb', line 23

def [] role
  symbolized_role = role.to_sym if role
  @roles[symbolized_role]
end

#find_or_create_role(role_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/bali/ruler.rb', line 28

def find_or_create_role role_name
  role = self[role_name]

  if role.nil?
    role = Bali::Role.new(role_name)
    self << role
  end

  role
end