Class: Fastaccess::Fastaccess
- Inherits:
-
Object
- Object
- Fastaccess::Fastaccess
- Defined in:
- lib/fastaccess/fastaccess.rb
Overview
This class contains the most relevant helpers methods, which doesn’t need to be located elsewhere. (e.g. like mixins)
Constant Summary collapse
- ACTS_OPTIONS_DEFAULTS =
the default options for the acts_with_fastaccess_on method.
{ :auto_update => true, :versions => [], }
- @@fastaccess_on =
Hash.new Set.new
- @@last_updated =
Hash.new
- @@registered_options =
Hash.new ACTS_OPTIONS_DEFAULTS
Class Method Summary collapse
-
.alias_for(method) ⇒ Symbol
returns the aliased name for any given method.
-
.get(redis_id) ⇒ Object
getting redis content.
-
.id_for(class_instance) ⇒ String
creates a fastaccess id for a class_instance.
-
.merge_defaults(options) ⇒ Hash
merges the, hopefully, reasonable defaults with the supplied options hash.
-
.options_for(class_name, method_name) ⇒ Hash
gets the options for a class_name, method_name pair.
-
.options_id_for(class_name, method_name) ⇒ String
creates the id for the registered_options hash.
-
.redis ⇒ Redis
getting the redis instance.
-
.redis_id_for(class_instance, method, args = []) ⇒ Object
returns the id used by fastaccess for the storage of content in the redis database.
-
.register_on(class_name, method_name, options = {}) ⇒ Object
registers a method, defined on a certain class, as being handled by fastaccess.
-
.registered?(class_name, method_name) ⇒ Boolean
inquires if a certain method, which is defined on a given class, is registered with fastaccess.
-
.set(redis_id, content) ⇒ Object
setting content in redis.
-
.set_redis(redis_instance) ⇒ Object
setting the global redis instance for fastaccess.
-
.setup(&block) ⇒ Object
setting up the environment for fastaccess.
-
.update_check(class_instance) ⇒ Boolean
checks if a class_instance seems to be up to date according to updated_at timestamp.
-
.update_content(obj, options = {}) ⇒ Object
manually update content in redis for a given object.
-
.update_info(class_instance) ⇒ Object
updates the timestamp in the redis database with the one from class_instance.
Class Method Details
.alias_for(method) ⇒ Symbol
returns the aliased name for any given method.
113 114 115 |
# File 'lib/fastaccess/fastaccess.rb', line 113 def self.alias_for(method) :"aliased_#{method}" end |
.get(redis_id) ⇒ Object
getting redis content
128 129 130 131 132 133 134 135 |
# File 'lib/fastaccess/fastaccess.rb', line 128 def self.get(redis_id) response = redis.get(redis_id) begin return JSON.parse response rescue JSON::ParserError return response end end |
.id_for(class_instance) ⇒ String
creates a fastaccess id for a class_instance
97 98 99 |
# File 'lib/fastaccess/fastaccess.rb', line 97 def self.id_for(class_instance) "#{class_instance.class}-#{class_instance.id}" end |
.merge_defaults(options) ⇒ Hash
merges the, hopefully, reasonable defaults with the supplied options hash
200 201 202 |
# File 'lib/fastaccess/fastaccess.rb', line 200 def self.merge_defaults() ACTS_OPTIONS_DEFAULTS.merge() end |
.options_for(class_name, method_name) ⇒ Hash
gets the options for a class_name, method_name pair
58 59 60 61 |
# File 'lib/fastaccess/fastaccess.rb', line 58 def self.(class_name, method_name) id = (class_name, method_name) self.[id] end |
.options_id_for(class_name, method_name) ⇒ String
creates the id for the registered_options hash
105 106 107 |
# File 'lib/fastaccess/fastaccess.rb', line 105 def self.(class_name, method_name) "#{class_name}-#{method_name}" end |
.redis ⇒ Redis
getting the redis instance
190 191 192 |
# File 'lib/fastaccess/fastaccess.rb', line 190 def self.redis @@redis end |
.redis_id_for(class_instance, method, args = []) ⇒ Object
returns the id used by fastaccess for the storage of content in the redis database
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/fastaccess/fastaccess.rb', line 209 def self.redis_id_for(class_instance, method, args=[]) opts = self.(class_instance.class, method) fastaccess_id = self.id_for(class_instance) base_id = "#{method}_#{fastaccess_id}" return base_id if opts[:versions].empty? opts[:versions].each do |version| if self.match_version(version, args) sha = Digest::SHA2.new << version.inspect return "#{base_id}:#{sha}" end end base_id end |
.register_on(class_name, method_name, options = {}) ⇒ Object
registers a method, defined on a certain class, as being handled by fastaccess.
36 37 38 39 40 |
# File 'lib/fastaccess/fastaccess.rb', line 36 def self.register_on(class_name, method_name, ={}) self.fastaccess_on[class_name] << method_name id = (class_name, method_name) self.[id] = self.[id].merge() end |
.registered?(class_name, method_name) ⇒ Boolean
inquires if a certain method, which is defined on a given class, is registered with fastaccess.
48 49 50 |
# File 'lib/fastaccess/fastaccess.rb', line 48 def self.registered?(class_name, method_name) self.fastaccess_on[class_name].include? method_name end |
.set(redis_id, content) ⇒ Object
setting content in redis
121 122 123 |
# File 'lib/fastaccess/fastaccess.rb', line 121 def self.set(redis_id, content) redis.set(redis_id, (content.is_a? String) ? content : content.to_json) end |
.set_redis(redis_instance) ⇒ Object
setting the global redis instance for fastaccess.
180 181 182 183 184 185 186 |
# File 'lib/fastaccess/fastaccess.rb', line 180 def self.set_redis(redis_instance) if redis_instance @@redis = redis_instance else @@redis = $redis if $redis end end |
.setup(&block) ⇒ Object
setting up the environment for fastaccess
174 175 176 |
# File 'lib/fastaccess/fastaccess.rb', line 174 def self.setup(&block) instance_eval &block if block_given? end |
.update_check(class_instance) ⇒ Boolean
checks if a class_instance seems to be up to date according to updated_at timestamp. This only works if the registered method is actually dependent (and only dependent) on model attributes.
72 73 74 75 76 |
# File 'lib/fastaccess/fastaccess.rb', line 72 def self.update_check(class_instance) id = self.id_for(class_instance) return true if self.last_updated[id] == false class_instance.updated_at == self.last_updated[id] end |
.update_content(obj, options = {}) ⇒ Object
manually update content in redis for a given object.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/fastaccess/fastaccess.rb', line 151 def self.update_content(obj, ={}) class_name = obj.is_a?(Class) ? obj : obj.class methods = if method = [:on] if registered? class_name, method [method] else [] end else fastaccess_on[class_name] end methods.each do |method| callable = obj.method( alias_for(method) ) content = if [:arguments] callable.call(*[:arguments]) else callable.call end self.set("#{method}_#{id_for(obj)}", content) end end |
.update_info(class_instance) ⇒ Object
updates the timestamp in the redis database with the one from class_instance. usually called after there was new content pushed into redis (or content was updated)
85 86 87 88 89 90 |
# File 'lib/fastaccess/fastaccess.rb', line 85 def self.update_info(class_instance) id = self.id_for(class_instance) unless self.last_updated[id] == false self.last_updated[id] = class_instance.updated_at end end |