Module: Ronin::Cacheable
Included Modules
Contextify, Ronin::Model
Public Visibility
Public Class Method Summary
| all(*arguments) |
Returns all instances of the model that matches the given query. |
|---|---|
| cache(path) |
Caches an object from the file at the specified path. |
| cache_all(path) |
Cache all objects defined in a file. |
| cached_path(repository = nil) |
The path to the file where the object was defined in. |
| cached_timestamp(repository = nil) |
The timestamp of the cached file. |
| first(*arguments) |
Returns the first instance of the model that matches the given. |
| included(base) | |
| load_all(attributes = {}) |
Loads all objects with the matching attributes. |
| load_all_from(path, &block) {|obj| ... } |
Loads all cacheable objects from the specified path. Returns: Array |
| load_first(attributes = {}, &block) {|objs| ... } |
Loads the first object with matching attributes. Returns: Cacheable |
| load_from(path) |
Loads an object from the file at the specified path. |
| models |
Returns: Array |
Public Instance Method Summary
| #cache! |
Deletes any previously cached copies of the object and caches it into. Returns: Boolean |
|---|---|
| #cached_path |
The path to the file where the object was defined in. |
| #cached_path=(value) |
The path to the file where the object was defined in. |
| #cached_timestamp |
The timestamp of the cached file. |
| #cached_timestamp=(value) |
The timestamp of the cached file. |
| #load_original! |
Loads the code from the cached file for the object, and instance evals. |
| #original_loaded? |
Returns: Boolean |
| #prepare_cache |
Prepares the object for caching. |
| #prepared_for_cache? |
Returns: Boolean |
| #sync! |
Deletes any previous cached copies of the object and caches it into. Returns: Boolean |
Public Instance Methods Included from Ronin::Model
Public Class Method Details
all
Returns all instances of the model that matches the given query arguments.
64 65 66 67 68 |
# File 'lib/ronin/cacheable.rb', line 64 def self.all(*arguments) super(*arguments).each do |obj| obj.instance_variable_set('@cache_prepared',true) end end |
cache
Caches an object from the file at the specified path.
87 88 89 90 91 92 |
# File 'lib/ronin/cacheable.rb', line 87 def self.cache(path) obj = self.load_from(File.(path)) obj.cache! return obj end |
cache_all
Cache all objects defined in a file.
184 185 186 187 188 189 190 |
# File 'lib/ronin/cacheable.rb', line 184 def Cacheable.cache_all(path) path = File.(path) Cacheable.load_all_from(path) do |obj| obj.cache! end end |
cached_path
The path to the file where the object was defined in
33 |
# File 'lib/ronin/cacheable.rb', line 33 property :cached_path, DataMapper::Types::FilePath |
cached_timestamp
The timestamp of the cached file
36 |
# File 'lib/ronin/cacheable.rb', line 36 property :cached_timestamp, DataMapper::Types::EpochTime |
first
Returns the first instance of the model that matches the given query arguments.
52 53 54 55 56 57 58 |
# File 'lib/ronin/cacheable.rb', line 52 def self.first(*arguments) if (obj = super(*arguments)) obj.instance_variable_set('@cache_prepared',true) end return obj end |
included
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ronin/cacheable.rb', line 27 def self.included(base) base.module_eval do include Contextify include Ronin::Model # The path to the file where the object was defined in property :cached_path, DataMapper::Types::FilePath # The timestamp of the cached file property :cached_timestamp, DataMapper::Types::EpochTime # # Initializes the cacheable object. # def initialize(*arguments,&block) @original_loaded = false @cache_prepared = false super(*arguments,&block) end # # Returns the first instance of the model that matches the given # query _arguments_. # def self.first(*arguments) if (obj = super(*arguments)) obj.instance_variable_set('@cache_prepared',true) end return obj end # # Returns all instances of the model that matches the given query # _arguments_. # def self.all(*arguments) super(*arguments).each do |obj| obj.instance_variable_set('@cache_prepared',true) end end # # Loads an object from the file at the specified _path_. # def self.load_from(path) path = File.(path) obj = self.load_context(path) obj.instance_variable_set('@original_loaded',true) obj.cached_path = path obj. = File.mtime(path) obj.prepare_cache return obj end # # Caches an object from the file at the specified _path_. # def self.cache(path) obj = self.load_from(File.(path)) obj.cache! return obj end # # Loads all objects with the matching _attributes_. # def self.load_all(attributes={}) self.all(attributes).map { |obj| obj.load_original! } end # # Loads the first object with matching attributes. # # @param [Hash] attributes # Attributes to search for. # # @yield [objs] # If a block is given, it will be passed all matching # objects to be filtered down. The first object from the filtered # objects will end up being selected. # # @yieldparam [Array<Cacheable>] objs # All matching objects. # # @return [Cacheable] # The loaded cached objects. # def self.load_first(attributes={},&block) obj = if block objs = self.all(attributes) (block.call(objs) || objs).first else self.first(attributes) end obj.load_original! if obj return obj end end unless Cacheable.models.include?(base) Cacheable.models << base end end |
load_all
Loads all objects with the matching attributes.
97 98 99 |
# File 'lib/ronin/cacheable.rb', line 97 def self.load_all(attributes={}) self.all(attributes).map { |obj| obj.load_original! } end |
load_all_from
Loads all cacheable objects from the specified path.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/ronin/cacheable.rb', line 160 def Cacheable.load_all_from(path,&block) path = File.(path) objs = Contextify.load_contexts(path).select do |obj| obj.class.include?(Cacheable) end objs.each do |obj| obj.instance_variable_set('@original_loaded',true) obj.cached_path = path obj. = File.mtime(path) obj.prepare_cache block.call(obj) if block end return objs end |
load_first
Loads the first object with matching attributes.
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ronin/cacheable.rb', line 118 def self.load_first(attributes={},&block) obj = if block objs = self.all(attributes) (block.call(objs) || objs).first else self.first(attributes) end obj.load_original! if obj return obj end |
load_from
Loads an object from the file at the specified path.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ronin/cacheable.rb', line 73 def self.load_from(path) path = File.(path) obj = self.load_context(path) obj.instance_variable_set('@original_loaded',true) obj.cached_path = path obj. = File.mtime(path) obj.prepare_cache return obj end |
models
141 142 143 |
# File 'lib/ronin/cacheable.rb', line 141 def Cacheable.models @@ronin_cacheable_models ||= [] end |
Public Instance Method Details
cache!
Deletes any previously cached copies of the object and caches it into the database.
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/ronin/cacheable.rb', line 223 def cache! if self.cached_path # delete any existing objects self.class.all(:cached_path => self.cached_path).destroy! self. = File.mtime(self.cached_path) return save end return false end |
cached_path
The path to the file where the object was defined in
33 |
# File 'lib/ronin/cacheable.rb', line 33 property :cached_path, DataMapper::Types::FilePath |
cached_path=
The path to the file where the object was defined in
33 |
# File 'lib/ronin/cacheable.rb', line 33 property :cached_path, DataMapper::Types::FilePath |
cached_timestamp
The timestamp of the cached file
36 |
# File 'lib/ronin/cacheable.rb', line 36 property :cached_timestamp, DataMapper::Types::EpochTime |
cached_timestamp=
The timestamp of the cached file
36 |
# File 'lib/ronin/cacheable.rb', line 36 property :cached_timestamp, DataMapper::Types::EpochTime |
load_original!
Loads the code from the cached file for the object, and instance evals it into the object.
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/ronin/cacheable.rb', line 205 def load_original! if (self.cached_path && !(@original_loaded)) block = self.class.load_context_block(self.cached_path) instance_eval(&block) if block @original_loaded = true end return self end |
original_loaded?
197 198 199 |
# File 'lib/ronin/cacheable.rb', line 197 def original_loaded? @original_loaded == true end |
prepare_cache
Prepares the object for caching.
273 274 275 276 277 278 |
# File 'lib/ronin/cacheable.rb', line 273 def prepare_cache unless @cache_prepared instance_eval(&(@cache_block)) if @cache_block @cache_prepared = true end end |
prepared_for_cache?
266 267 268 |
# File 'lib/ronin/cacheable.rb', line 266 def prepared_for_cache? @cache_prepared == true end |
sync!
Deletes any previous cached copies of the object and caches it into the database, only if the file where the object was originally cached from was modified. The object will also be destroyed if the file where the object was originally cached from is missing.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/ronin/cacheable.rb', line 244 def sync! if (self.cached_path && self.) if File.file?(self.cached_path) if File.mtime(self.cached_path) > self. self.class.cache(self.cached_path) else return false end else self.destroy end return true end return false end |
Protected Visibility
Protected Instance Method Summary
| #cache(&block) |
Will run the specified block when the object is about to be cached. |
|---|
Protected Instance Method Details
cache
Will run the specified block when the object is about to be cached.
289 290 291 |
# File 'lib/ronin/cacheable.rb', line 289 def cache(&block) @cache_block = block end |