Class: DarDaDa::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/dar_da_da/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Config

Returns a new instance of Config.



3
4
5
6
7
# File 'lib/dar_da_da/config.rb', line 3

def initialize(options={}, &block)
  @options = DarDaDa.config.merge(options)
  @all_rights, @first_added_role_name = [], nil
  reopen(&block)
end

Instance Attribute Details

#all_rightsObject (readonly)

Returns the value of attribute all_rights.



14
15
16
# File 'lib/dar_da_da/config.rb', line 14

def all_rights
  @all_rights
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/dar_da_da/config.rb', line 14

def options
  @options
end

Instance Method Details

#[](role_name) ⇒ Object



24
25
26
27
# File 'lib/dar_da_da/config.rb', line 24

def [](role_name)
  role_name = role_name.to_sym unless role_name.blank?
  super(role_name)
end

#finalizeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dar_da_da/config.rb', line 29

def finalize
  # Merge rights between roles
  each do |name, role|
    role.merge_rights do |role_name|
      self[role_name]
    end
  end
  
  # Check if default_role exists
  # and delete it if it does not
  options.delete(:default_role) unless self[options[:default_role]]
  
  # Set default role to first added 
  # if not specified
  options[:default_role] = @first_added_role_name unless options[:default_role]
  
  # Finalize roles
  each{|name, role| role.finalize}
  
  # Collect and unify all_rights
  @all_rights = map do |role|
    role[1].to_a
  end
  @all_rights.flatten!
  @all_rights.uniq!
end

#reopen(&block) ⇒ Object



9
10
11
12
# File 'lib/dar_da_da/config.rb', line 9

def reopen(&block)
  instance_eval(&block) if block_given?
  finalize
end

#role(name, options = {}, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/dar_da_da/config.rb', line 16

def role(name, options={}, &block)
  name = name.to_sym
  @options[:default_role] = name if options[:default]
  @first_added_role_name = name if empty?
  self[name] ||= DarDaDa::Role.new(name)
  self[name].instance_eval(&block) if block_given?
end