Class: EventMachine::Protocols::RedisMethodFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/joffice_redis/redis_method_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent) {|_self| ... } ⇒ RedisMethodFactory

Returns a new instance of RedisMethodFactory.

Yields:

  • (_self)

Yield Parameters:



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

#generateObject



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

Yields:

  • (buf)


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, options={})      
  em_name="#{name}_real"
  name_sync="#{name}_sync"
  name_async="#{name}_async"        
  @buffer << "alias :#{em_name} :#{name}"
  args = (method_call_args[options[:args_count].to_i || 1] || '*args')
  
  method(name_sync, args) do |m|
    m << "return [] if blank?(args)" if (options[:args_count] || 1)>3
    m << "c=Fiber.current"
    m << "#{em_name}(#{args}) { |a|  c.resume(a); }"
    m << "Fiber.yield"
  end
  
  if options[: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 (options[:args_count] || 1)>3
    m << "#{em_name}(#{args}) { |a| }"
  end
  
  @buffer << "alias :#{name}! :#{name_async}"
  @buffer << "alias :#{name} :#{options[:sync] ? name_sync : name_async}"
  
  if options[:alias]
    options[: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, options={})
  name_sync="#{name}_sync"
  name_async="#{name}_async"        
  
  args = (method_call_args[options[:args_count] || 1] || '*args')       
  
  call_args = case (options[: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 (options[: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 (options[:args_count] || 1)>3
    m << "call_command(#{call_args}) { |a| }"
  end
  
  @buffer << "alias :#{name}! :#{name_async}"
  @buffer << "alias :#{name} :#{options[:sync] ? name_sync : name_async}"
  
  if options[:alias]
    options[:alias].each do |alias_name|
      @buffer << "alias :#{alias_name} :#{name}"
    end
  end
end

#method_call_argsObject



55
56
57
# File 'lib/joffice_redis/redis_method_factory.rb', line 55

def method_call_args
  @method_call_args||=['', 'key', 'key, value']
end