Module: Rolistic::ClassMethods

Defined in:
lib/rolistic/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#abilities_for(name) ⇒ Object



31
32
33
# File 'lib/rolistic/class_methods.rb', line 31

def abilities_for(name)
  abilities_map.fetch(name) { [] }
end

#abilities_for_trait(trait) ⇒ Object



35
36
37
# File 'lib/rolistic/class_methods.rb', line 35

def abilities_for_trait(trait)
  traits_map.fetch(trait) { abilities_for(trait) }
end

#abilities_mapObject



21
22
23
24
# File 'lib/rolistic/class_methods.rb', line 21

def abilities_map
  return @abilities_map if defined?(@abilities_map)
  @abilities_map = {}
end

#availableObject



62
63
64
# File 'lib/rolistic/class_methods.rb', line 62

def available
  abilities_map.keys
end

#defaultObject



58
59
60
# File 'lib/rolistic/class_methods.rb', line 58

def default
  @default if defined?(@default)
end

#dump(role) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rolistic/class_methods.rb', line 13

def dump(role)
  case role
  when String, Symbol then role.to_s
  when self
    role.to_s unless role.default?
  end
end

#load(raw) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/rolistic/class_methods.rb', line 3

def load(raw)
  case raw
  when self then raw
  when String, Symbol
    new(raw) unless raw.blank?
  else
    new
  end
end

#role(name, *traits_and_abilities, **options) ⇒ Object Also known as: add_role



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rolistic/class_methods.rb', line 44

def role(name, *traits_and_abilities, **options)
  abilities = traits_and_abilities.reduce([]) do |m, t_or_a|
    case t_or_a
    when :everything then Everything
    when Symbol then m + abilities_for_trait(t_or_a)
    when Array then m + t_or_a
    else t_or_a
    end
  end
  abilities_map[name] = abilities
  @default = name if options[:default]
end

#trait(trait, abilities) ⇒ Object Also known as: add_trait



39
40
41
# File 'lib/rolistic/class_methods.rb', line 39

def trait(trait, abilities)
  traits_map[trait] = abilities
end

#traits_mapObject



26
27
28
29
# File 'lib/rolistic/class_methods.rb', line 26

def traits_map
  return @traits_map if defined?(@traits_map)
  @traits_map = {}
end