roleplay 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -1
- data/README.md +3 -1
- data/lib/roleplay.rb +18 -1
- data/lib/roleplay/version.rb +1 -1
- metadata +1 -1
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -27,7 +27,9 @@ Set up the model:
|
|
27
27
|
has_role :seller, using: [Seller, Payable]
|
28
28
|
end
|
29
29
|
|
30
|
-
Now, instead of User.new, you can use User.new_with_role(:buyer) to create an instance that includes only the modules specified in the role definition
|
30
|
+
Now, instead of User.new, you can use User.new_with_role(:buyer) to create an instance that includes only the modules specified in the role definition.
|
31
|
+
If you already have an instance, user.playing(:buyer) or user.as_a(:buyer) will both return duped instances with the requested modules included. Adding
|
32
|
+
a bang will modify the original instance.
|
31
33
|
|
32
34
|
## Contributing
|
33
35
|
|
data/lib/roleplay.rb
CHANGED
@@ -8,7 +8,10 @@ module Roleplay
|
|
8
8
|
|
9
9
|
def playing(name)
|
10
10
|
modules = self.class.roles[name]
|
11
|
+
raise "role #{name} does not define any modules" unless modules.present?
|
11
12
|
value = self.dup
|
13
|
+
value.singleton_class.send(:attr_accessor, :role)
|
14
|
+
value.role = {name => modules}
|
12
15
|
modules.each do |m|
|
13
16
|
value.extend m
|
14
17
|
end
|
@@ -16,19 +19,33 @@ module Roleplay
|
|
16
19
|
end
|
17
20
|
alias :as_a :playing
|
18
21
|
|
22
|
+
def playing!(name)
|
23
|
+
modules = self.class.roles[name]
|
24
|
+
raise "role #{name} does not define any modules" unless modules.present?
|
25
|
+
singleton_class.send(:attr_accessor, :role)
|
26
|
+
self.role = {name => modules}
|
27
|
+
modules.each do |m|
|
28
|
+
extend m
|
29
|
+
end
|
30
|
+
self
|
31
|
+
end
|
32
|
+
alias :as_a! :playing!
|
33
|
+
|
19
34
|
module ClassMethods
|
20
35
|
|
21
36
|
attr_accessor :roles
|
22
37
|
|
23
38
|
def plays_role(name, opts={})
|
24
39
|
@roles ||= {}
|
40
|
+
raise "no modules specified for role #{name}" unless opts[:using].present?
|
25
41
|
roles[name] = [opts[:using]].flatten
|
26
42
|
end
|
27
43
|
|
28
44
|
def new_with_role(name)
|
29
45
|
value = new
|
30
|
-
value.singleton_class.send(:attr_accessor, :role)
|
31
46
|
modules = roles[name]
|
47
|
+
raise "role #{name} does not define any modules" unless modules.present?
|
48
|
+
value.singleton_class.send(:attr_accessor, :role)
|
32
49
|
value.role = {name => modules}
|
33
50
|
modules.each do |m|
|
34
51
|
value.extend m
|
data/lib/roleplay/version.rb
CHANGED