Module: Scheherazade::Story::ClassMethods

Included in:
Scheherazade::Story
Defined in:
lib/scheherazade/story.rb

Instance Method Summary collapse

Instance Method Details

#beginObject

Begins a story within the current story. Should be balanced with a call to end



13
14
15
16
# File 'lib/scheherazade/story.rb', line 13

def begin
  (Thread.current[:scheherazade_stories] ||= []).push Story.new
  current
end

#currentObject



6
7
8
# File 'lib/scheherazade/story.rb', line 6

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



21
22
23
24
25
# File 'lib/scheherazade/story.rb', line 21

def end(opts = nil)
  current.send :rollback if opts && opts[:rollback]
  Thread.current[:scheherazade_stories].pop
  current
end

#tell(opts = nil) ⇒ Object

Begins a substory, yields, and ends the story



29
30
31
32
33
# File 'lib/scheherazade/story.rb', line 29

def tell(opts = nil)
  yield self.begin
ensure
  self.end(opts)
end

#to_character(character_or_model) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/scheherazade/story.rb', line 35

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



46
47
48
# File 'lib/scheherazade/story.rb', line 46

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



51
52
53
# File 'lib/scheherazade/story.rb', line 51

def to_model(character)
  character.to_s.camelize.safe_constantize
end