dot_hash 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ module DotHash
2
+ class Settings
3
+ attr_reader :configs
4
+
5
+ def initialize(configs, wrapper=Properties)
6
+ @configs = Properties.new(configs)
7
+ end
8
+
9
+ def method_missing(key)
10
+ configs[key]
11
+ end
12
+
13
+ def respond_to?(key)
14
+ configs.respond_to?(key)
15
+ end
16
+
17
+ class << self
18
+ attr_reader :instance
19
+
20
+ def method_missing(key)
21
+ instance.public_send key
22
+ end
23
+
24
+ def respond_to?(key)
25
+ instance.respond_to?(key)
26
+ end
27
+
28
+ def load(*args)
29
+ @instance = new hash_from(*args)
30
+ end
31
+
32
+ private
33
+
34
+ def hash_from *args
35
+ args.inject({}) do |hash, arg|
36
+ merge_hashes hash, get_hash_from(arg)
37
+ end
38
+ end
39
+
40
+ def get_hash_from arg
41
+ if arg.is_a? Hash
42
+ arg
43
+ elsif File.file? arg
44
+ get_hash_from_file arg
45
+ elsif File.directory? arg
46
+ get_hash_from_directory arg
47
+ end
48
+ end
49
+
50
+ def get_hash_from_file(file)
51
+ return {} unless file =~ /\.(yaml|yml)/
52
+ YAML.load_file(file)
53
+ end
54
+
55
+ def get_hash_from_directory(directory)
56
+ Dir["#{directory}/**/*"].inject({}) do |hash, file|
57
+ merge_hashes hash, get_hash_from_file(file)
58
+ end
59
+ end
60
+
61
+ def merge_hashes(h1, h2)
62
+ return h1 unless h2
63
+ return h2 unless h1
64
+
65
+ h2.each do |key, value|
66
+ h1[key] = value.is_a?(Hash) ?
67
+ merge_hashes(h1[key], value) : value
68
+ end
69
+
70
+ h1
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,3 +1,3 @@
1
1
  module DotHash
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/dot_hash.rb CHANGED
@@ -2,4 +2,5 @@ $:.push File.expand_path('../', __FILE__)
2
2
 
3
3
  require 'dot_hash/properties'
4
4
  require 'dot_hash/hash_to_properties'
5
+ require 'dot_hash/settings'
5
6
  require 'dot_hash/version'
@@ -2,80 +2,80 @@ require File.expand_path("../../test_helper", __FILE__)
2
2
 
3
3
  module DotHash
4
4
  describe Properties do
5
- attr_reader :settings
5
+ attr_reader :properties
6
6
 
7
7
  describe "#method_missing" do
8
8
  describe "with a simple hash" do
9
9
  before do
10
- @settings = Properties.new speed: "15", "power" => 100
10
+ @properties = Properties.new speed: "15", "power" => 100
11
11
  end
12
12
 
13
13
  it "gets a hash property using the dot notation" do
14
- settings.speed.must_equal "15"
14
+ properties.speed.must_equal "15"
15
15
  end
16
16
 
17
17
  it "raises an error if the method is not on the hash" do
18
- -> {settings.name}.must_raise NoMethodError
18
+ -> {properties.name}.must_raise NoMethodError
19
19
  end
20
20
 
21
21
  it "gets a property from a stringed key" do
22
- settings.power.must_equal 100
22
+ properties.power.must_equal 100
23
23
  end
24
24
  end
25
25
 
26
26
  describe "with a nested hash" do
27
27
  before do
28
- @settings = Properties.new user: {
28
+ @properties = Properties.new user: {
29
29
  "info" => {name: "dude", is_admin: false}
30
30
  }
31
31
  end
32
32
 
33
33
  it "returns a DotHash instence for property with a nested hash" do
34
- settings.user.must_be_instance_of DotHash::Properties
34
+ properties.user.must_be_instance_of DotHash::Properties
35
35
  end
36
36
 
37
37
  it "gets chained properties" do
38
- settings.user.info.name.must_equal "dude"
39
- settings.user.info.is_admin.must_equal false
38
+ properties.user.info.name.must_equal "dude"
39
+ properties.user.info.is_admin.must_equal false
40
40
  end
41
41
  end
42
42
 
43
43
  describe "#[]" do
44
44
  before do
45
- @settings = Properties.new user: { name: "dude" }
45
+ @properties = Properties.new user: { name: "dude" }
46
46
  end
47
47
 
48
48
  it "accesses properties like a symbol hash" do
49
- settings[:user][:name].must_equal "dude"
49
+ properties[:user][:name].must_equal "dude"
50
50
  end
51
51
 
52
52
  it "accesses properties like a string hash" do
53
- settings["user"]["name"].must_equal "dude"
53
+ properties["user"]["name"].must_equal "dude"
54
54
  end
55
55
  end
56
56
  end
57
57
 
58
58
  describe "#respond_to?" do
59
59
  before do
60
- @settings = Properties.new speed: "15",
60
+ @properties = Properties.new speed: "15",
61
61
  info: {name: "Eden"},
62
62
  "color" => "#00FFBB"
63
63
  end
64
64
 
65
65
  it "responds to a simple property" do
66
- settings.must_respond_to :speed
66
+ properties.must_respond_to :speed
67
67
  end
68
68
 
69
69
  it "responds to a nested property" do
70
- settings.info.must_respond_to :name
70
+ properties.info.must_respond_to :name
71
71
  end
72
72
 
73
73
  it "respond to a string property" do
74
- settings.must_respond_to :color
74
+ properties.must_respond_to :color
75
75
  end
76
76
 
77
77
  it "doesn't respond to a missing property" do
78
- settings.wont_respond_to :size
78
+ properties.wont_respond_to :size
79
79
  end
80
80
  end
81
81
 
@@ -0,0 +1,110 @@
1
+ require File.expand_path("../../test_helper", __FILE__)
2
+
3
+ module DotHash
4
+ describe Settings do
5
+ attr_reader :settings
6
+
7
+ describe "#method_missing" do
8
+ before do
9
+ configs = {site: "skyo.com", facebook: {api: "123"}}
10
+ @settings = Settings.new configs
11
+ end
12
+
13
+ it "gets hash properties" do
14
+ settings.site.must_equal "skyo.com"
15
+ end
16
+
17
+ it "gets a nested hash property" do
18
+ settings.facebook.api.must_equal "123"
19
+ end
20
+ end
21
+
22
+ describe "#respond_to?" do
23
+ before do
24
+ configs = {site: "skyo.com"}
25
+ @settings = Settings.new configs
26
+ end
27
+
28
+ it "responds to a property on the configs" do
29
+ settings.must_respond_to :site
30
+ end
31
+ end
32
+
33
+ describe ".instance" do
34
+ before do
35
+ configs = {site: "skyo.com"}
36
+ Settings.load configs
37
+ end
38
+
39
+ it "returns a singleton instance of the settings" do
40
+ Settings.instance.must_be_instance_of Settings
41
+ end
42
+ end
43
+
44
+ describe ".method_missing" do
45
+ before do
46
+ configs = {site: "skyo.com"}
47
+ Settings.load configs
48
+ end
49
+
50
+ it "gets hash properties" do
51
+ Settings.site.must_equal "skyo.com"
52
+ end
53
+ end
54
+
55
+ describe ".respond_to?" do
56
+ before do
57
+ configs = {site: "skyo.com"}
58
+ Settings.load configs
59
+ end
60
+
61
+ it "responds to a property on the configs" do
62
+ Settings.must_respond_to :site
63
+ end
64
+ end
65
+
66
+ describe ".load" do
67
+ describe "loading from hashes" do
68
+ before do
69
+ Settings.load({a: 1, b: {c: 2}}, {b: {x: {z: 10}}})
70
+ end
71
+
72
+ it "adds new properties to the previous hash" do
73
+ Settings.b.x.z.must_equal 10
74
+ end
75
+
76
+ it "keeps non-changed properties untouched" do
77
+ Settings.a.must_equal 1
78
+ end
79
+
80
+ it "merges nested hashes correctly" do
81
+ Settings.b.c.must_equal 2
82
+ end
83
+ end
84
+
85
+ describe "loading from files" do
86
+ it "loads from a file" do
87
+ Settings.load fixtures_path("configs1.yaml")
88
+ Settings.rogue.speed.must_equal 20
89
+ Settings.rogue.power.must_equal 11
90
+ end
91
+
92
+ it "loads from two files" do
93
+ Settings.load(
94
+ fixtures_path("configs1.yaml"),
95
+ fixtures_path("configs2.yaml")
96
+ )
97
+
98
+ Settings.rogue.speed.must_equal 25
99
+ Settings.rogue.power.must_equal 11
100
+ end
101
+
102
+ it "loads from a directory" do
103
+ Settings.load fixtures_path
104
+ Settings.rogue.speed.must_equal 25
105
+ Settings.rogue.power.must_equal 11
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,7 @@
1
+ default: &default
2
+ speed: 10
3
+ power: 11
4
+
5
+ rogue:
6
+ <<: *default
7
+ speed: 20
@@ -0,0 +1,2 @@
1
+ rogue:
2
+ speed: 25
data/test/test_helper.rb CHANGED
@@ -1,2 +1,7 @@
1
1
  require "minitest/autorun"
2
2
  require File.expand_path("../../lib/dot_hash", __FILE__)
3
+ TESTS_PATH= File.dirname(__FILE__)
4
+
5
+ def fixtures_path file=nil
6
+ "#{TESTS_PATH}/fixtures/#{file}"
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dot_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-22 00:00:00.000000000 Z
12
+ date: 2013-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -44,9 +44,13 @@ files:
44
44
  - lib/dot_hash.rb
45
45
  - lib/dot_hash/hash_to_properties.rb
46
46
  - lib/dot_hash/properties.rb
47
+ - lib/dot_hash/settings.rb
47
48
  - lib/dot_hash/version.rb
48
49
  - test/dot_hash/hash_to_properties_test.rb
49
50
  - test/dot_hash/properties_test.rb
51
+ - test/dot_hash/settings_test.rb
52
+ - test/fixtures/configs1.yaml
53
+ - test/fixtures/configs2.yaml
50
54
  - test/test_helper.rb
51
55
  homepage: https://github.com/3den/dot_hash
52
56
  licenses: []
@@ -62,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
66
  version: '0'
63
67
  segments:
64
68
  - 0
65
- hash: -937726017345117898
69
+ hash: -4301238719098485564
66
70
  required_rubygems_version: !ruby/object:Gem::Requirement
67
71
  none: false
68
72
  requirements:
@@ -71,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
75
  version: '0'
72
76
  segments:
73
77
  - 0
74
- hash: -937726017345117898
78
+ hash: -4301238719098485564
75
79
  requirements: []
76
80
  rubyforge_project:
77
81
  rubygems_version: 1.8.24
@@ -81,4 +85,7 @@ summary: Converts hash.to_properties so you can access values using properties.s
81
85
  test_files:
82
86
  - test/dot_hash/hash_to_properties_test.rb
83
87
  - test/dot_hash/properties_test.rb
88
+ - test/dot_hash/settings_test.rb
89
+ - test/fixtures/configs1.yaml
90
+ - test/fixtures/configs2.yaml
84
91
  - test/test_helper.rb