Mam taki problem:
module Harvester
class Device
attr_reader :ip, :mac
include Telnet
include SNMP
def initialize(ip:nil, mac:nil)
if ip
@ip = ip.is_a?(Net::IP::Address) ? ip : Net::IP::Address.new(ip)
else
@ip = nil
end
if mac
@mac = mac.is_a?(Net::Mac::Address) ? mac : Net::Mac::Address.new(mac)
else
@mac = nil
end
end
def ip=(ip)
@ip = ip.is_a?(Net::IP::Address) ? ip : Net::IP::Address.new(ip)
end
def mac=(mac)
@mac = mac.is_a?(Net::Mac::Address) ? mac : Net::Mac::Address.new(mac)
end
end
end
module Harvester
module Telnet
def self.included(base)
base.extend ClassMethods
base.overwrite_initialize
base.instance_eval do
def method_added(name)
return if name != :initialize
overwrite_initialize
end
end
end
def mod_initialize
puts "Prompt - #{@__prompt}"
__connect @__prompt
end
def __connect(prompt)
puts prompt
@__telnet_connection = Net::Telnet::new('Host' => @ip, 'Prompt' => prompt)
end
module ClassMethods
def overwrite_initialize
class_eval do
unless method_defined?(:custom_initialize)
define_method(:custom_initialize) do |*args|
mod_initialize
original_initialize(*args)
end
end
if instance_method(:initialize) != instance_method(:custom_initialize)
alias_method :original_initialize, :initialize
alias_method :initialize, :custom_initialize
end
end
end
def prompt(prompt)
@__prompt = prompt
end
end
end
end
module Harvester
class Device
class Modem < self
prompt '>'
end
end
end
Oczywiści to nie działa, ale chciałbym móc za pomocą metody ‘prompt’ ustawiać sobie zmienną @__prompt potrzebną do połączenia Telnet, lub podobny efekt pozwalający mi modyfikować prompt dla połączenia Telnet.