Skip to content

add after_initialize hooks #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/iruby/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ class Kernel

class<< self
attr_accessor :instance

def after_initialize_hooks
@after_initialize_hooks ||= []
end

def after_initialize(&block)
after_initialize_hooks << block
end

def run_after_initialize_hooks
after_initialize_hooks.each(&:call)
end
end

attr_reader :session
Expand All @@ -23,6 +35,8 @@ def initialize(config_file)
@execution_count = 0
@backend = create_backend
@running = true

Kernel.run_after_initialize_hooks
end

def create_backend
Expand Down
29 changes: 29 additions & 0 deletions test/iruby/kernel_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'test_helper'
require 'json'

module IRubyTest
class KernelTest < TestBase
def test_after_intialize_hooks
ran_hook = false
IRuby::Kernel.after_initialize do
ran_hook = true
end

# may be better to put this in test/test_helper.rb
IRuby.logger ||= Logger.new(nil)

IRuby::Kernel.new(config_file.path)

assert ran_hook
ensure
IRuby::Kernel.after_initialize_hooks.clear
end

def config_file
config_file = Tempfile.new
config_file.write({key: ""}.to_json)
config_file.flush
config_file
end
end
end