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.
@@ -8,7 +8,7 @@ module Scheherazade
8
8
  while (c = story.characters[@chain.first])
9
9
  @chain.unshift c
10
10
  end
11
- @model = @chain.first
11
+ @model = Story.to_model(@chain.first)
12
12
  story.send(:building) << @ar = @model.new
13
13
  @chain.each{|c| story.current[c] = @ar }
14
14
  end
@@ -2,47 +2,72 @@ module Scheherazade
2
2
  class Story < Hash
3
3
  attr_reader :fill_attributes, :characters, :counter, :current
4
4
 
5
- def self.current
6
- (Thread.current[:scheherazade_stories] ||= []).last || TOP
7
- end
5
+ module ClassMethods
6
+ def current
7
+ (Thread.current[:scheherazade_stories] ||= []).last || TOP
8
+ end
8
9
 
9
- # Begins a story within the current story.
10
- # Should be balanced with a call to +end+
11
- #
12
- def self.begin
13
- (Thread.current[:scheherazade_stories] ||= []).push Story.new
14
- current
15
- end
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
- # Ends the current substory and comes back
18
- # to the previous current story
19
- #
20
- def self.end(opts = nil)
21
- current.send :rollback if opts && opts[:rollback]
22
- Thread.current[:scheherazade_stories].pop
23
- current
24
- end
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
- # Begins a substory, yields, and ends the story
27
- #
28
- def self.tell(opts = nil)
29
- yield self.begin
30
- ensure
31
- self.end(opts)
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{|h, k| parent.current[k] if parent}
38
- @fill_attributes = Hash.new{|h, k| parent.fill_attributes[k] if parent }
39
- @characters = Hash.new do |h, k|
40
- if parent
41
- parent.characters[k]
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(character, attributes = nil)
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
- fill_attributes[character_or_model] = with
133
- @characters[character_or_model] = current_fill if character_or_model.is_a? Symbol
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
@@ -1,3 +1,3 @@
1
1
  module Scheherazade
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
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-05 00:00:00.000000000 Z
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: -3529941096747853729
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: -3529941096747853729
103
+ hash: 3377084161247893965
104
104
  requirements: []
105
105
  rubyforge_project:
106
106
  rubygems_version: 1.8.24