info_hub 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 391b44bfb0ff261310da84f2b0026f1be9e790cc
4
+ data.tar.gz: 21d5fec5a67c24320f465405bcb1139fbb77589e
5
+ SHA512:
6
+ metadata.gz: e3ebb2e5c6efaad9cdab111cdae1504c995aa3aeb156293e6007f82e63fdc5cdfc6a6b2f41175887afc7dd9f04df3cefd115670f68674bf531b887df01892143
7
+ data.tar.gz: 4ae5f6e4f2db0497a82de5a144904f57afbe76280172d4b65f6d93ba72aab91c9b59e9973602adb24404378e31726de513668325ba98d5d1046939a2f99e9c21
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ [gem]: https://rubygems.org/gems/info_hub
2
+ [travis]: https://travis-ci.org/OnApp/info_hub
3
+
4
+ # InfoHub
5
+
6
+ [![Gem Version](https://badge.fury.io/rb/info_hub.svg)][gem]
7
+ [![Build Status](https://travis-ci.org/OnApp/info_hub.svg?branch=master)][travis]
8
+
9
+ This gem delivers a simple DSL to read data from YAML files which might be useful for storing some basic knowledge around the application.
10
+
11
+ ### Installation
12
+ Add to your `Gemfile`:
13
+
14
+ ```ruby
15
+ # Gemfile
16
+ gem 'info_hub'
17
+ ```
18
+ and run `bundle install`
19
+
20
+ Create a new file which includes all syste-wide constants:
21
+ ```ruby
22
+ # config/info_hub.yml
23
+ percentage:
24
+ min: 1
25
+ max: 100
26
+ ```
27
+
28
+ Then add add path to that file to `InfoHub` paths (in `config/initializers/info_hub.rb`)
29
+ ```ruby
30
+ # config/initializers/info_hub.rb
31
+ InfoHub.info_hub_file_paths << File.expand_path('../info_hub.yml', __dir__)
32
+
33
+ InfoHub.finalize!
34
+ ```
35
+ Before `finalize!` execution, you may add as many `.yml` files as you need. All of them will be deeply merged in the order they were added.
36
+
37
+ ### Usage
38
+ Now anywhere in you code you can:
39
+ ```ruby
40
+ InfoHub[:percentage] # => { min: 1, max: 100 }
41
+ # or the equivalent
42
+ InfoHub.fetch(:percentage) # => { min: 1, max: 100 }
43
+ ```
44
+ You may also get some internal values:
45
+ ```ruby
46
+ # Similar to `dig` in ruby 2.3
47
+ InfoHub.get(:percentage, :min) # => 1
48
+ InfoHub.get(:percentage, :max) # => 100
49
+ ```
50
+ ### Licence
51
+ See `LICENSE` file.
52
+
53
+ ## Contributing
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/OnApp/info_hub.
@@ -0,0 +1,3 @@
1
+ module InfoHub
2
+ VERSION = '0.0.1'.freeze
3
+ end
data/lib/info_hub.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'active_support/all'
2
+ require 'yaml'
3
+
4
+ module InfoHub
5
+ extend self
6
+
7
+ attr_accessor :info_hub_file_paths
8
+
9
+ KeyNotFoundError = Class.new(StandardError)
10
+ SettingsNotFinalizedError = Class.new(StandardError)
11
+ SettingsAlreadyFinalizedError = Class.new(StandardError)
12
+
13
+ def info_hub_file_paths
14
+ @info_hub_file_paths ||= []
15
+ end
16
+
17
+ def fetch(key)
18
+ settings.fetch(key) { raise_error(key) }
19
+ end
20
+ alias_method :[], :fetch
21
+
22
+ def use(key)
23
+ yield fetch(key)
24
+ end
25
+
26
+ def get(*keys)
27
+ keys.inject(settings) { |settings, key| settings.fetch(key) { raise_error(key) } }
28
+ end
29
+
30
+ def finalize!
31
+ raise SettingsAlreadyFinalizedError, 'InfoHub configuration is already finalized.' if finalized?
32
+
33
+ info_hub_file_paths.freeze
34
+ end
35
+
36
+ def finalized?
37
+ info_hub_file_paths.frozen?
38
+ end
39
+
40
+ private
41
+
42
+ def settings
43
+ @settings ||= begin
44
+ raise SettingsNotFinalizedError, 'Settings not finalized' unless finalized?
45
+
46
+ info_hub_file_paths.inject({}) do |settings, path|
47
+ settings.deep_merge!(YAML.load_file(path).deep_symbolize_keys)
48
+ end
49
+ end
50
+ end
51
+
52
+ def raise_error(key)
53
+ raise KeyNotFoundError, "`#{key}` key not found"
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: info_hub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - OnApp devs
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fuubar
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: |2
70
+ This gem delivers a simple DSL to read data from YAML files.
71
+ That might be useful for storing some basic knowledge around the application.
72
+ email:
73
+ - onapp@onapp.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - README.md
79
+ - lib/info_hub.rb
80
+ - lib/info_hub/version.rb
81
+ homepage: https://github.com/OnApp/info_hub
82
+ licenses:
83
+ - Apache 2.0
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '2.0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Handy library to read from YAML files
105
+ test_files: []