Class: Cached::ConfigCompiler
- Inherits:
-
Object
- Object
- Cached::ConfigCompiler
- Defined in:
- lib/cached/config_compiler.rb
Instance Method Summary collapse
- #compiled_fetch_method_for(index) ⇒ Object
- #compiled_fetch_method_for_primary_key ⇒ Object
- #compiled_fetch_methods ⇒ Object
- #compiled_meta_methods ⇒ Object
- #compiled_save_index_method ⇒ Object
- #compiled_save_methods ⇒ Object
- #compiled_save_object_method ⇒ Object
-
#initialize(config) ⇒ ConfigCompiler
constructor
A new instance of ConfigCompiler.
- #to_ruby ⇒ Object
Constructor Details
#initialize(config) ⇒ ConfigCompiler
Returns a new instance of ConfigCompiler.
5 6 7 |
# File 'lib/cached/config_compiler.rb', line 5 def initialize(config) @config = config end |
Instance Method Details
#compiled_fetch_method_for(index) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cached/config_compiler.rb', line 54 def compiled_fetch_method_for(index) index_name = index_name(index) cache_key = index_cache_key(index) method_suffix_and_parameters = "#{index_name}(#{index.join(', ')})" delegation = @config.delegates.collect { |delegate| "|| #{delegate}_by_#{method_suffix_and_parameters}" } "def self.lookup_by_#{method_suffix_and_parameters};" + " if key = Cached.store.read(#{cache_key});"+ " lookup(key);"+ " else;"+ " obj = nil #{delegation};" + " obj.save_indexes_to_cache if obj.respond_to?(:save_indexes_to_cache);" + " obj;" + " end;" + "end;" end |
#compiled_fetch_method_for_primary_key ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cached/config_compiler.rb', line 73 def compiled_fetch_method_for_primary_key delegation = @config.delegates.collect{|c| "|| #{c}(pk)" }.join "def self.lookup(pk);" + " Cached.store.fetch(\"#\{object_cache_prefix}:#\{pk}\") { nil #{delegation} };" + "end;" + "def self.lookup_by_#{@config.primary_key}(pk);" + " lookup(pk); "+ "end;" end |
#compiled_fetch_methods ⇒ Object
87 88 89 |
# File 'lib/cached/config_compiler.rb', line 87 def compiled_fetch_methods compiled_fetch_method_for_primary_key + @config.indexes.collect { |index| compiled_fetch_method_for(index) }.join(';') end |
#compiled_meta_methods ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cached/config_compiler.rb', line 13 def # Instance methods "def object_cache_primary_key; \"#{@config.primary_key}\"; end;" + "def object_cache_key; \"\#{object_cache_prefix}:\#{#{@config.primary_key}}\"; end;" + "def object_cache_prefix; \"#{@config.class_name}\"; end;" + "def object_cache_hash(*args); args.join.hash; end;" + # Class methods "def self.object_cache_prefix; \"#{@config.class_name}\"; end;" + "def self.object_cache_hash(*args); args.join.hash; end;" end |
#compiled_save_index_method ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cached/config_compiler.rb', line 40 def compiled_save_index_method keys = @config.indexes.collect { |index| index_cache_key(index) } "def save_indexes_to_cache;" + "v = #{@config.primary_key};" + keys.collect{|k| "Cached.store.write(#{k}, v);"}.join + "end;" + "def expire_indexes_in_cache;" + keys.collect{|k| "Cached.store.delete(#{k});"}.join + "end;" end |
#compiled_save_methods ⇒ Object
26 27 28 |
# File 'lib/cached/config_compiler.rb', line 26 def compiled_save_methods compiled_save_object_method + compiled_save_index_method end |
#compiled_save_object_method ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/cached/config_compiler.rb', line 30 def compiled_save_object_method "def save_object_to_cache;" + "Cached.store.write(object_cache_key, self);" + "end;" + "def expire_object_in_cache;" + "Cached.store.delete(object_cache_key);" + "end;" end |
#to_ruby ⇒ Object
9 10 11 |
# File 'lib/cached/config_compiler.rb', line 9 def to_ruby [, compiled_save_methods, compiled_fetch_methods].join end |