config_this 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 104987bb4424859bdc6f284cc86c3c00d7e664cb1d1ecef53bfc77ef82cabd61
4
+ data.tar.gz: d1fb58a6172f84d6332aefba72a374789fcc9bf9cbee8a016d637ab0b02beb31
5
+ SHA512:
6
+ metadata.gz: 57222320ce79d442fdf26be91e5e0960dd85027836c0d7ae0baef50c698b48b6708b4fecbdcfd55d580a39cc3c3f554ed35f18b68cd88d4751b909a0fb29e8e5
7
+ data.tar.gz: 1ec9bbdd863ced085a4f877ee248a48ca59c976454d3b8796e20b3cc2e3110667ec2f3ea71d45d1e576d3966e21e3e219e6bedbef88d23f2889f10c76c7e5b65
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.0.pre.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in config.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Edmundo Sanchez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # Config
2
+
3
+ Have you ever wondered where is the right way to store sensitive information like passwords for your ruby scripts and gems?
4
+
5
+ I have, doing some research I found a few ways to create a class that will help set configuration object for your script, but I would have to write it for every script I write, so I created this gem.
6
+
7
+ With **Config** you can load your configuration parameters from a YAML or json file, a block or a hash.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'config'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install config
24
+
25
+ ## Usage
26
+
27
+ The Config Module is the main interface for this gem, as you know you can not instantiate a Module so be careful with your values
28
+
29
+ ### Get the Configuration object
30
+
31
+ This will get the latest object you created.
32
+ ``` ruby
33
+ Config.get
34
+ => #<Config::Configuration:0x000055d538ef2dc0>
35
+ ```
36
+
37
+ ### Setting the configuration Object
38
+
39
+ If you plan on having only one configuration object you don't need to assign it, it will always return
40
+
41
+ ``` ruby
42
+ Config.set(id:42, name: "Arthur Dent")
43
+ => #<Config::Configuration:0x000055d538f3c9c0 @id=42, @name="Arthur Dent">
44
+ ```
45
+
46
+ If you do plan to have multiple configurations, handle them properly
47
+
48
+ ``` ruby
49
+ Config.set(id:42, name: "Arthur Dent")
50
+ => #<Config::Configuration:0x000055d538f3c9c0 @id=42, @name="Arthur Dent">
51
+ number1 = Config.set(id:1, name: "Stefán Karl Stefánsson")
52
+ => #<Config::Configuration:0x000055d53866ea00 @id=1, @name="Stefán Karl Stefánsson">
53
+ ```
54
+
55
+ #### Setting it With a Hash
56
+
57
+ As you noticed, this is how you set it with a hash:
58
+ ``` ruby
59
+ Config.set(id:42, name: "Arthur Dent")
60
+ => #<Config::Configuration:0x000055d538f3c9c0 @id=42, @name="Arthur Dent">
61
+ ```
62
+
63
+ ### Setting it with a YAML file
64
+ ``` ruby
65
+ Config.yaml_file("/absolute/path/file.yaml")
66
+ => #<Config::Configuration:0x000055d538ed7d90 @id=42, @name="Arthur Dent", @weapon="Towel">
67
+ ```
68
+
69
+ ### Setting it with a JSON file
70
+ ``` ruby
71
+ Config.json_file("/absolute/path/file.json")
72
+ => #<Config::Configuration:0x000055d538a8f210 @id=42, @name="Arthur Dent", @weapon="Towel">
73
+ ```
74
+
75
+ ### Setting it with a Environmental Variables
76
+ Be sute the variables are set and accesible
77
+ ``` ruby
78
+ ENV["id"] = "42"
79
+ ENV["name"] = "Arthur Dent"
80
+ Config.env_variables("id","name")
81
+ => #<Config::Configuration:0x000055d538ef8b58 @id="42", @name="Arthur Dent">
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mundo03/config_me.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "config"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/config.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ require_relative 'lib/config/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "config_this"
5
+ spec.version = Config::VERSION
6
+ spec.authors = ["Edmundo Sanchez"]
7
+ spec.email = ["zomundo@gmail.com"]
8
+
9
+ spec.summary = "An easy way to create configuration for your gem or script"
10
+ spec.description = "Create a configuration object with yaml, json, env variables, or ruby"
11
+ spec.homepage = "https://github.com/mundo03/config_me/releases"
12
+ spec.license = "MIT"
13
+
14
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "https://github.com/mundo03/config_me/releases"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.17.3"
30
+ spec.add_development_dependency "rake", "~> 12.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "yard", "~> 0.9.2"
33
+ end
data/lib/config.rb ADDED
@@ -0,0 +1,67 @@
1
+ require "config/version"
2
+ require "yaml"
3
+ require "json"
4
+
5
+ # @author Edmundo Sanchez
6
+ # Configuration module
7
+ module Config
8
+ class Error < StandardError; end
9
+ # Get configuration
10
+ # @return [Config] configuration
11
+ def self.get
12
+ @configuration ||= Configuration.new
13
+ end
14
+ # Set Configuration with a block
15
+ # @param [Hash] Configuration Hash
16
+ def self.set(config)
17
+ @configuration = Configuration.new do |conf|
18
+ config.each do |key,value|
19
+ conf.instance_variable_set("@#{key}",value)
20
+ end
21
+ end
22
+ end
23
+ # Reset the configuration to an empty Configuration object
24
+ def self.reset
25
+ @configuration = Configuration.new
26
+ end
27
+ # Read YAML file and set configuration
28
+ # @param [String] Absolute Path to Yaml file
29
+ def self.yaml_file(path)
30
+ self.set YAML.load_file(path)
31
+ end
32
+ # Read JSON file and set configuration
33
+ # @param [String] Absolute Path to Yaml file
34
+ def self.json_file(path)
35
+ file = File.read(path)
36
+ self.set JSON.parse(file)
37
+ end
38
+ # Read Environmental Variables and set configuration
39
+ # Note All values will be strings by definition
40
+ # @param [Array] List of Environmental variables
41
+ def self.env_variables(*variables)
42
+ variables.map! do |var|
43
+ [var,ENV[var]]
44
+ end
45
+ self.set(Hash[*variables.flatten])
46
+ end
47
+
48
+ class Configuration
49
+ # If block given, greate all instance variables
50
+ # @return [Configuration] self
51
+ def initialize
52
+ yield(self) if block_given?
53
+ return self
54
+ end
55
+ # Sets any un set instance variables
56
+ def method_missing(method, *args, &block)
57
+ attr_name = method.to_s.gsub("=","")
58
+ self.class.send(:attr_accessor, attr_name) ## Ensure accesor is set
59
+ # ensure correct behavior if accesor was not set, hence method was missing
60
+ if self.instance_variable_defined?("@#{attr_name}")
61
+ self.instance_variable_get("@#{attr_name}")
62
+ else
63
+ self.instance_variable_set("@#{attr_name}", args.first)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Config
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config_this
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Edmundo Sanchez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.17.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.17.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.2
69
+ description: Create a configuration object with yaml, json, env variables, or ruby
70
+ email:
71
+ - zomundo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - config.gemspec
86
+ - lib/config.rb
87
+ - lib/config/version.rb
88
+ homepage: https://github.com/mundo03/config_me/releases
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ homepage_uri: https://github.com/mundo03/config_me/releases
93
+ source_code_uri: https://github.com/mundo03/config_me/releases
94
+ changelog_uri: https://github.com/mundo03/config_me/releases
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.0.4
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: An easy way to create configuration for your gem or script
114
+ test_files: []