class Rmodel::Injector

Public Class Methods

new(repository_class) click to toggle source
# File lib/rmodel/injector.rb, line 5
def initialize(repository_class)
  @repository = LazyObject.new { repository_class.new }
end

Public Instance Methods

extended(base) click to toggle source
# File lib/rmodel/injector.rb, line 13
def extended(base)
  included(base)
end
included(base) click to toggle source
# File lib/rmodel/injector.rb, line 9
def included(base)
  add_method_missing(base)
end

Private Instance Methods

add_method_missing(base) click to toggle source
Calls superclass method
# File lib/rmodel/injector.rb, line 19
def add_method_missing(base)
  repository = @repository
  injector = self

  base.define_singleton_method :method_missing do |name, *args|
    if repository.respond_to?(name)
      injector.send(:delegate_to_repository, self, name, *args)
    else
      super(name, *args)
    end
  end
end
delegate_to_repository(base, name, *delegated_args) click to toggle source
# File lib/rmodel/injector.rb, line 32
def delegate_to_repository(base, name, *delegated_args)
  repository = @repository

  base.define_singleton_method name do |*args|
    repository.public_send(name, *args)
  end

  base.public_send(name, *delegated_args)
end