lib/scheherazade/story.rb
def imagine(character_or_model, attributes = nil)
      character = to_character(character_or_model)
      prev, @building = @building, [] # because method might be re-entrant
      CharacterBuilder.new(character).build(attributes) do |ar|
        ar.save!
        # While errors on records associated with :has_many will prevent records
        # from being saved, they won't for :belongs_to, so:
        @building.each do |built|
          raise ActiveRecord::RecordInvalid, built unless built.persisted? || built.valid?
        end
        Scheherazade.log(:saving, character, ar)
        handle_callbacks(@building)
      end
    ensure
      @built.concat(@building)
      @building = prev
    end
def imagine(character_or_model, attributes = nil)
      character = to_character(character_or_model)
      prev, @building = @building, [] # because method might be re-entrant
      CharacterBuilder.new(character).build(attributes) do |ar|
        ar.save!
        # While errors on records associated with :has_many will prevent records
        # from being saved, they won't for :belongs_to, so:
        @building.each do |built|
          raise ActiveRecord::RecordInvalid, built unless built.persisted? || built.valid?
        end
        Scheherazade.log(:saving, character, ar)
        handle_callbacks(@building)
      end
    ensure
      @built.concat(@building)
      @building = prev
    end
Creates a character with the given attributes

 A character can be designated either by the model (e.g. `User`), the corresponding
 symbol (`:user`) or the symbol for a specialized type of character, defined using +fill+
 (e.g. `:admin`).

 The attributes can be nil, a list of symbols, a hash or a combination of both
 These, along with attributes passed to +fill+ for the current stories
 and the mandatory attributes for the model will be provided.

 If some fields generate validation errors, they will be provided also.

 For associations, the values can also be a character (Symbol or Model),
 integers (meaning the default Model * that integer) or arrays of characters.

    imagine(:user, :account, :company => :fortune_500_company, :emails => 3)

 Similarly:

    User.imagine(...)
    :user.imagine(...)

 This record (and any other imagined through associations) will become the
 current character in the current story:

    story.current[User] # => nil
    story.tell do
      :admin.imagine # => A User record
      story.current[:admin] # => same
      story.current[User]   # => same
    end
    story.current[User] # => nil