Class: Specify::Session

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/specify/session.rb

Overview

A Session is responsible for logging an existing Specify::Model::User in and out of a Specify::Model::Collection. Session objects are used to set the #created_by and #modified_by attributes for any record created or updated to the session #user.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, collection) ⇒ Session

Returns a new Session.

user is a String with an existing Specify::Model::User#name.

collection is a String with and existing Specify::Model::Collection#name.



37
38
39
40
41
42
43
# File 'lib/specify/session.rb', line 37

def initialize(user, collection)
  @user = Model::User.first(Name: user)
  @collection = Model::Collection.first(CollectionName: collection)
  @discipline = @collection.discipline
  @division = @discipline.division
  @active = false
end

Instance Attribute Details

#activeObject (readonly)

true if the session is open (the #user is logged in), false otherwise.



14
15
16
# File 'lib/specify/session.rb', line 14

def active
  @active
end

#collectionObject (readonly)

The Specify::Model::Collection that #user is logged in to during the life span of the session.



18
19
20
# File 'lib/specify/session.rb', line 18

def collection
  @collection
end

#disciplineObject (readonly)

The Specify::Model::Discipline that #user is logged in to during the life span of the session.



22
23
24
# File 'lib/specify/session.rb', line 22

def discipline
  @discipline
end

#divisionObject (readonly)

The Specify::Model::Division that #collection and #discipline belong to.



25
26
27
# File 'lib/specify/session.rb', line 25

def division
  @division
end

#userObject (readonly)

The Specify::Model::User that is logged in to #collection during the session.



29
30
31
# File 'lib/specify/session.rb', line 29

def user
  @user
end

Instance Method Details

#closeObject

Closes this session, logs #user out of #collection.



52
53
54
55
56
57
58
# File 'lib/specify/session.rb', line 52

def close
  @user.log_out
  @active = false
  changed
  notify_observers(self)
  self
end

#inspectObject

Creates a string representation of self.



46
47
48
49
# File 'lib/specify/session.rb', line 46

def inspect
  "#{self} specify user: #{@user}"\
  ", collection: #{@collection}, open: #{@active}"
end

#openObject

Opens this session, logs #user in to #collection.



61
62
63
64
65
# File 'lib/specify/session.rb', line 61

def open
  @user.(@collection)
  @active = true
  self
end

#open?Boolean

Returns true if this session is currently open.

Returns:

  • (Boolean)


68
69
70
# File 'lib/specify/session.rb', line 68

def open?
  @active
end

#session_agentObject

Returns the Specify::Model::Agent for #user in #division.



73
74
75
# File 'lib/specify/session.rb', line 73

def session_agent
  user.logged_in_agent
end