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

humanize_attributes

Public Class Method Details

all

public all(*arguments)

Returns all instances of the model that matches the given query arguments.

[View source]


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

public cache(path)

Caches an object from the file at the specified path.

[View source]


87
88
89
90
91
92
# File 'lib/ronin/cacheable.rb', line 87

def self.cache(path)
  obj = self.load_from(File.expand_path(path))

  obj.cache!
  return obj
end

cache_all

public cache_all(path)

Cache all objects defined in a file.

Meta Tags

Parameters:

[String] path

The path to cache all objects from.

[View source]


184
185
186
187
188
189
190
# File 'lib/ronin/cacheable.rb', line 184

def Cacheable.cache_all(path)
  path = File.expand_path(path)

  Cacheable.load_all_from(path) do |obj|
    obj.cache!
  end
end

cached_path

public cached_path(repository = nil)

The path to the file where the object was defined in

[View source]


33
# File 'lib/ronin/cacheable.rb', line 33

property :cached_path, DataMapper::Types::FilePath

cached_timestamp

public cached_timestamp(repository = nil)

The timestamp of the cached file

[View source]


36
# File 'lib/ronin/cacheable.rb', line 36

property :cached_timestamp, DataMapper::Types::EpochTime

first

public first(*arguments)

Returns the first instance of the model that matches the given query arguments.

[View source]


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

public included(base)
[View source]


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.expand_path(path)
      obj = self.load_context(path)

      obj.instance_variable_set('@original_loaded',true)
      obj.cached_path = path
      obj.cached_timestamp = 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.expand_path(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

public load_all(attributes = {})

Loads all objects with the matching attributes.

[View source]


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

public Array load_all_from(path, &block) {|obj| ... }

Loads all cacheable objects from the specified path.

Meta Tags

Parameters:

[String] path

The path to load the objects from.

Yields:

[obj]

If a block is given, it will be passed each loaded object.

Yield Parameters:

[Cacheable] obj

An object loaded from the specified path.

Returns:

[Array]

All objects loaded from the specified path.

[View source]


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.expand_path(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.cached_timestamp = File.mtime(path)
    obj.prepare_cache

    block.call(obj) if block
  end

  return objs
end

load_first

public Cacheable load_first(attributes = {}, &block) {|objs| ... }

Loads the first object with matching attributes.

Meta Tags

Parameters:

[Hash] attributes

Attributes to search for.

Yields:

[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.

Yield Parameters:

[Array<Cacheable>] objs

All matching objects.

Returns:

[Cacheable]

The loaded cached objects.

[View source]


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

public load_from(path)

Loads an object from the file at the specified path.

[View source]


73
74
75
76
77
78
79
80
81
82
# File 'lib/ronin/cacheable.rb', line 73

def self.load_from(path)
  path = File.expand_path(path)
  obj = self.load_context(path)

  obj.instance_variable_set('@original_loaded',true)
  obj.cached_path = path
  obj.cached_timestamp = File.mtime(path)
  obj.prepare_cache
  return obj
end

models

public Array models

Meta Tags

Returns:

[Array]

List of cacheable models.

[View source]


141
142
143
# File 'lib/ronin/cacheable.rb', line 141

def Cacheable.models
  @@ronin_cacheable_models ||= []
end

Public Instance Method Details

cache!

public Boolean cache!

Deletes any previously cached copies of the object and caches it into the database.

Meta Tags

Returns:

[Boolean]

Specifies whether the object was successfully cached,

[View source]


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.cached_timestamp = File.mtime(self.cached_path)
    return save
  end

  return false
end

cached_path

public cached_path

The path to the file where the object was defined in

[View source]


33
# File 'lib/ronin/cacheable.rb', line 33

property :cached_path, DataMapper::Types::FilePath

cached_path=

public cached_path=(value)

The path to the file where the object was defined in

[View source]


33
# File 'lib/ronin/cacheable.rb', line 33

property :cached_path, DataMapper::Types::FilePath

cached_timestamp

public cached_timestamp

The timestamp of the cached file

[View source]


36
# File 'lib/ronin/cacheable.rb', line 36

property :cached_timestamp, DataMapper::Types::EpochTime

cached_timestamp=

public cached_timestamp=(value)

The timestamp of the cached file

[View source]


36
# File 'lib/ronin/cacheable.rb', line 36

property :cached_timestamp, DataMapper::Types::EpochTime

load_original!

public load_original!

Loads the code from the cached file for the object, and instance evals it into the object.

[View source]


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?

public Boolean original_loaded?

Meta Tags

Returns:

[Boolean]

Specifies whether the original code has been loaded into the object.

[View source]


197
198
199
# File 'lib/ronin/cacheable.rb', line 197

def original_loaded?
  @original_loaded == true
end

prepare_cache

public prepare_cache

Prepares the object for caching.

[View source]


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?

public Boolean prepared_for_cache?

Meta Tags

Returns:

[Boolean]

Specifies whether the object has been prepared to be cached,

[View source]


266
267
268
# File 'lib/ronin/cacheable.rb', line 266

def prepared_for_cache?
  @cache_prepared == true
end

sync!

public Boolean 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.

Meta Tags

Returns:

[Boolean]

Specifies whether the object was successfully synced.

[View source]


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.cached_timestamp)
    if File.file?(self.cached_path)
      if File.mtime(self.cached_path) > self.cached_timestamp
        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

protected cache(&block)

Will run the specified block when the object is about to be cached.

Meta Tags

Yields:

[Object]
The block will be ran inside the object when the object is to be prepared for caching.
[View source]


289
290
291
# File 'lib/ronin/cacheable.rb', line 289

def cache(&block)
  @cache_block = block
end
Generated on Friday, September 25 2009 at 02:57:12 PM by YARD 0.2.3.5 (ruby-1.8.6).