Module: ObjectifiedSessions

Defined in:
lib/objectified_sessions.rb,
lib/objectified_sessions/base.rb,
lib/objectified_sessions/errors.rb,
lib/objectified_sessions/version.rb,
lib/objectified_sessions/field_definition.rb

Overview

ObjectifiedSessions is the outermost interface to the ObjectifiedSessions Gem. This module exists only as a namespace (i.e., is not included into any classes), and has a single public method, #session_class, that lets you configure which class is to be used as your objsession.

Defined Under Namespace

Modules: Errors Classes: Base, FieldDefinition

Constant Summary collapse

DEFAULT_OBJSESSION_CLASS_NAME =
"Objsession"
VERSION =
"1.0.2"

Class Method Summary collapse

Class Method Details

._create_new_objsession(underlying_session) ⇒ Object

Should be called from code internal to the ObjectifiedSessions Gem only. Given the underlying Session object (as returned by ‘#session` in a controller), creates a new instance of the correct objectified-session class and returns it.

This method is actually trivially simple; it’s more than two lines just because we want to be careful to raise good, usable exceptions if there’s a problem.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/objectified_sessions.rb', line 19

def _create_new_objsession(underlying_session)
  klass = _session_class_object
  out = nil

  # Create a new instance...
  begin
    out = klass.new(underlying_session)
  rescue Exception => e
    raise ObjectifiedSessions::Errors::CannotCreateSessionError, %{When objectified_sessions went to create a new instance of the session class, it
got an exception from the call to #{klass.name}.new:

(#{e.class.name}) #{e.message}
#{e.backtrace.join("\n    ")}}
  end

  # ...and make sure it's a subclass of ::ObjectifiedSessions::Base.
  unless out.kind_of?(::ObjectifiedSessions::Base)
    raise ObjectifiedSessions::Errors::CannotCreateSessionError, %{When objectified_sessions went to create a new instance of the session class, it
got back an object that isn't an instance of a subclass of ObjectifiedSessions::Base.

It got back an instance of #{out.class.name}:
#{out.inspect}}
  end

  out
end

.session_classObject

Returns the session class that’s been set – in whatever format it’s been set. This means that the return value can be a String, Symbol, or Class, depending on how the client set it.



48
49
50
# File 'lib/objectified_sessions.rb', line 48

def session_class
  @session_class ||= DEFAULT_OBJSESSION_CLASS_NAME
end

.session_class=(target_class) ⇒ Object

Sets the class that should be instantiated and bound to #objsession in controllers. You can pass a String or Symbol that’s the name of the class, or the actual Class object itself.

Class loading: if the class is not already loaded, then ObjectifiedSessions will attempt to load it, using Kernel#require, using a file path that’s the Rails-style mapping from the name of the class. (In other words, if you pass ‘Foo::BarBaz’ for target_class, then ObjectifiedSessions will require 'foo/bar_baz'.)

However specified, the class must be a subclass of ObjectifiedSessions::Base, or you’ll get an error when you call #objsession.

Note that this is evaluated the first time you call #objsession from within a controller, not immediately. This means your application will be fully booted and all of Rails available when you do this, but it also means that if you set a class that can’t be resolved or has an error in it, you won’t find out until you first try to access the #objsession. Be aware.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/objectified_sessions.rb', line 66

def session_class=(target_class)
  unless [ String, Symbol, Class ].include?(target_class.class)
    raise ArgumentError, "You must pass a String, Symbol, or Class, not: #{target_class.inspect}"
  end

  if target_class.kind_of?(String) || target_class.kind_of?(Symbol)
    target_class = target_class.to_s.camelize
  end

  @session_class = target_class
  @_session_class_object = nil
end