Class: EPIC::ResourceFactory
- Inherits:
-
Object
- Object
- EPIC::ResourceFactory
- Includes:
- Singleton
- Defined in:
- src/epic.rb
Overview
Move this class to a separate file? Not needed quite yet…
Resource Factory for all our ReSTful resources.
Rackful::Server requires a resource factory. This singleton class implements EPIC’s resource factory.
Like every Singleton in a multi-threaded environment, this class must be thread safe!
Instance Method Summary (collapse)
- - (Resource?) [](path)
-
- (Hash< unslashified_path => resource_object >) resource_cache
private
For performance, this ResourceFactory maintains a cache of Resources it has produced earlier within this same request..
-
- (self) uncache(path)
Can be called by tainted resources, to be removed from the cache.
Instance Method Details
- (Resource?) [](path)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'src/epic.rb', line 65 def [] path path = path.to_path unless Rackful::Path == path.class path.unslashify! segments = path.segments cached = resource_cache[path] # Legal values for +cached+ are: # - nil: the resource is not in cache # - false: resource was requested earlier, without success # - Rackful::Resource if ! cached.nil? # if +cached+ is +false+, we want to return +nil+. return cached || nil end n = segments.length resource_cache[path] = if 0 === n StaticCollection.new( '/', [ 'handles/', #~ 'profiles/', 'generators/', #~ 'batches/' ] ) elsif 'handles' === segments[0] if 1 === n NAs.new( path.slashify ) elsif %r{\A\d+\z} === segments[1] if 2 === n Handles.new( path.slashify ) elsif 3 === n Handle.new path end end elsif 'generators' === segments[0] if 1 === n StaticCollection.new(path.slashify, Generator.generators.keys) elsif 2 === n generator = Generator.generators[segments[1]] generator && generator.new( path ) end end end |
- (Hash< unslashified_path => resource_object >) resource_cache (private)
For performance, this EPIC::ResourceFactory maintains a cache of Resources it has produced earlier within this same request.
Valid Hash values are:
- EPIC::Resource
A cached resource
- false
The resource has been requested earlier, but wasn’t found.
- nil
The resource hasn’t been requested yet.
122 123 124 |
# File 'src/epic.rb', line 122 def resource_cache Rackful::Request.current.env[:epic_resource_cache] ||= Hash.new end |
- (self) uncache(path)
Can be called by tainted resources, to be removed from the cache.
54 55 56 57 |
# File 'src/epic.rb', line 54 def uncache path resource_cache.delete path.to_s.unslashify self end |