Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
138 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
require 'yaml' | ||
|
||
module Micon | ||
end | ||
|
||
%w{ | ||
support | ||
|
||
metadata | ||
config | ||
core | ||
helper | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Micon::Config | ||
attr_reader :micon, :name | ||
def initialize micon, name | ||
@micon, @name = micon, name | ||
end | ||
|
||
def load | ||
files = [] | ||
files << find_file(config_path(name, nil), $LOAD_PATH) | ||
files << find_file(config_path(name, micon.mode_name), $LOAD_PATH) if micon.mode_name | ||
if micon.runtime_path | ||
files << find_file(runtime_config_path(name, nil), [micon.runtime_path]) | ||
files << find_file(runtime_config_path(name, micon.mode_name), [micon.runtime_path]) if micon.mode_name | ||
end | ||
|
||
config = {} | ||
files.compact.each do |f| | ||
c = YAML.load_file(f) | ||
next unless c | ||
raise "component config must be a Hash (#{f})!" unless c.is_a? Hash | ||
c.each{|k, v| config[:"#{k}="] = v} | ||
end | ||
|
||
config.empty? ? nil : config | ||
end | ||
|
||
protected | ||
def config_path name, mode | ||
fs_name = name.to_s.gsub(/::/, '/') | ||
mode ? "/components/#{fs_name}.#{mode}.yml" : "/components/#{fs_name}.yml" | ||
end | ||
|
||
def runtime_config_path name, mode | ||
fs_name = name.to_s.gsub(/::/, '/') | ||
mode ? "/config/#{fs_name}.#{mode}.yml" : "/config/#{fs_name}.yml" | ||
end | ||
|
||
def find_file path, directories | ||
files = directories.collect{|dir| "#{dir}#{path}"}.select{|f| File.exist? f} | ||
raise "multiple configs for :#{name} component" if files.size > 1 | ||
files.first | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
def p; end | ||
require 'spec_helper' | ||
|
||
describe "Configuration" do | ||
before{self.micon = Micon::Core.new} | ||
after{micon.runtime_path = nil} | ||
|
||
it "should configure component if config provided" do | ||
micon.register(:logger){OpenStruct.new} | ||
with_load_path "#{spec_dir}/basic/lib" do | ||
micon[:logger].level.should == :info | ||
end | ||
end | ||
|
||
it "should merge in order: conf <- conf.mode <- runtime <- runtime.mode" do | ||
micon.register(:object){OpenStruct.new} | ||
with_load_path "#{spec_dir}/order/lib" do | ||
micon.runtime_path = "#{spec_dir}/order/app" | ||
micon.mode_name = :production | ||
micon[:object].tap do |o| | ||
o.a.should == 'object.production.yml' | ||
o.b.should == 'runtime.object.yml' | ||
o.c.should == 'runtime.object.production.yml' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
level: :info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
c: 'runtime.object.production.yml' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
b: 'runtime.object.yml' | ||
c: 'runtime.object.yml' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a: 'object.production.yml' | ||
b: 'object.production.yml' | ||
c: 'object.production.yml' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a: 'object.yml' | ||
b: 'object.yml' | ||
c: 'object.yml' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters