scheherazade 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/scheherazade/character_builder.rb +1 -1
- data/lib/scheherazade/story.rb +61 -46
- data/lib/scheherazade/version.rb +1 -1
- metadata +4 -4
data/lib/scheherazade/story.rb
CHANGED
@@ -2,47 +2,72 @@ module Scheherazade
|
|
2
2
|
class Story < Hash
|
3
3
|
attr_reader :fill_attributes, :characters, :counter, :current
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
module ClassMethods
|
6
|
+
def current
|
7
|
+
(Thread.current[:scheherazade_stories] ||= []).last || TOP
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
# Begins a story within the current story.
|
11
|
+
# Should be balanced with a call to +end+
|
12
|
+
#
|
13
|
+
def begin
|
14
|
+
(Thread.current[:scheherazade_stories] ||= []).push Story.new
|
15
|
+
current
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
18
|
+
# Ends the current substory and comes back
|
19
|
+
# to the previous current story
|
20
|
+
#
|
21
|
+
def end(opts = nil)
|
22
|
+
current.send :rollback if opts && opts[:rollback]
|
23
|
+
Thread.current[:scheherazade_stories].pop
|
24
|
+
current
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
# Begins a substory, yields, and ends the story
|
28
|
+
#
|
29
|
+
def tell(opts = nil)
|
30
|
+
yield self.begin
|
31
|
+
ensure
|
32
|
+
self.end(opts)
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_character(character_or_model)
|
36
|
+
case character_or_model
|
37
|
+
when Class
|
38
|
+
character_or_model.name.underscore.to_sym
|
39
|
+
when Symbol
|
40
|
+
character_or_model
|
41
|
+
else
|
42
|
+
raise ArgumentError, "expected character or Model, got #{character_or_model.ancestors}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_character!(character_or_model)
|
47
|
+
to_character(character_or_model) unless character_or_model.is_a?(Symbol)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns a Model or nil
|
51
|
+
def to_model(character)
|
52
|
+
character.to_s.camelize.safe_constantize
|
53
|
+
end
|
32
54
|
end
|
55
|
+
extend ClassMethods
|
56
|
+
delegate :begin, :end, :tell, :to_character, :to_character!, :to_model, :to => 'self.class'
|
57
|
+
|
33
58
|
|
34
59
|
def initialize(parent = self.class.current)
|
35
60
|
super(){|h, k| parent[k] if parent }
|
36
61
|
@parent = parent
|
37
|
-
@current = Hash.new
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
parent.
|
42
|
-
elsif k.is_a?(Symbol)
|
43
|
-
k.to_s.camelize.constantize
|
62
|
+
@current = Hash.new do |h, k|
|
63
|
+
if (char = to_character!(k))
|
64
|
+
h[char]
|
65
|
+
else
|
66
|
+
parent.current[k] if parent
|
44
67
|
end
|
45
68
|
end
|
69
|
+
@fill_attributes = Hash.new{|h, k| parent.fill_attributes[k] if parent }
|
70
|
+
@characters = Hash.new {|h, k| parent.characters[k] if parent }
|
46
71
|
@counter = parent ? parent.counter.dup : Hash.new(0)
|
47
72
|
@filling = []
|
48
73
|
@after_imagine = {}
|
@@ -84,7 +109,8 @@ module Scheherazade
|
|
84
109
|
# end
|
85
110
|
# story.current[User] # => nil
|
86
111
|
#
|
87
|
-
def imagine(
|
112
|
+
def imagine(character_or_model, attributes = nil)
|
113
|
+
character = to_character(character_or_model)
|
88
114
|
prev, @building = @building, [] # because method might be re-entrant
|
89
115
|
CharacterBuilder.new(character).build(attributes) do |ar|
|
90
116
|
ar.save!
|
@@ -105,18 +131,6 @@ module Scheherazade
|
|
105
131
|
current[character] || imagine(character)
|
106
132
|
end
|
107
133
|
|
108
|
-
def begin
|
109
|
-
self.class.begin
|
110
|
-
end
|
111
|
-
|
112
|
-
def end(opts = nil)
|
113
|
-
self.class.end(opts)
|
114
|
-
end
|
115
|
-
|
116
|
-
def tell(opts = nil)
|
117
|
-
self.class.tell(opts){|story| yield story }
|
118
|
-
end
|
119
|
-
|
120
134
|
# Allows one to temporarily override the current characters while
|
121
135
|
# the given block executes
|
122
136
|
#
|
@@ -129,8 +143,9 @@ module Scheherazade
|
|
129
143
|
end
|
130
144
|
|
131
145
|
def fill(character_or_model, *with)
|
132
|
-
|
133
|
-
|
146
|
+
char = to_character(character_or_model)
|
147
|
+
fill_attributes[char] = with
|
148
|
+
@characters[char] = current_fill unless to_model(char)
|
134
149
|
begin
|
135
150
|
@filling.push(character_or_model)
|
136
151
|
yield
|
data/lib/scheherazade/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scheherazade
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -91,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
segments:
|
93
93
|
- 0
|
94
|
-
hash:
|
94
|
+
hash: 3377084161247893965
|
95
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
96
|
none: false
|
97
97
|
requirements:
|
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
segments:
|
102
102
|
- 0
|
103
|
-
hash:
|
103
|
+
hash: 3377084161247893965
|
104
104
|
requirements: []
|
105
105
|
rubyforge_project:
|
106
106
|
rubygems_version: 1.8.24
|