Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Demonstrating the use of const_get() and caller() to build a smarter delegation pattern.

# Let's pretend this is a lot more complicated and in some other file.
class ImapProfile
  def self.config
   {:username => "todb",
    :password => "Shadowfax" # Unguessable!
   }
  end
end

# This too.
class ImapSendUserNameCmd
  def self.data
    username = ImapProfile.config[:username]
    [username].pack("m*")
  end
end

module ActionHandler

  def my_protocol
    self.class.name.to_s.split("::").last 
  end

  def caller_action_to_constant
    caller[1] =~ /`([^']+?)'/
    $1 =~ /^do_(client|server)_(.*)/
    $2.split("_").map {|s| s.capitalize}.join
  end

  def action_executor(args={})
    Kernel.const_get(my_protocol + caller_action_to_constant + "Cmd").send :data
  end

end

module AppManager
 class Imap

    include ActionHandler

    def get_profile_params
      Kernel.const_get(my_protocol + "Profile").send :config
    end

    def do_client_send_user_name(args={})
      action_executor(args)
    end

 end
end

@app = AppManager::Imap.new