Class: Fastaccess::Fastaccess

Inherits:
Object
  • Object
show all
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

Class Method Details

.alias_for(method) ⇒ Symbol

returns the aliased name for any given method.

Parameters:

  • method (Symbol)

    a symbol denoting the method.

Returns:

  • (Symbol)

    aliased method symbol.



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

Parameters:

  • redis_id (String)

    the id

Returns:

  • (Object)

    stored 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

Parameters:

  • class_instance (Object)

    any Object, preferably a decendent of an actual Rails Model.

Returns:

  • (String)

    the identifier



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

Parameters:

  • options (Hash)

    the pertaining options supplied by the user

Returns:

  • (Hash)

    the merged options hash



200
201
202
# File 'lib/fastaccess/fastaccess.rb', line 200

def self.merge_defaults(options)
  ACTS_OPTIONS_DEFAULTS.merge(options)
end

.options_for(class_name, method_name) ⇒ Hash

gets the options for a class_name, method_name pair

Parameters:

  • class_name (Class)

    is the actual Class.

  • method_name (Symbol)

    is the symbol denoting the actual method

Returns:

  • (Hash)

    the options for the pair



58
59
60
61
# File 'lib/fastaccess/fastaccess.rb', line 58

def self.options_for(class_name, method_name)
  id = options_id_for(class_name, method_name)
  self.registered_options[id]
end

.options_id_for(class_name, method_name) ⇒ String

creates the id for the registered_options hash

Parameters:

  • class_name (Class)

    a class singleton object

  • method_name (Symbol)

    is the identifying symbol of a method

Returns:

  • (String)

    the identifier



105
106
107
# File 'lib/fastaccess/fastaccess.rb', line 105

def self.options_id_for(class_name, method_name)
  "#{class_name}-#{method_name}"
end

.redisRedis

getting the redis instance

Returns:

  • (Redis)

    the 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

Parameters:

  • class_instance (Object)

    instance of a registered class

  • method (Symbol)

    is a symbol denoting the called method

  • args (Array) (defaults to: [])

    arguments supplied to the method on the call



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.options_for(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.

Parameters:

  • class_name (Class)

    is the actual Class.

  • method_name (Symbol)

    is the symbol denoting the actual method



36
37
38
39
40
# File 'lib/fastaccess/fastaccess.rb', line 36

def self.register_on(class_name, method_name, options={})
  self.fastaccess_on[class_name] << method_name
  id = options_id_for(class_name, method_name)
  self.registered_options[id] = self.registered_options[id].merge(options)
end

.registered?(class_name, method_name) ⇒ Boolean

inquires if a certain method, which is defined on a given class, is registered with fastaccess.

Parameters:

  • class_name (Class)

    is the actual Class.

  • method_name (Symbol)

    is the symbol denoting the actual method

Returns:

  • (Boolean)


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

Parameters:

  • redis_id (String)

    the id

  • content (Object)

    should be basic content e.g. String, Hash, Array or Numbers.



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.

Parameters:

  • redis_instance (Redis)

    The Connection to a redis server



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.

Parameters:

  • class_instance (Object)

    any Object, preferably a decendent of an actual Rails Model.

Returns:

  • (Boolean)

    is true if everything is up to date.



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.

Parameters:

  • obj (Object)

    any Object, preferably a decendent of an actual Rails Model.

  • options (Hash) (defaults to: {})

    a simple hash

Options Hash (options):

  • :on (Symbol)

    a certain registered method.

  • :arguments (Array)

    an array of arguments passed to the :on method or, if :on is not present, every method registered with fastaccess on the pertaining class.



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, options={})
  class_name = obj.is_a?(Class) ? obj : obj.class
  methods = if method = options[: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 options[:arguments]
                callable.call(*options[: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)

Parameters:

  • class_instance (Object)

    any Object, preferably a decendent of an actual Rails Model.



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