Class: EventMachine::Protocols::RedisMethodFactory
- Inherits:
-
Object
- Object
- EventMachine::Protocols::RedisMethodFactory
- Defined in:
- lib/joffice_redis/redis_method_factory.rb
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(parent) {|_self| ... } ⇒ RedisMethodFactory
constructor
A new instance of RedisMethodFactory.
- #method(name, args = nil) {|buf| ... } ⇒ Object
- #method_alias(name, options = {}) ⇒ Object
- #method_call(name, options = {}) ⇒ Object
- #method_call_args ⇒ Object
Constructor Details
#initialize(parent) {|_self| ... } ⇒ RedisMethodFactory
Returns a new instance of RedisMethodFactory.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/joffice_redis/redis_method_factory.rb', line 26 def initialize(parent) @class_name='syncronized_redis_protocol' #@buffer=['', "require 'em-redis'","require 'active_support'",'module EventMachine', 'module Protocols', 'module Redis'] @buffer=[''] yield self if block_given? method 'ping' do |m| m << 'exists?(0)' end method('blank?', 'args') do |m| m << "args.blank? || args.flatten!.blank?" end #@buffer << 'end' #@buffer << 'end' #@buffer << 'end' #generate parent.class_eval @buffer.join("\n") end |
Instance Method Details
#generate ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/joffice_redis/redis_method_factory.rb', line 130 def generate dir=File.join(File.absolute_path(File.dirname(__FILE__)),'metadata') FileUtils.mkdir_p(dir) path=File.join(dir, "#{@class_name}.rb") f=File.open(path,'w') f.write(@buffer.join("\n")) f.close #require path end |
#method(name, args = nil) {|buf| ... } ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/joffice_redis/redis_method_factory.rb', line 44 def method(name, args=nil) buf=[] yield buf buf=buf.map{|v| " #{v}"}.join("\n") signature=(args ? "#{name}(#{args})" : name) @buffer << '' @buffer << "def #{signature}\n#{buf}\nend" @buffer << "public :#{name}" @buffer << '' end |
#method_alias(name, options = {}) ⇒ Object
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 |
# File 'lib/joffice_redis/redis_method_factory.rb', line 94 def method_alias(name, ={}) em_name="#{name}_real" name_sync="#{name}_sync" name_async="#{name}_async" @buffer << "alias :#{em_name} :#{name}" args = (method_call_args[[:args_count].to_i || 1] || '*args') method(name_sync, args) do |m| m << "return [] if blank?(args)" if ([:args_count] || 1)>3 m << "c=Fiber.current" m << "#{em_name}(#{args}) { |a| c.resume(a); }" m << "Fiber.yield" end if [:cache] @buffer << "alias :#{name_sync}_no_cache :#{name_async}" method(name_sync, args) do |m| m << "@cache_#{name}||=#{name_sync}_no_cache(#{args})" end end method(name_async, args) do |m| m << "return if blank?(args)" if ([:args_count] || 1)>3 m << "#{em_name}(#{args}) { |a| }" end @buffer << "alias :#{name}! :#{name_async}" @buffer << "alias :#{name} :#{[:sync] ? name_sync : name_async}" if [:alias] [:alias].each do |alias_name| @buffer << "alias :#{alias_name} :#{name}" end end end |
#method_call(name, options = {}) ⇒ Object
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 |
# File 'lib/joffice_redis/redis_method_factory.rb', line 59 def method_call(name, ={}) name_sync="#{name}_sync" name_async="#{name}_async" args = (method_call_args[[:args_count] || 1] || '*args') call_args = case ([:args_count] || 1) when 2 then "[:#{name}, key, value]" when 1 then "[:#{name}, key]" when 0 then "[:#{name}]" else "args.unshift(:#{name})" end method(name_sync, args) do |m| m << "return [] if blank?(args)" if ([:args_count] || 1)>3 m << "c=Fiber.current" m << "call_command(#{call_args}) { |a| c.resume(a); }" m << "Fiber.yield" end method(name_async, args) do |m| m << "return if blank?(args)" if ([:args_count] || 1)>3 m << "call_command(#{call_args}) { |a| }" end @buffer << "alias :#{name}! :#{name_async}" @buffer << "alias :#{name} :#{[:sync] ? name_sync : name_async}" if [:alias] [:alias].each do |alias_name| @buffer << "alias :#{alias_name} :#{name}" end end end |
#method_call_args ⇒ Object
55 56 57 |
# File 'lib/joffice_redis/redis_method_factory.rb', line 55 def method_call_args @method_call_args||=['', 'key', 'key, value'] end |