Class: ObjectifiedSessionGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/objectified_session_generator.rb

Overview

The ObjectifiedSessionGenerator is what gets invoked when you run rails generate objectified_session. It looks at whatever you have set as your ObjectifiedSessions class (which, of course, is overwhelmingly going to be the default at this point, since users likely won’t have changed/customized it before they run this), and then plunks a file under lib/, in the right place, with an empty class, with nice comments in it.

Instance Method Summary collapse

Instance Method Details

#create_session_fileObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/objectified_session_generator.rb', line 10

def create_session_file
  class_name = ::ObjectifiedSessions.session_class

  # Check to see if this class exists in Ruby -- if so, we don't want to do anything; we don't want to rely on users
  # putting it under lib/, in the exact same place we would.
  if class_exists?(class_name)
    say "You appear to already have a class #{class_name.inspect}; doing nothing."
  else
    class_name = class_name.name if class_name.kind_of?(Class)
    class_path = class_name.underscore
    target_file = File.expand_path(File.join(Rails.root, 'lib', class_path + ".rb"))

    if (! File.exist?(target_file))
      # The success case -- write the class.
      write_objsession_class(target_file, class_name)
      say "Class #{class_name} created at #{target_file.inspect}."
    else
      # Somehow, we can't resolve the class, yet there's a file on disk in exactly the same place we want to put
      # one. Let the user know, and bail out.
      say %{You've configured ObjectifiedSessions to use class #{class_name} as your session class;
I can't currently load that class, but there's already a file at the following path:

#{target_file.inspect}

Please check that file to see what class it contains; if it's incorrect, remove it, and try again.}
    end
  end
end