diff --git a/lib/tamashii/client.rb b/lib/tamashii/client.rb index dcf4bec..33cc520 100644 --- a/lib/tamashii/client.rb +++ b/lib/tamashii/client.rb @@ -1,3 +1,5 @@ +require "tamashii/hookable" +require "tamashii/config" require "tamashii/common" require "tamashii/client/version" require "tamashii/client/config" @@ -7,7 +9,7 @@ module Client autoload :Base, "tamashii/client/base" def self.config(&block) - return Config.class_eval(&block) if block_given? + return instance_exec(Config.instance, &block) if block_given? Config end @@ -18,3 +20,7 @@ def self.logger end end end + +Tamashii::Hook.after(:config) do |config| + config.register(:client, Tamashii::Client.config) +end diff --git a/lib/tamashii/client/config.rb b/lib/tamashii/client/config.rb index 4389218..51c8beb 100644 --- a/lib/tamashii/client/config.rb +++ b/lib/tamashii/client/config.rb @@ -1,16 +1,33 @@ require 'tamashii/common' module Tamashii module Client - class Config < Tamashii::Config - register :log_file, STDOUT + class Config + class << self + def instance + @instance ||= Config.new + end - register :use_ssl, false - register :entry_point, "" - register :host, "localhost" - register :port, 3000 - register :opening_timeout, 10 - register :opening_retry_interval, 1 - register :closing_timeout, 10 + def respond_to_missing?(name, _all = false) + super + end + + def method_missing(name, *args, &block) + return instance.send(name, *args, &block) if instance.respond_to?(name) + super + end + end + + include Tamashii::Configurable + + config :log_file, default: STDOUT + + config :use_ssl, default: false + config :entry_point, default: "" + config :host, default: "localhost" + config :port, default: 3000 + config :opening_timeout, default: 10 + config :opening_retry_interval, default: 1 + config :closing_timeout, default: 10 def log_level(level = nil) return Client.logger.level if level.nil? diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2d0e932..5fea897 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,5 +11,5 @@ require "tamashii/client" Tamashii::Client.config do - log_file Tempfile.new.path + log_file = Tempfile.new.path end diff --git a/spec/tamashii/client/base_spec.rb b/spec/tamashii/client/base_spec.rb index 23d618e..2bfa2f3 100644 --- a/spec/tamashii/client/base_spec.rb +++ b/spec/tamashii/client/base_spec.rb @@ -98,14 +98,14 @@ context "when timeout is reach" do before do - allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) + allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) end it "returns nil" do expect(subject.open_socket).to be nil end end - + context "when other error happens in the block" do before do allow(Timeout).to receive(:timeout) do @@ -290,7 +290,7 @@ def write_head_instance it "terminates the loop and call nio#close, does not call select anymore" do expect(nio).not_to receive(:select) expect(nio).to receive(:close) - subject.run + subject.run end end @@ -376,7 +376,7 @@ def write_head_instance before do allow(driver).to receive(:parse).and_raise RuntimeError end - + it "calls server gone" do expect(subject).to receive(:server_gone) subject.read @@ -389,7 +389,7 @@ def write_head_instance describe "#server_gone" do let(:socket_closed_callback) { proc { "socket closed" } } - + before do subject.instance_variable_set(:@driver, driver) subject.instance_variable_set(:@io, io) @@ -400,11 +400,13 @@ def write_head_instance end it "makes opened? become false" do + allow(subject).to receive(:open_socket_async).and_return(nil) subject.server_gone expect(subject.opened?).to be false end it "call the callback 'socket_closed'" do + allow(subject).to receive(:open_socket_async).and_return(nil) subject.on(:socket_closed, &socket_closed_callback) expect(socket_closed_callback).to receive(:call) subject.server_gone @@ -452,7 +454,7 @@ def write_head_instance describe "#wakeup" do it "call the nio#wakeup" do - expect(nio).to receive(:wakeup) + expect(nio).to receive(:wakeup) subject.wakeup end end diff --git a/tamashii-client.gemspec b/tamashii-client.gemspec index 7dbd997..92ad8ce 100644 --- a/tamashii-client.gemspec +++ b/tamashii-client.gemspec @@ -32,7 +32,8 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "websocket-driver" spec.add_runtime_dependency "nio4r" spec.add_runtime_dependency "logger-colors" - spec.add_runtime_dependency "tamashii-common" + spec.add_runtime_dependency "tamashii-config" + spec.add_runtime_dependency "tamashii-common", ">= 0.2" spec.add_runtime_dependency "concurrent-ruby" spec.add_development_dependency "bundler", "~> 1.13"