Created
March 12, 2012 16:50
-
-
Save roolo/2023292 to your computer and use it in GitHub Desktop.
Hash merging
This file contains hidden or 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
| [base] | |
| data_working_dir = "/tmp" |
This file contains hidden or 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
| [base] | |
| data_working_dir = "/root" |
This file contains hidden or 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
| class MyClass | |
| @instance = nil | |
| @config = nil | |
| @APP_ROOT = nil | |
| def initialize | |
| @APP_ROOT = File.expand_path __FILE__ + '/../../../' | |
| end | |
| def self.get_instance | |
| @instance = MyClass.new unless @instance | |
| @instance | |
| end | |
| def config custom_config_path = nil | |
| unless @config | |
| ini_file_config = {:escape => false} | |
| default_ini = IniFile.new( (@APP_ROOT + "/config/default.ini"), ini_file_config ) | |
| if custom_config_path | |
| custom_ini = IniFile.new( (custom_config_path), ini_file_config) | |
| else | |
| custom_ini = IniFile.new( (@APP_ROOT + "/config/local.ini"), ini_file_config ) | |
| end | |
| @config = default_ini.to_h.merge custom_ini.to_h | |
| end | |
| @config | |
| end | |
| def self.clean_ini_value ini_value | |
| ini_value.match(/[\/\w]+/).to_a.last | |
| end | |
| end |
This file contains hidden or 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
| require File.expand_path(__FILE__ + "/../../spec_helper.rb") | |
| describe "MyClass" do | |
| it "overload configuration by custom_config" do | |
| testing_ini_path = File.expand_path( | |
| File.dirname(__FILE__) + '/../fixtures/local.ini' | |
| ) | |
| app_data_wd = MyClass .get_instance \ | |
| .config(testing_ini_path) \ | |
| ['base']['data_working_dir'] | |
| MyClass.clean_ini_value(app_data_wd).should eq '/root' | |
| end | |
| end |
This file contains hidden or 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
| # Terminal | |
| expected: "/root" | |
| got: "/tmp" | |
| # RubyMine | |
| 1 example, 0 failures, 1 passed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment