dot_hash 0.3.2 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ module DotHash
2
+ class HashLoader
3
+ attr_reader :hashes
4
+
5
+ def initialize *hashes
6
+ @hashes = hashes
7
+ end
8
+
9
+ def load
10
+ hashes.inject({}) do |hash, arg|
11
+ merge_hashes hash, get_hash_from(arg)
12
+ end
13
+ end
14
+
15
+ def merge_hashes(h1, h2)
16
+ return h1 unless h2
17
+ return h2 unless h1
18
+
19
+ h2.each do |key, value|
20
+ h1[key] = value.is_a?(Hash) ?
21
+ merge_hashes(h1[key], value) : value
22
+ end
23
+
24
+ h1
25
+ end
26
+
27
+ private
28
+
29
+ def get_hash_from arg
30
+ if arg.is_a? Hash
31
+ arg
32
+ elsif File.file? arg
33
+ get_hash_from_file arg
34
+ elsif File.directory? arg
35
+ get_hash_from_directory arg
36
+ end
37
+ end
38
+
39
+ def get_hash_from_file(file)
40
+ return {} unless file =~ /\.(yaml|yml)$/
41
+ YAML.load_file(file)
42
+ end
43
+
44
+ def get_hash_from_directory(directory)
45
+ Dir["#{directory}/**/*"].inject({}) do |hash, file|
46
+ merge_hashes hash, get_hash_from_file(file)
47
+ end
48
+ end
49
+ end
50
+ end
51
+
@@ -8,15 +8,21 @@ module DotHash
8
8
  end
9
9
 
10
10
  def method_missing(key, *args, &block)
11
- has_key?(key) ?
12
- execute(key, *args, &block) :
11
+ if has_key?(key)
12
+ execute(key, *args, &block)
13
+ else
13
14
  super(key, *args, &block)
15
+ end
14
16
  end
15
17
 
16
18
  def respond_to?(key)
17
19
  has_key?(key) or super(key)
18
20
  end
19
21
 
22
+ def to_s
23
+ hash.to_s
24
+ end
25
+
20
26
  def [](key)
21
27
  get_value(key)
22
28
  end
@@ -22,52 +22,11 @@ module DotHash
22
22
  end
23
23
 
24
24
  def respond_to?(key)
25
- instance.respond_to?(key)
25
+ super(key) || instance.respond_to?(key)
26
26
  end
27
27
 
28
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
29
+ @instance = new HashLoader.new(*args).load
71
30
  end
72
31
  end
73
32
  end
@@ -1,3 +1,3 @@
1
1
  module DotHash
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.4"
3
3
  end
data/lib/dot_hash.rb CHANGED
@@ -3,5 +3,6 @@ $:.push File.expand_path('../', __FILE__)
3
3
  require 'yaml'
4
4
  require 'dot_hash/properties'
5
5
  require 'dot_hash/hash_to_properties'
6
+ require 'dot_hash/hash_loader'
6
7
  require 'dot_hash/settings'
7
8
  require 'dot_hash/version'
@@ -0,0 +1,53 @@
1
+ require_relative "../test_helper"
2
+
3
+ module DotHash
4
+ describe HashLoader do
5
+
6
+ describe "#load" do
7
+ it "merges the given hashes" do
8
+ loader = HashLoader.new(
9
+ {a: 1, b: {c: 2}},
10
+ {b: {x: {z: 10}}}
11
+ )
12
+ loader.load.must_equal({a: 1, b: {c: 2, x: {z: 10}}})
13
+ end
14
+
15
+ it "loads from a file" do
16
+ loader = HashLoader.new fixtures_path("configs1.yaml")
17
+
18
+ loader.load.must_equal({
19
+ "default" => {"attr" => {"speed"=>10, "power"=>11}},
20
+ "rogue" => {"attr" => {"speed"=>20, "power"=>11}}
21
+ })
22
+ end
23
+
24
+ it "loads from two files" do
25
+ loader = HashLoader.new(
26
+ fixtures_path("configs1.yaml"),
27
+ fixtures_path("configs2.yaml")
28
+ )
29
+
30
+ loader.load.must_equal({
31
+ "default"=>{"attr"=>{"speed"=>10, "power"=>11}},
32
+ "rogue"=>{"attr"=>{"speed"=>25, "power"=>11}}
33
+ })
34
+ end
35
+
36
+ it "does not load files without the yaml extension" do
37
+ loader = HashLoader.new fixtures_path("config.yaml.example")
38
+
39
+ loader.load.must_equal({})
40
+ end
41
+
42
+ it "loads from a directory" do
43
+ loader = HashLoader.new fixtures_path
44
+
45
+ loader.load.must_equal({
46
+ "default"=>{"attr"=>{"speed"=>10, "power"=>11}},
47
+ "rogue"=>{"attr"=>{"speed"=>25, "power"=>11}}
48
+ })
49
+ end
50
+ end
51
+ end
52
+ end
53
+
@@ -1,4 +1,4 @@
1
- require File.expand_path("../../test_helper", __FILE__)
1
+ require_relative "../test_helper"
2
2
 
3
3
  describe Hash do
4
4
  attr_reader :properties
@@ -1,4 +1,4 @@
1
- require File.expand_path("../../test_helper", __FILE__)
1
+ require_relative "../test_helper"
2
2
 
3
3
  module DotHash
4
4
  describe Properties do
@@ -15,7 +15,9 @@ module DotHash
15
15
  end
16
16
 
17
17
  it "raises an error if the method is not on the hash" do
18
- -> {properties.name}.must_raise NoMethodError
18
+ -> {
19
+ properties.name
20
+ }.must_raise NoMethodError, "undefined method `name' for {:speed=>\"15\", \"power\"=>100}:DotHash::Properties"
19
21
  end
20
22
 
21
23
  it "gets a property from a stringed key" do
@@ -75,6 +77,19 @@ module DotHash
75
77
  end
76
78
  end
77
79
 
80
+ describe "#to_s" do
81
+ attr_reader :hash
82
+
83
+ before do
84
+ @hash = { user: {name: "dude"} }
85
+ @properties = Properties.new @hash
86
+ end
87
+
88
+ it "returns the hash as a string" do
89
+ properties.to_s.must_equal hash.to_s
90
+ end
91
+ end
92
+
78
93
  describe "#respond_to?" do
79
94
  before do
80
95
  @properties = Properties.new speed: "15",
@@ -1,4 +1,4 @@
1
- require File.expand_path("../../test_helper", __FILE__)
1
+ require_relative "../test_helper"
2
2
 
3
3
  module DotHash
4
4
  describe Settings do
data/test/test_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "minitest/autorun"
2
- require File.expand_path("../../lib/dot_hash", __FILE__)
2
+ require_relative "../lib/dot_hash"
3
+
3
4
  TESTS_PATH= File.dirname(__FILE__)
4
5
 
5
6
  def fixtures_path file=nil
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.3.2
4
+ version: 0.3.4
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-02-28 00:00:00.000000000 Z
12
+ date: 2013-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -42,10 +42,12 @@ files:
42
42
  - Rakefile
43
43
  - dot_hash.gemspec
44
44
  - lib/dot_hash.rb
45
+ - lib/dot_hash/hash_loader.rb
45
46
  - lib/dot_hash/hash_to_properties.rb
46
47
  - lib/dot_hash/properties.rb
47
48
  - lib/dot_hash/settings.rb
48
49
  - lib/dot_hash/version.rb
50
+ - test/dot_hash/hash_loader_test.rb
49
51
  - test/dot_hash/hash_to_properties_test.rb
50
52
  - test/dot_hash/properties_test.rb
51
53
  - test/dot_hash/settings_test.rb
@@ -67,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
69
  version: '0'
68
70
  segments:
69
71
  - 0
70
- hash: -2180397336244030562
72
+ hash: -817138279000566861
71
73
  required_rubygems_version: !ruby/object:Gem::Requirement
72
74
  none: false
73
75
  requirements:
@@ -76,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
78
  version: '0'
77
79
  segments:
78
80
  - 0
79
- hash: -2180397336244030562
81
+ hash: -817138279000566861
80
82
  requirements: []
81
83
  rubyforge_project:
82
84
  rubygems_version: 1.8.24
@@ -84,6 +86,7 @@ signing_key:
84
86
  specification_version: 3
85
87
  summary: Converts hash.to_properties so you can access values using properties.some_key
86
88
  test_files:
89
+ - test/dot_hash/hash_loader_test.rb
87
90
  - test/dot_hash/hash_to_properties_test.rb
88
91
  - test/dot_hash/properties_test.rb
89
92
  - test/dot_hash/settings_test.rb