benry-config 0.1.0

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3370f1b72911ca0f9a74d6240b1025a792166d3d
4
+ data.tar.gz: a4b2152cb0e4f51f4b3c36e25fccd66bf210f5e3
5
+ SHA512:
6
+ metadata.gz: 7f81be992cc07abe26b2680940cf618daa2f5f4047ac91748e3ad16286d54d6c64a527a543e68e2fa50bcd7114307d3dfb4ac202d977b54ad5720651db3a3913
7
+ data.tar.gz: 1939f9f82a48c679ae927ea9f34f0e34d75bfb08e3909984b6e68a07267fb24f959a2a588e8bf2210a4721a96efef152bde1ebd4636089708b691d5e7b213021
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 kuwata-lab.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ benry-config
2
+ ============
3
+
4
+ ($Release: 0.1.0 $)
5
+
6
+
7
+ Overview
8
+ --------
9
+
10
+ Utility class to support configuration.
11
+
12
+ * Easy to define configuration for environments (production, development, ...).
13
+ * Raises error when configuration name is wrong (typo).
14
+ * Represents secret configurations which should be set in private file.
15
+
16
+
17
+ Example
18
+ -------
19
+
20
+ ```ruby
21
+ #----- config/common.rb -----
22
+ require 'benry/config'
23
+ class CommonConfig < Benry::BaseConfig
24
+ add :db_user , "user1"
25
+ add :db_pass , ABSTRACT
26
+ add :session_cooie , "sess"
27
+ add :session_secret , SECRET
28
+ end
29
+
30
+ #----- config/development.rb -----
31
+ require 'config/common'
32
+ class Config < CommonConfig
33
+ set :db_pass , "pass1"
34
+ end
35
+
36
+ #----- config/development.private -----
37
+ Config.class_eval do
38
+ set :session_secret , "abc123"
39
+ end
40
+
41
+ #----- main.rb -----
42
+ ## Ruby < 2.2 has obsoleted 'Config' class, therefore remove it at first.
43
+ Object.class_eval { remove_const :Config } if defined?(Config)
44
+ #
45
+ rack_env = ENV['RACK_ENV'] or raise "$RACK_ENV required."
46
+ require "./config/#{rack_env}.rb"
47
+ load "./config/#{rack_env}.private"
48
+ #
49
+ $config = Config.new.freeze
50
+ p $config.db_user #=> "user1"
51
+ p $config.db_pass #=> "pass1"
52
+ p $config.session_cookie #=> "sess"
53
+ p $config.session_secret #=> "abc123"
54
+ #
55
+ p $config.get_all(:db_) #=> {:user=>"user1", :pass=>"pass1"}
56
+ p $config.get_all(:session_) #=> {:cookie=>"sess", :secret=>"abc123"}
57
+ ```
58
+
59
+
60
+ Copyright and License
61
+ ---------------------
62
+
63
+ $Copyright: copyright(c) 2016 kuwata-lab.com all rights reserved $
64
+
65
+ $License: MIT License $
@@ -0,0 +1,83 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ project = "benry-config"
5
+ release = ENV['RELEASE'] || "0.0.0"
6
+ copyright = "copyright(c) 2016 kuwata-lab.com all rights reserved"
7
+ license = "MIT License"
8
+
9
+ target_files = Dir[*%W[
10
+ README.md MIT-LICENSE.txt Rakefile
11
+ lib/**/*.rb
12
+ test/**/*_test.rb
13
+ #{project}.gemspec
14
+ ]]
15
+
16
+
17
+ task :default => :help
18
+
19
+
20
+ desc "show help"
21
+ task :help do
22
+ puts "rake help # help"
23
+ puts "rake test # run test"
24
+ puts "rake package RELEASE=X.X.X # create gem file"
25
+ puts "rake publish RELEASE=X.X.X # upload gem file"
26
+ end
27
+
28
+
29
+ desc "do test"
30
+ task :test do
31
+ sh "ruby", *Dir.glob("test/*.rb")
32
+ end
33
+
34
+
35
+ desc "create package"
36
+ task :package do
37
+ release != "0.0.0" or
38
+ raise "specify $RELEASE"
39
+ ## copy
40
+ dir = "build"
41
+ rm_rf dir if File.exist?(dir)
42
+ mkdir dir
43
+ target_files.each do |file|
44
+ dest = File.join(dir, File.dirname(file))
45
+ mkdir_p dest, :verbose=>false unless File.exist?(dest)
46
+ cp file, "#{dir}/#{file}"
47
+ end
48
+ ## edit
49
+ Dir.glob("#{dir}/**/*").each do |file|
50
+ next unless File.file?(file)
51
+ File.open(file, 'rb+') do |f|
52
+ s = f.read()
53
+ s = s.gsub(/\$Release: 0.1.0 $/, "$"+"Release: #{release} $")
54
+ s = s.gsub(/\$Copyright: copyright(c) 2016 kuwata-lab.com all rights reserved $/, "$"+"Copyright: #{copyright} $")
55
+ s = s.gsub(/\$License: MIT License $/, "$"+"License: #{license} $")
56
+ #
57
+ f.rewind()
58
+ f.truncate(0)
59
+ f.write(s)
60
+ end
61
+ end
62
+ ## build
63
+ chdir dir do
64
+ sh "gem build #{project}.gemspec"
65
+ end
66
+ mv "#{dir}/#{project}-#{release}.gem", "."
67
+ end
68
+
69
+
70
+ desc "upload gem file to rubygems.org"
71
+ task :publish do
72
+ release != "0.0.0" or
73
+ raise "specify $RELEASE"
74
+ #
75
+ gemfile = "#{project}-#{release}.gem"
76
+ print "** Are you sure to publish #{gemfile}? [y/N]: "
77
+ ans = $stdin.gets().strip()
78
+ if ans.downcase.start_with?("y")
79
+ sh "gem push #{gemfile}"
80
+ sh "git tag #{project}-#{release}"
81
+ sh "git push --tags"
82
+ end
83
+ end
@@ -0,0 +1,140 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 0.1.0 $
5
+ ### $Copyright: copyright(c) 2016 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+
10
+ module Benry
11
+
12
+
13
+ class ConfigError < StandardError
14
+ end
15
+
16
+
17
+ ##
18
+ ## Configuration class.
19
+ ##
20
+ ## ex:
21
+ ##
22
+ ## #----- config/common.rb -----
23
+ ## require 'benry/config'
24
+ ## class CommonConfig < Benry::BaseConfig
25
+ ## add :db_user , "user1"
26
+ ## add :db_pass , ABSTRACT
27
+ ## add :session_cooie , "SESS"
28
+ ## add :session_secret , SECRET
29
+ ## end
30
+ ##
31
+ ## #----- config/development.rb -----
32
+ ## require 'config/common'
33
+ ## class Config < TestCommonConfig
34
+ ## set :db_pass , "pass1"
35
+ ## end
36
+ ##
37
+ ## #----- config/development.private -----
38
+ ## class Config
39
+ ## set :session_secret , "abc123"
40
+ ## end
41
+ ##
42
+ ## #----- main.rb -----
43
+ ## rack_env = ENV['RACK_ENV'] or raise "$RACK_ENV required."
44
+ ## require "./config/#{rack_env}.rb"
45
+ ## load "./config/#{rack_env}.private"
46
+ ## #
47
+ ## $config = Config.new.freeze
48
+ ## p $config.db_user #=> "user1"
49
+ ## p $config.db_pass #=> "pass1"
50
+ ## p $config.session_cookie #=> "SESS"
51
+ ## p $config.session_secret #=> "abc123"
52
+ ## #
53
+ ## p $config.get_all(:db_) #=> {:user=>"user1", :pass=>"pass1"}
54
+ ## p $config.get_all(:session_) #=> {:cookie=>"SESS", :secret=>"abc123"}
55
+ ##
56
+ class BaseConfig
57
+
58
+ class AbstractValue
59
+ end
60
+
61
+ ABSTRACT = AbstractValue.new # represents 'should be set in subclass'
62
+ SECRET = AbstractValue.new # represents 'should be set in private config file'
63
+
64
+ def initialize
65
+ #; [!7rdq4] traverses parent class and gathers config values.
66
+ pr = proc {|cls|
67
+ pr.call(cls.superclass) if cls.superclass
68
+ d = cls.instance_variable_get('@__dict')
69
+ d.each {|k, v| instance_variable_set("@#{k}", v) } if d
70
+ }
71
+ pr.call(self.class)
72
+ #; [!z9mno] raises ConfigError when ABSTRACT or SECRET is not overriden.
73
+ instance_variables().each do |ivar|
74
+ val = instance_variable_get(ivar)
75
+ ! val.is_a?(AbstractValue) or
76
+ raise ConfigError.new("config ':#{ivar.to_s[1..-1]}' should be set, but not.")
77
+ end
78
+ end
79
+
80
+ ## Add new config. Raises ConfigError when already defined.
81
+ def self.add(key, value, desc=nil)
82
+ #; [!m7w96] raises ConfigError when already added.
83
+ ! self.method_defined?(key) or
84
+ raise ConfigError.new("add #{key.inspect} : already defined (use set() instead).")
85
+ #; [!s620t] adds new key and value.
86
+ (@__dict ||= {})[key] = value
87
+ #; [!o0ts4] defines getter method.
88
+ attr_reader key
89
+ value
90
+ end
91
+
92
+ ## Set existing config. Raises ConfigError when key not defined.
93
+ def self.set(key, value, desc=nil)
94
+ #; [!fxc4h] raises ConfigError when not defined yet.
95
+ self.method_defined?(key) or
96
+ raise ConfigError.new("set #{key.inspect} : not defined (use add() instead).")
97
+ #; [!cv8iz] overrides existing value.
98
+ (@__dict ||= {})[key] = value
99
+ value
100
+ end
101
+
102
+ ## Add or set config. Raises nothing whether defined or not.
103
+ def self.put(key, value, desc=nil)
104
+ #; [!abd3f] raises nothing whener defined or not.
105
+ #; [!gu2f0] sets key and value.
106
+ (@__dict ||= {})[key] = value
107
+ #; [!84kbr] defines getter method.
108
+ attr_reader key
109
+ value
110
+ end
111
+
112
+ ## Return config value.
113
+ def [](key)
114
+ #; [!z9r30] returns config value.
115
+ return self.__send__(key)
116
+ end
117
+
118
+ ## Gathers related configs starting with prefix specified.
119
+ def get_all(prefix_key)
120
+ #; [!85z23] gathers configs which name starts with specified prefix.
121
+ prefix = "@#{prefix_key}"
122
+ symbol_p = prefix_key.is_a?(Symbol)
123
+ range = prefix.length..-1
124
+ d = {}
125
+ self.instance_variables.each do |ivar|
126
+ if ivar.to_s.start_with?(prefix)
127
+ val = self.instance_variable_get(ivar)
128
+ key = ivar[range]
129
+ #; [!b72fr] if prefix is a string, then keys returend will also be string.
130
+ key = key.intern if symbol_p
131
+ d[key] = val
132
+ end
133
+ end
134
+ return d
135
+ end
136
+
137
+ end
138
+
139
+
140
+ end
@@ -0,0 +1,182 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'minitest/spec'
4
+ require 'minitest/autorun'
5
+ require 'minitest/ok'
6
+
7
+ require 'benry/config'
8
+
9
+
10
+
11
+
12
+ class TestCommonConfig < Benry::BaseConfig
13
+ add :db_name , "db1"
14
+ add :db_user , "user1"
15
+ add :db_pass , ABSTRACT
16
+ add :session_secret , SECRET
17
+ end
18
+
19
+ class TestConfig < TestCommonConfig
20
+ set :db_pass , "pass1"
21
+ set :session_secret , "abc123"
22
+ end
23
+
24
+ class TestConfig2 < TestCommonConfig
25
+ #set :db_pass , "pass1"
26
+ set :session_secret , "abc123"
27
+ end
28
+
29
+ class TestConfig3 < TestCommonConfig
30
+ set :db_pass , "pass1"
31
+ #set :session_secret , "abc123"
32
+ end
33
+
34
+
35
+
36
+ describe Benry::BaseConfig do
37
+
38
+
39
+ describe '#initialize()' do
40
+
41
+ it "[!7rdq4] traverses parent class and gathers config values." do
42
+ config = TestConfig.new
43
+ ok {config.db_name} == "db1"
44
+ ok {config.db_user} == "user1"
45
+ ok {config.db_pass} == "pass1"
46
+ ok {config.session_secret} == "abc123"
47
+ end
48
+
49
+ it "[!z9mno] raises ConfigError when ABSTRACT or SECRET is not overriden." do
50
+ pr = proc { TestConfig2.new }
51
+ ex = ok {pr}.raise?(Benry::ConfigError)
52
+ ok {ex.message} == "config ':db_pass' should be set, but not."
53
+ #
54
+ pr = proc { TestConfig3.new }
55
+ ex = ok {pr}.raise?(Benry::ConfigError)
56
+ ok {ex.message} == "config ':session_secret' should be set, but not."
57
+ end
58
+
59
+ end
60
+
61
+
62
+ describe '.add()' do
63
+
64
+ it "[!m7w96] raises ConfigError when already added." do
65
+ pr = proc do
66
+ TestCommonConfig.class_eval do
67
+ add :db_name, "db9"
68
+ end
69
+ end
70
+ ex = ok {pr}.raise?(Benry::ConfigError)
71
+ ok {ex.message} == "add :db_name : already defined (use set() instead)."
72
+ end
73
+
74
+ it "[!s620t] adds new key and value." do
75
+ ok {TestCommonConfig.instance_variable_get('@__dict')} == {
76
+ :db_name => "db1",
77
+ :db_user => "user1",
78
+ :db_pass => Benry::BaseConfig::ABSTRACT,
79
+ :session_secret => Benry::BaseConfig::SECRET,
80
+ }
81
+ end
82
+
83
+ it "[!o0ts4] defines getter method." do
84
+ cls = Class.new(Benry::BaseConfig) do
85
+ add :foo , "FOO"
86
+ end
87
+ obj = cls.new
88
+ ok {obj}.respond_to?(:foo)
89
+ ok {obj.foo} == "FOO"
90
+ end
91
+
92
+ end
93
+
94
+
95
+ describe '.set()' do
96
+
97
+ it "[!fxc4h] raises ConfigError when not defined yet." do
98
+ pr = proc do
99
+ TestCommonConfig.class_eval do
100
+ set :db_port, 5432
101
+ end
102
+ end
103
+ ex = ok {pr}.raise?(Benry::ConfigError)
104
+ ok {ex.message} == "set :db_port : not defined (use add() instead)."
105
+ end
106
+
107
+ it "[!cv8iz] overrides existing value." do
108
+ ok {TestConfig.instance_variable_get('@__dict')} == {
109
+ :db_pass => "pass1",
110
+ :session_secret => "abc123",
111
+ }
112
+ #
113
+ config = TestConfig.new
114
+ ok {config.db_pass} == "pass1"
115
+ ok {config.session_secret} == "abc123"
116
+ end
117
+
118
+ end
119
+
120
+
121
+ describe '.put()' do
122
+
123
+ it "[!abd3f] raises nothing whener defined or not." do
124
+ pr = proc do
125
+ Class.new(TestCommonConfig) do
126
+ put :db_name , "db9" # existing
127
+ put :db_port , "5432" # not existing
128
+ end
129
+ end
130
+ ok {pr}.NOT.raise?(Exception)
131
+ end
132
+
133
+ it "[!gu2f0] sets key and value." do
134
+ cls = Class.new(TestCommonConfig) do
135
+ put :db_name , "db9" # existing
136
+ put :db_port , 5432 # not existing
137
+ end
138
+ ok {cls.instance_variable_get('@__dict')} == {
139
+ :db_name => "db9",
140
+ :db_port => 5432,
141
+ }
142
+ end
143
+
144
+ it "[!84kbr] defines getter method." do
145
+ cls = Class.new(TestCommonConfig) do
146
+ put :db_name , "db9" # existing
147
+ put :db_port , 5432 # not existing
148
+ end
149
+ ok {cls}.method_defined?(:db_name)
150
+ ok {cls}.method_defined?(:db_port)
151
+ end
152
+
153
+ end
154
+
155
+
156
+ describe '#[]' do
157
+
158
+ it "[!z9r30] returns config value." do
159
+ config = TestConfig.new()
160
+ ok {config[:db_user]} == "user1"
161
+ ok {config[:db_pass]} == "pass1"
162
+ end
163
+
164
+ end
165
+
166
+
167
+ describe '#get_all()' do
168
+
169
+ it "[!85z23] gathers configs which name starts with specified prefix." do
170
+ config = TestConfig.new()
171
+ ok {config.get_all(:db_)} == {:name=>"db1", :user=>"user1", :pass=>"pass1"}
172
+ end
173
+
174
+ it "[!b72fr] if prefix is a string, then keys returend will also be string." do
175
+ config = TestConfig.new()
176
+ ok {config.get_all("db_")} == {"name"=>"db1", "user"=>"user1", "pass"=>"pass1"}
177
+ end
178
+
179
+ end
180
+
181
+
182
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benry-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - makoto kuwata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-ok
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
+ description: 'See https://github.com/kwatch/benry/tree/ruby/benry-config for details.
42
+
43
+ '
44
+ email:
45
+ - kwa(at)kuwata-lab.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - MIT-LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/benry/config.rb
54
+ - test/config_test.rb
55
+ homepage: https://github.com/kwatch/benry/tree/ruby/benry-config
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.5.1
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: useful configuration class
79
+ test_files: []