dot_hash 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39b7417393fe9bedfd168b25cbef8acab0610fc0
4
- data.tar.gz: fff127d3af33e47e58ef51e8493ad1c1521b7f57
3
+ metadata.gz: 7018ca020ba869170194582fc6d71ec4cddbc187
4
+ data.tar.gz: e864953ad473baac57823386d63892028a24db4e
5
5
  SHA512:
6
- metadata.gz: 99110126469ff816221d67b4bd34a426c9544206584c528ee6947c778574cb66b3516cf65772c222a1f0df19852b7e319438631754f701a29a975ea450a2a37b
7
- data.tar.gz: 46f93563bfeea0c1966a813c344581a0c7afc4e40692d811c77c71623b39e02a0e3116c5e244dd3af05ffba16b5bd1302072e9dd67da1f4d5ae9614eb88bba3a
6
+ metadata.gz: 288f017493329d2e3fba220f8eeaefba7720f36e1aa9087055a0a939c712d4660fde84c9b2ab9e712c842d4e5dbd330a5a3a1a7dae21e834697989601f29a328
7
+ data.tar.gz: 1a1f2c30f1ab511ffd5b08408c1f6c61812cb26f21a897d888ee396c03dfc1edc0223d960fde56f63dc93b4b8889ccc2ebb5cca9a3f55361ea7c9ed1939481bd
@@ -0,0 +1,7 @@
1
+ build
2
+ clean
3
+ clobber
4
+ install
5
+ install:local
6
+ release[remote]
7
+ test
data/README.md CHANGED
@@ -20,18 +20,28 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ You can convert and hash to DotHash's properties.
24
+
23
25
  ```ruby
24
26
  some_hash = {size: {height: 100, width: 500}, "color" => "red"}
25
- properties = some_hash.to_properties
27
+ properties = DotHash.load(some_hash)
26
28
 
27
29
  properties.size.height # returns 100, it is the same as some_hash[:size][:height]
28
30
  properties.color # returns "red", it works with Strings and Symbol keys
29
31
  properties[:color] # returns "red", can be used like a hash with string keys
30
32
  properties["color"] # returns "red", can be used like a hash with symbol keys
33
+ ```
34
+
35
+ You can use DotHash::Settings to manage all configs of your app it can load yml, json with or without ERB code embeded.
31
36
 
37
+ ```ruby
32
38
  # App Settings
33
39
  class Settings < DotHash::Settings
34
- load 'path/to/some/settings.yaml', 'path/to/settings-directory/', {something: 'Some Value'}
40
+ load(
41
+ 'path/to/some/settings.json',
42
+ 'path/to/settings-directory/',
43
+ {something: 'Some Value'}
44
+ )
35
45
  end
36
46
 
37
47
  # Use the settings as a Singleton
@@ -40,10 +50,23 @@ Settings.other.stuff.from_yml_settings
40
50
 
41
51
  # Create a settings instance from some YML
42
52
  swagger = Settings.new Rails.root.join('config', 'my-swagger.yml')
43
-
44
53
  swagger.info.title # returns the title from swagger doc
45
54
  ```
46
55
 
56
+ DotHash supports Rails and is very easy to manage fancy settings with it.
57
+
58
+ ```ruby
59
+ class Settings < DotHash::Settings
60
+ load(
61
+ Rails.root.join('config', 'settings.yml'), # loads config/settings.yml
62
+ Rails.root.join('package.json'), # loads package.json
63
+ *Dir(Rails.root.join('config', 'settings', '*.yml')), # loads all config/settings/*.yml but dont go to nested directories
64
+ Rails.root.join('config', 'settings', Rails.env), # loads all files on config/settings/<env>/
65
+ Rails.root.join('config', 'settings.local.yml') # loads config/settings.local.yml
66
+ )
67
+ end
68
+ ```
69
+
47
70
  Check the tests for more details.
48
71
 
49
72
  ## Contributing
@@ -4,7 +4,14 @@ require 'yaml'
4
4
  require 'json'
5
5
  require 'erb'
6
6
  require 'dot_hash/properties'
7
- require 'dot_hash/hash_to_properties'
8
7
  require 'dot_hash/loader'
9
8
  require 'dot_hash/settings'
10
9
  require 'dot_hash/version'
10
+
11
+ module DotHash
12
+ class << self
13
+ def load(*args)
14
+ Loader.new(*args).properties
15
+ end
16
+ end
17
+ end
@@ -6,12 +6,16 @@ module DotHash
6
6
  @hashes = hashes
7
7
  end
8
8
 
9
- def load
9
+ def hash
10
10
  hashes.inject({}) do |hash, arg|
11
11
  merge_hashes hash, get_hash_from(arg)
12
12
  end
13
13
  end
14
14
 
15
+ def properties
16
+ Properties.new hash
17
+ end
18
+
15
19
  def merge_hashes(h1, h2)
16
20
  return h1 unless h2
17
21
  return h2 unless h1
@@ -29,7 +33,7 @@ module DotHash
29
33
  def get_hash_from arg
30
34
  if arg.is_a? Hash
31
35
  arg
32
- elsif File.file? arg
36
+ elsif File.file? arg.to_s
33
37
  get_hash_from_file arg
34
38
  elsif File.directory? arg
35
39
  get_hash_from_directory arg
@@ -37,8 +41,14 @@ module DotHash
37
41
  end
38
42
 
39
43
  def get_hash_from_file(file)
40
- return {} unless file =~ /\.(yaml|yml)(\.erb)?$/
41
- YAML.load load_erb(file)
44
+ case file
45
+ when /\.ya?ml(\.erb)?$/
46
+ YAML.load load_erb(file)
47
+ when /\.json(\.erb)?$/
48
+ JSON.parse load_erb(file)
49
+ else
50
+ {}
51
+ end
42
52
  end
43
53
 
44
54
  def load_erb(file)
@@ -1,12 +1,14 @@
1
1
  module DotHash
2
2
  class Settings < Properties
3
3
  def initialize(*args)
4
- super Loader.new(*args).load
4
+ super Loader.new(*args).hash
5
5
  end
6
6
 
7
- class << self
8
- attr_reader :instance
7
+ def load(*args)
8
+ @hash = Loader.new(hash, *args).hash
9
+ end
9
10
 
11
+ class << self
10
12
  def method_missing(*args, &block)
11
13
  instance.public_send(*args, &block)
12
14
  end
@@ -16,11 +18,13 @@ module DotHash
16
18
  end
17
19
 
18
20
  def load(*args)
19
- @instance = new(*args)
21
+ instance.load(*args)
20
22
  end
21
23
 
22
- def namespace(namespace)
23
- @instance = @instance[namespace]
24
+ private
25
+
26
+ def instance
27
+ @instance ||= new({})
24
28
  end
25
29
  end
26
30
  end
@@ -1,3 +1,3 @@
1
1
  module DotHash
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,24 +1,19 @@
1
1
  require_relative "../test_helper"
2
2
 
3
3
  class CustomSettings < DotHash::Settings
4
- load attributes: {power: 10, skills: ["fireball", "frost"]}
4
+ load(
5
+ fixtures_path, # load from many sources and merges everything
6
+ attributes: {power: 10, skills: ["fireball", "frost"]}
7
+ )
5
8
  end
6
9
 
7
10
  describe CustomSettings do
8
- it "loads the given settings" do
9
- assert_equal CustomSettings.attributes.power, 10
10
- assert_equal CustomSettings.attributes.skills, ["fireball", "frost"]
11
- end
12
- end
13
-
14
- class CustomSettings2 < DotHash::Settings
15
- load attributes: {power: 10, name: "Eden", skills: ["fireball", "frost"]}
16
- namespace :attributes
17
- end
18
-
19
- describe CustomSettings2 do
20
- it "loads the settings within the namespace" do
21
- assert_equal CustomSettings2.power, 10
22
- assert_equal CustomSettings2.skills, ["fireball", "frost"]
11
+ it "loads the all given settings" do
12
+ assert_equal CustomSettings.to_hash, {
13
+ "default"=>{"attr"=>{"speed"=>10, "power"=>11}},
14
+ "rogue"=>{"attr"=>{"speed"=>25, "power"=>11}},
15
+ "hero"=>{"name"=>"Eden", "power"=>100, "location"=> TESTS_PATH},
16
+ :attributes=>{:power=>10, :skills=>["fireball", "frost"]}
17
+ }
23
18
  end
24
19
  end
@@ -2,59 +2,58 @@ require_relative "../test_helper"
2
2
 
3
3
  module DotHash
4
4
  describe Loader do
5
-
6
- describe "#load" do
5
+ describe "#hash" do
7
6
  it "merges the given hashes" do
8
7
  loader = Loader.new(
9
8
  {a: 1, b: {c: 2}},
10
9
  {b: {x: {z: 10}}}
11
10
  )
12
- assert_equal loader.load,({a: 1, b: {c: 2, x: {z: 10}}})
11
+ assert_equal loader.hash, {a: 1, b: {c: 2, x: {z: 10}}}
13
12
  end
14
13
 
15
14
  it "loads from a file" do
16
- loader = Loader.new fixtures_path("configs1.yaml")
15
+ loader = Loader.new fixtures_path("configs1.yml")
17
16
 
18
- assert_equal loader.load,({
17
+ assert_equal loader.hash, {
19
18
  "default" => {"attr" => {"speed"=>10, "power"=>11}},
20
19
  "rogue" => {"attr" => {"speed"=>20, "power"=>11}}
21
- })
20
+ }
22
21
  end
23
22
 
24
23
  it "loads from two files" do
25
24
  loader = Loader.new(
26
- fixtures_path("configs1.yaml"),
27
- fixtures_path("configs2.yaml")
25
+ fixtures_path("configs1.yml"),
26
+ fixtures_path("configs2.json")
28
27
  )
29
28
 
30
- assert_equal loader.load,({
29
+ assert_equal loader.hash,({
31
30
  "default"=>{"attr"=>{"speed"=>10, "power"=>11}},
32
31
  "rogue"=>{"attr"=>{"speed"=>25, "power"=>11}}
33
32
  })
34
33
  end
35
34
 
36
- it "does not load files without the yaml extension" do
35
+ it "does not hash files without the yaml extension" do
37
36
  loader = Loader.new fixtures_path("config.yaml.example")
38
37
 
39
- assert_equal loader.load,({})
38
+ assert_equal loader.hash, {}
40
39
  end
41
40
 
42
41
  it "loads from a directory" do
43
42
  loader = Loader.new fixtures_path
44
43
 
45
- assert_equal loader.load,({
44
+ assert_equal loader.hash, {
46
45
  "default"=>{"attr"=>{"speed"=>10, "power"=>11}},
47
46
  "rogue"=>{"attr"=>{"speed"=>25, "power"=>11}},
48
47
  "hero"=>{"name"=>"Eden", "power"=>100, "location"=>TESTS_PATH}
49
- })
48
+ }
50
49
  end
51
50
 
52
51
  it "loads ERB files" do
53
52
  loader = Loader.new fixtures_path("configs3.yaml.erb")
54
53
 
55
- assert_equal loader.load,({
54
+ assert_equal loader.hash, {
56
55
  "hero"=>{"name"=>"Eden", "power"=>100, "location"=>TESTS_PATH}
57
- })
56
+ }
58
57
  end
59
58
  end
60
59
  end
@@ -38,14 +38,28 @@ module DotHash
38
38
  end
39
39
  end
40
40
 
41
- describe ".instance" do
42
- before do
43
- configs = {site: "skyo.com"}
44
- Settings.load configs
45
- end
46
-
47
- it "returns a singleton instance of the settings" do
48
- Settings.instance.must_be_instance_of Settings
41
+ describe "#load" do
42
+ it 'merges new hashes' do
43
+ # starts as a empty hash
44
+ settings = Settings.new
45
+ assert_equal settings.hash, {}
46
+
47
+ # loads a new hash
48
+ settings.load({"name" => 'bar'})
49
+ assert_equal settings.hash, {"name" => "bar"}
50
+
51
+ # replaces existent props
52
+ settings.load({"name" => 'foo'})
53
+ assert_equal settings.hash, {"name" => "foo"}
54
+
55
+ # loads more stuff from a file
56
+ settings.load(fixtures_path)
57
+ assert_equal settings.hash, {
58
+ "name" => "foo",
59
+ "default" => {"attr" => {"speed"=>10, "power"=>11}},
60
+ "rogue"=>{"attr"=>{"speed"=>25, "power"=>11}},
61
+ "hero"=>{"name"=>"Eden", "power"=>100, "location"=> TESTS_PATH}
62
+ }
49
63
  end
50
64
  end
51
65
 
@@ -93,15 +107,15 @@ module DotHash
93
107
 
94
108
  describe "loading from files" do
95
109
  it "loads from a file" do
96
- Settings.load fixtures_path("configs1.yaml")
110
+ Settings.load fixtures_path("configs1.yml")
97
111
  assert_equal Settings.rogue.attr.speed, 20
98
112
  assert_equal Settings.rogue.attr.power, 11
99
113
  end
100
114
 
101
115
  it "loads from two files" do
102
116
  Settings.load(
103
- fixtures_path("configs1.yaml"),
104
- fixtures_path("configs2.yaml")
117
+ fixtures_path("configs1.yml"),
118
+ fixtures_path("configs2.json")
105
119
  )
106
120
 
107
121
  assert_equal Settings.rogue.attr.speed, 25
@@ -1,13 +1,14 @@
1
- require_relative "../test_helper"
1
+ require_relative "./test_helper"
2
2
 
3
- describe Hash do
3
+ describe DotHash do
4
4
  attr_reader :properties
5
5
 
6
- describe "#to_propertires" do
6
+ describe ".load" do
7
7
  before do
8
- @properties = {
9
- "price" => 10, info: {name: "eagle"}
10
- }.to_properties
8
+ @properties = DotHash.load({
9
+ "price" => 10,
10
+ info: {name: "eagle"}
11
+ })
11
12
  end
12
13
 
13
14
  it "returns a DotHash object" do
@@ -0,0 +1,7 @@
1
+ {
2
+ "rogue": {
3
+ "attr": {
4
+ "speed": 25
5
+ }
6
+ }
7
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dot_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Eden
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-30 00:00:00.000000000 Z
11
+ date: 2017-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -46,6 +46,7 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - ".rake_tasks"
49
50
  - ".travis.yml"
50
51
  - Gemfile
51
52
  - LICENSE
@@ -53,19 +54,18 @@ files:
53
54
  - Rakefile
54
55
  - dot_hash.gemspec
55
56
  - lib/dot_hash.rb
56
- - lib/dot_hash/hash_to_properties.rb
57
57
  - lib/dot_hash/loader.rb
58
58
  - lib/dot_hash/properties.rb
59
59
  - lib/dot_hash/settings.rb
60
60
  - lib/dot_hash/version.rb
61
61
  - test/dot_hash/custom_settings_test.rb
62
- - test/dot_hash/hash_to_properties_test.rb
63
62
  - test/dot_hash/loader_test.rb
64
63
  - test/dot_hash/properties_test.rb
65
64
  - test/dot_hash/settings_test.rb
65
+ - test/dot_hash_test.rb
66
66
  - test/fixtures/config.yaml.example
67
- - test/fixtures/configs1.yaml
68
- - test/fixtures/configs2.yaml
67
+ - test/fixtures/configs1.yml
68
+ - test/fixtures/configs2.json
69
69
  - test/fixtures/configs3.yaml.erb
70
70
  - test/test_helper.rb
71
71
  homepage: https://github.com/3den/dot_hash
@@ -93,12 +93,12 @@ specification_version: 4
93
93
  summary: Converts hash.to_properties so you can access values using properties.some_key
94
94
  test_files:
95
95
  - test/dot_hash/custom_settings_test.rb
96
- - test/dot_hash/hash_to_properties_test.rb
97
96
  - test/dot_hash/loader_test.rb
98
97
  - test/dot_hash/properties_test.rb
99
98
  - test/dot_hash/settings_test.rb
99
+ - test/dot_hash_test.rb
100
100
  - test/fixtures/config.yaml.example
101
- - test/fixtures/configs1.yaml
102
- - test/fixtures/configs2.yaml
101
+ - test/fixtures/configs1.yml
102
+ - test/fixtures/configs2.json
103
103
  - test/fixtures/configs3.yaml.erb
104
104
  - test/test_helper.rb
@@ -1,9 +0,0 @@
1
- module DotHash
2
- module HashToProperties
3
- def to_properties
4
- Properties.new self
5
- end
6
- end
7
- end
8
-
9
- Hash.send :include, DotHash::HashToProperties
@@ -1,3 +0,0 @@
1
- rogue:
2
- attr:
3
- speed: 25