Skip to content

Commit

Permalink
Config: 設定IDを記録できるようにする
Browse files Browse the repository at this point in the history
refs #132
  • Loading branch information
ochaochaocha3 committed Jul 16, 2019
1 parent 30614f8 commit e2f6b45
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
9 changes: 7 additions & 2 deletions lib/rgrb/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
module RGRB
# RGRB の設定を表すクラス
class Config
# 設定 ID
# @return [String]
attr_reader :id
# IRC ボットの設定のハッシュ
# @return [Hash]
attr_reader :irc_bot
Expand Down Expand Up @@ -45,14 +48,16 @@ def load_yaml_file(config_id, root_path)
config_data.merge!(child_config_data)
end

new(config_data)
new(config_id, config_data)
end
end

# 新しい RGRB::Config インスタンスを返す
# @param [String] config_id 設定 ID
# @param [Hash] config_data 設定データのハッシュ
# @return [RGRB::Config]
def initialize(config_data)
def initialize(config_id, config_data)
@id = config_id
@irc_bot = config_data['IRCBot']
@discord_bot = config_data['DiscordBot']
@plugins = config_data['Plugins'] || []
Expand Down
17 changes: 15 additions & 2 deletions spec/rgrb/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@
}
end

let(:config_id) { 'rgrb' }
let(:config) do
described_class.new(config_data)
described_class.new(config_id, config_data)
end

describe '#id' do
it '指定されたIDと等しい' do
expect(config.id).to eq(config_id)
end
end

describe '#irc_bot' do
Expand Down Expand Up @@ -81,7 +88,13 @@
describe '.#load_yaml_file' do
let(:config_from_file) do
root_path = File.expand_path('config_data', File.dirname(__FILE__))
described_class.load_yaml_file('rgrb', root_path)
described_class.load_yaml_file(config_id, root_path)
end

describe '#id' do
it '指定されたIDと等しい' do
expect(config_from_file.id).to eq(config_id)
end
end

describe '#irc_bot' do
Expand Down
20 changes: 17 additions & 3 deletions spec/rgrb/plugins_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ module RGRB::Plugin; end

describe RGRB::PluginsLoader do
let(:empty_config) do
RGRB::Config.new('IRCBot' => {})
config_data = {
'IRCBot' => {}
}

RGRB::Config.new('empty_config', config_data)
end
let(:empty_plugins_loader) { described_class.new(empty_config) }

Expand All @@ -26,12 +30,22 @@ module RGRB::Plugin; end
end
end
let(:rgrb_config) do
RGRB::Config.new('IRCBot' => {}, 'Plugins' => plugin_names)
config_data = {
'IRCBot' => {},
'Plugins' => plugin_names
}

RGRB::Config.new('rgrb_config', config_data)
end
let(:plugins_loader) { described_class.new(rgrb_config) }

let(:wrong_config) do
RGRB::Config.new('IRCBot' => {}, 'Plugins' => ['MissingPlugin'])
config_data = {
'IRCBot' => {},
'Plugins' => ['MissingPlugin']
}

RGRB::Config.new('wrong_config', config_data)
end
let(:wrong_plugins_loader) do
described_class.new(wrong_config)
Expand Down

0 comments on commit e2f6b45

Please sign in to comment.