Module: Scheherazade::Story::ClassMethods
- Included in:
- Scheherazade::Story
- Defined in:
- lib/scheherazade/story.rb
Instance Method Summary collapse
-
#begin ⇒ Object
Begins a story within the current story.
- #current ⇒ Object
-
#end(opts = nil) ⇒ Object
Ends the current substory and comes back to the previous current story.
-
#tell(opts = nil) ⇒ Object
Begins a substory, yields, and ends the story.
- #to_character(character_or_model) ⇒ Object
- #to_character!(character_or_model) ⇒ Object
-
#to_model(character) ⇒ Object
Returns a Model or nil.
Instance Method Details
#begin ⇒ Object
Begins a story within the current story. Should be balanced with a call to end
16 17 18 19 |
# File 'lib/scheherazade/story.rb', line 16 def begin (Thread.current[:scheherazade_stories] ||= []).push Story.new current end |
#current ⇒ Object
9 10 11 |
# File 'lib/scheherazade/story.rb', line 9 def current (Thread.current[:scheherazade_stories] ||= []).last || TOP end |
#end(opts = nil) ⇒ Object
Ends the current substory and comes back to the previous current story
24 25 26 27 28 |
# File 'lib/scheherazade/story.rb', line 24 def end(opts = nil) current.send :rollback, opts && opts[:rollback] Thread.current[:scheherazade_stories].pop current end |
#tell(opts = nil) ⇒ Object
Begins a substory, yields, and ends the story
32 33 34 35 36 |
# File 'lib/scheherazade/story.rb', line 32 def tell(opts = nil) yield self.begin ensure self.end(opts) end |
#to_character(character_or_model) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/scheherazade/story.rb', line 38 def to_character(character_or_model) case character_or_model when Class character_or_model.name.underscore.to_sym when Symbol character_or_model else raise ArgumentError, "expected character or Model, got #{character_or_model.ancestors}" end end |
#to_character!(character_or_model) ⇒ Object
49 50 51 |
# File 'lib/scheherazade/story.rb', line 49 def to_character!(character_or_model) to_character(character_or_model) unless character_or_model.is_a?(Symbol) end |
#to_model(character) ⇒ Object
Returns a Model or nil
54 55 56 |
# File 'lib/scheherazade/story.rb', line 54 def to_model(character) character.to_s.camelize.safe_constantize end |