Class: SWS::ComponentCache

Inherits:
Object
  • Object
show all
Defined in:
lib/sws/session.rb

Overview

TODO: not sure if it works entirely correctly, but for now it works good enough

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComponentCache

Returns a new instance of ComponentCache.



122
123
124
125
126
127
# File 'lib/sws/session.rb', line 122

def initialize	
	@components = Array.new
	# Object_ids of components removed from cache. Useful when handling
	# backtracking.
	@old_component_ids = Array.new
end

Class Method Details

.max_components_per_session=(max) ⇒ Object



117
118
119
# File 'lib/sws/session.rb', line 117

def ComponentCache.max_components_per_session= ( max )
	@@max_components_per_session = max
end

Instance Method Details

#add_component(component) ⇒ Object

Adds a component to cache. Returns false if component was nil. Size of component cache can be customized in application.yaml



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sws/session.rb', line 133

def add_component ( component )

	unless @components.include?( component )
		@components << component
		if @components.length > @@max_components_per_session
			# As checking for only one copy of a component is already done when
			# inserting into @components, we don't need to do this here. We only
			# store object_ids to limit memory footprint.
			@old_component_ids << @components.shift.object_id
		end
	end

end

#component(component_id) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/sws/session.rb', line 148

def component ( component_id )

	if( component_id == @components.last.object_id )
		return @components.last
	else
		component = @components.find { |c| c.object_id == component_id }
 	end		
	
	unless component
		return nil
	else
		# Old behaviour (for optimalization)
		# Remove all components that are after found component - they are in the
		# "branch" the client shouldn't have access to. Of course they are added
		# to @old_component_ids for the case user backtracks to them.
		# 
		# Accidentally, this works correctly even if component ==
		# @components.last (that means there was no backtracking) - that is, it
		# doesn't remove anything.
		#
		#@old_component_ids.concat( 
		#	@components.slice!( @components.index( component ) + 1 .. -1 ).collect { |com| com.object_id }
		#)
		
#				@components.push( @components.delete( component ) )
#				puts @components.each { |c| print "-- ", c.object_id, "\n" }
#				puts component_id
		return component
	end
		
end

#lastObject

This method returns last component in array - this is used when we need direct access to some common component atributes (i.e. request.headers) TODO: maybe it is unnecessary (i mean: there is better way to do this)



189
190
191
# File 'lib/sws/session.rb', line 189

def last
	return @components.last
end

#old?(component_id) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/sws/session.rb', line 181

def old? ( component_id )
	return @old_component_ids.include?( component_id )
end