module LightIO::Library::Base::MockMethods

Attributes

mock_klass[R]

Protected Instance Methods

mock(klass) click to toggle source
# File lib/lightio/library/base.rb, line 5
def mock(klass)
  @mock_klass = klass
  define_alias_methods
  define_method_missing(singleton_class, @mock_klass)
  define_instance_method_missing(self, :@obj)
  define_mock_methods
  define_inherited
  extend_class_methods
end

Private Instance Methods

call_method_from_ancestors(method) click to toggle source
# File lib/lightio/library/base.rb, line 56
def call_method_from_ancestors(method)
  __send__(method) || begin
    self.ancestors.each do |klass|
      result = klass.__send__(method)
      break result if result
    end
  end
end
define_alias_methods() click to toggle source
# File lib/lightio/library/base.rb, line 19
def define_alias_methods
  class_methods_module = LightIO::Module.const_get("#{mock_klass}::ClassMethods") rescue nil
  return unless class_methods_module
  methods = class_methods_module.instance_methods(false).select {|method| mock_klass.respond_to?(method)}
  methods.each do |method|
    origin_method_name = "origin_#{method}"
    mock_klass.singleton_class.__send__(:alias_method, origin_method_name, method)
    mock_klass.singleton_class.__send__(:protected, origin_method_name)
  end
end
define_inherited() click to toggle source
Calls superclass method
# File lib/lightio/library/base.rb, line 65
def define_inherited
  mock_klass.define_singleton_method(:inherited) do |klass|
    super(klass)
    library_super_class = LightIO::Module::Base.find_library_class(self)
    library_klass = Class.new(library_super_class) do
      include LightIO::Library::Base
      mock klass
    end
    if klass.name
      LightIO::Library::Base.send(:full_const_set, LightIO::Library, klass.name, library_klass)
    else
      LightIO::Library::Base.send(:nameless_classes)[klass] = library_klass
    end
    klass.define_singleton_method :new do |*args, &blk|
      obj = library_klass.__send__ :allocate
      obj.__send__ :initialize, *args, &blk
      obj
    end
  end
end
define_instance_method_missing(base, target_var) click to toggle source
# File lib/lightio/library/base.rb, line 35
def define_instance_method_missing(base, target_var)
  base.send(:define_method, :method_missing) {|*args| instance_variable_get(target_var).__send__(*args)}
  base.send(:define_method, :respond_to_missing?) {|method, *| instance_variable_get(target_var).respond_to?(method)}
end
define_method_missing(base, target_var) click to toggle source
# File lib/lightio/library/base.rb, line 30
def define_method_missing(base, target_var)
  base.send(:define_method, :method_missing) {|*args| target_var.__send__(*args)}
  base.send(:define_method, :respond_to_missing?) {|method, *| target_var.respond_to?(method)}
end
define_mock_methods() click to toggle source
Calls superclass method
# File lib/lightio/library/base.rb, line 40
def define_mock_methods
  define_method :is_a? do |klass|
    mock_klass = self.class.__send__(:call_method_from_ancestors, :mock_klass)
    return super(klass) unless mock_klass
    mock_klass <= klass || super(klass)
  end

  alias_method :kind_of?, :is_a?

  define_method :instance_of? do |klass|
    mock_klass = self.class.__send__(:mock_klass)
    return super(klass) unless mock_klass
    mock_klass == klass || super(klass)
  end
end
extend_class_methods() click to toggle source
# File lib/lightio/library/base.rb, line 86
def extend_class_methods
  class_methods_module = LightIO::Module.const_get("#{mock_klass}::ClassMethods")
  self.__send__ :extend, class_methods_module
rescue NameError
  nil
end