jewelbox 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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jewelbox.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Jewelbox
2
+
3
+ Useful Ruby utility classes that handle:
4
+
5
+ - configuration
6
+ - time stamp
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'jewelbox'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install jewelbox
21
+
22
+ ## Usage
23
+
24
+ Service Configuration
25
+ =====================
26
+
27
+ If you want to keep your application configuration separate from Rails config,
28
+ you might store your configuration in a YAML file under conf/ directory.
29
+
30
+ 1. Suppose you have "/opt/myrailsapp/current/conf/service.yml" that looks like:
31
+
32
+ ---
33
+ service:
34
+ name: myrailsapp
35
+ read_port: 80
36
+ write_port: 81
37
+ num_workers: 4
38
+ section1:
39
+ directive1: true
40
+ directive2: 100
41
+ directive3:
42
+ key1: val1
43
+ key2: val2
44
+
45
+ 2. Access the configuration file like this:
46
+
47
+ # Load service.yml like this
48
+ Jewelbox::Config.load("conf/service.yml")
49
+ -or-
50
+ Jewelbox.service_init # this will load "conf/service.yml"
51
+ # from current working directory
52
+ # Access service configuration
53
+ Jewelbox.config.service.name => "myrailsapp"
54
+ Jewelbox.config.service.read_port => 80
55
+ Jewelbox.config.service.write_port => 81
56
+ Jewelbox.config.service.num_workers => 4
57
+ Jewelbox.config.section1.directive1 => true
58
+ Jewelbox.config.section1.directive2 => 100
59
+ Jewelbox.config.section1.directive3 => {"key1" => "val1", "key2" => "val2"}
60
+ Jewelbox.config.section1 => <Section> object
61
+
62
+ # You can dynamically add things to config or section objects
63
+ Jewelbox.config.add("section2", "directive1", "new val")
64
+ Jewelbox.config.add("section2", "directive2", {:a => 20})
65
+ Jewelbox.config.section2.add("directive3", 200)
66
+ Jewelbox.config.section2.directive1 => "new_val"
67
+ Jewelbox.config.section2.directive2 => returns the same object you added
68
+ Jewelbox.config.section2.directive3 => 200
69
+
70
+ # some automatic bonus configs
71
+ Jewelbox.config.service.root_dir => "/opt/myrailsapp/current"
72
+ Jewelbox.config.service.conf_dir => "/opt/myrailsapp/current/conf"
73
+ Jewelbox.config.service.log_dir => "/opt/myrailsapp/current/log"
74
+ Jewelbox.config.service.bin_dir => "/opt/myrailsapp/current/bin"
75
+
76
+ 3. And my unicorn configuration looks like this:
77
+
78
+ TBD
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake"
4
+ require "rake/testtask"
5
+
6
+ namespace :test do
7
+
8
+ Rake::TestTask.new(:unit) do |test|
9
+ test.libs << 'test'
10
+ test.pattern = '{test/unit/**/*_test.rb}'
11
+ end
12
+
13
+ Rake::TestTask.new(:all) do |test|
14
+ test.libs << 'test'
15
+ test.pattern = '{test/**/*_test.rb}'
16
+ end
17
+
18
+ end
19
+
20
+ task :test => "test:unit"
21
+ task :default => "test"
22
+
data/jewelbox.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/jewelbox/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Steve Jang"]
6
+ gem.email = ["estebanjang@gmail.com"]
7
+ gem.description = %q{A set of useful Ruby functions to be used when creating a new service or rails application}
8
+ gem.summary = %q{Contains functions that support service configuration, useful ruby methods (such as eigenclass), and other functionality commonly used by Ruby services}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "jewelbox"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Jewelbox::VERSION
17
+
18
+ gem.add_development_dependency('ruby-debug19')
19
+ end
@@ -0,0 +1,102 @@
1
+ require 'jewelbox/ext/object'
2
+ require 'yaml'
3
+ module Jewelbox
4
+
5
+ # === Description
6
+ # Configure the current process with the configuration items stored in
7
+ # conf/service.yml file from the current working directory.
8
+ #
9
+ # === Parameters
10
+ # None
11
+ #
12
+ # === Returns
13
+ # After this call, you can access the following configuration items
14
+ #
15
+ # Jewelbox.config.service.name => name of the service (e.g. "taskmon")
16
+ # Jewelbox.config.service.root_dir => everything else is relative to this
17
+ # Jewelbox.config.service.conf_dir
18
+ # Jewelbox.config.service.log_dir
19
+ # Jewelbox.config.service.bin_dir
20
+ # Jewelbox.config.service.lib_dir
21
+ #
22
+ # Also anything that's in service.yml will be accessible from
23
+ # Jewelbox::Service.config object.
24
+ #
25
+ def self.service_init
26
+ @service_root = ENV['SERVICE_ROOT'] || Dir.pwd
27
+ Config.load(File.join(@service_root,'conf','service.yml'))
28
+ @config.add('service', 'root_dir', @service_root)
29
+ @config.add('service', 'conf_dir', File.join(@service_root, 'conf'))
30
+ @config.add('service', 'log_dir', File.join(@service_root, 'log'))
31
+ @config.add('service', 'bin_dir', File.join(@service_root, 'bin'))
32
+ @config.add('service', 'lib_dir', File.join(@service_root, 'lib'))
33
+ @config
34
+ end
35
+
36
+ # === Description
37
+ # All configuration files can be accessed through this.
38
+ #
39
+ def self.config
40
+ @config ||= Config.new
41
+ end
42
+
43
+ # === Description
44
+ # Configuration object that represents ALL configuration items
45
+ # to be used by the current service process
46
+ #
47
+ class Config
48
+
49
+ # === Description
50
+ # Read the given YAML file, and add its content to Jewelbox.config.
51
+ #
52
+ def self.load(yml_path)
53
+ abs_path = File.expand_path(yml_path)
54
+ h = YAML::load(File.read(yml_path))
55
+ h.each do |section, inner|
56
+ inner.each do |directive, v|
57
+ Jewelbox.config.add(section, directive, v)
58
+ end
59
+ end
60
+ end
61
+
62
+ # === Description
63
+ # Inner class that represents a section
64
+ #
65
+ class Section
66
+ def initialize(name)
67
+ @name = name
68
+ @data = {}
69
+ end
70
+ def add(key, value)
71
+ @data[key] = value
72
+ eigenclass.send(:define_method, key) { value }
73
+ end
74
+ end
75
+
76
+ def initialize # :nodoc:
77
+ @section = {}
78
+ end
79
+
80
+ # === Description
81
+ # Add a configuration item to configuration object
82
+ #
83
+ # === Parameters
84
+ # section_name:: [String] section name
85
+ # directive_name:: [String] directive name
86
+ # value:: [Object] configuration value
87
+ #
88
+ # === Returns
89
+ # Config object self
90
+ #
91
+ def add(section_name, directive_name, value)
92
+ section = @section[section_name]
93
+ if section.nil?
94
+ section = (@section[section_name] = Section.new(section_name))
95
+ eigenclass.send(:define_method, section_name) { section }
96
+ end
97
+ section.add(directive_name, value)
98
+ self
99
+ end
100
+
101
+ end # Config
102
+ end # Jewelbox
@@ -0,0 +1,7 @@
1
+ class Object
2
+ def eigenclass
3
+ class << self
4
+ self
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'time'
2
+ class Time
3
+ def iso_8601
4
+ strftime("%FT%T%z")
5
+ end
6
+ end
7
+
@@ -0,0 +1,3 @@
1
+ module Jewelbox
2
+ VERSION = "0.0.1"
3
+ end
data/lib/jewelbox.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "jewelbox/version"
2
+ require "jewelbox/config"
3
+ require "jewelbox/ext/object"
4
+ require "jewelbox/ext/time"
@@ -0,0 +1,5 @@
1
+ ---
2
+ section2:
3
+ directive1: false
4
+ directive2: 200
5
+
@@ -0,0 +1,18 @@
1
+ ---
2
+ service:
3
+ name: clocktower
4
+ webui_port: 80100
5
+ api_port: 80100
6
+ worker_port: 80101
7
+ num_workers: 4
8
+ section1:
9
+ directive1: true
10
+ directive2: 100
11
+ directive3: "string x"
12
+ directive4: "string y"
13
+ directive5: ["array", "of", "strings"]
14
+ directive6:
15
+ key1: val1
16
+ key2: val2
17
+ key3: val3
18
+
@@ -0,0 +1,39 @@
1
+ ENV['SERVICE_ROOT'] = File.expand_path(File.dirname(__FILE__))
2
+ require 'minitest/autorun'
3
+ require 'jewelbox'
4
+
5
+ class JewelboxConfigTest < MiniTest::Unit::TestCase
6
+
7
+ def setup
8
+ Jewelbox.service_init
9
+ end
10
+
11
+ def test_service_config
12
+ assert_equal('clocktower', Jewelbox.config.service.name)
13
+ assert_equal(80100, Jewelbox.config.service.webui_port)
14
+ assert_equal(80100, Jewelbox.config.service.api_port)
15
+ assert_equal(80101, Jewelbox.config.service.worker_port)
16
+ assert_equal(4, Jewelbox.config.service.num_workers)
17
+ assert_equal(Jewelbox.config.service.root_dir + "/conf", Jewelbox.config.service.conf_dir)
18
+ assert_equal(Jewelbox.config.service.root_dir + "/log", Jewelbox.config.service.log_dir)
19
+ assert_equal(Jewelbox.config.service.root_dir + "/bin", Jewelbox.config.service.bin_dir)
20
+ assert_equal(Jewelbox.config.service.root_dir + "/lib", Jewelbox.config.service.lib_dir)
21
+ end
22
+
23
+ def test_section1
24
+ assert_equal(true, Jewelbox.config.section1.directive1)
25
+ assert_equal(100, Jewelbox.config.section1.directive2)
26
+ assert_equal("string x", Jewelbox.config.section1.directive3)
27
+ assert_equal("string y", Jewelbox.config.section1.directive4)
28
+ assert_equal(["array", "of", "strings"], Jewelbox.config.section1.directive5)
29
+ assert_equal({"key1" => "val1", "key2" => "val2", "key3" => "val3"}, Jewelbox.config.section1.directive6)
30
+ end
31
+
32
+ def test_section2
33
+ Jewelbox::Config.load(Jewelbox.config.service.conf_dir + "/additional.yml")
34
+ assert_equal(false, Jewelbox.config.section2.directive1)
35
+ assert_equal(200, Jewelbox.config.section2.directive2)
36
+ end
37
+
38
+ end
39
+
@@ -0,0 +1,19 @@
1
+ require 'minitest/autorun'
2
+ require 'jewelbox/ext/object'
3
+
4
+ class MyTestClass
5
+ end
6
+
7
+ class JewelboxObjectTest < MiniTest::Unit::TestCase
8
+
9
+ def test_eigenclass
10
+ ec = MyTestClass.eigenclass
11
+ ec.send(:define_method, :test1) do |x|
12
+ return x + x
13
+ end
14
+ assert_equal(4, MyTestClass.test1(2))
15
+ end
16
+
17
+ end
18
+
19
+
@@ -0,0 +1,16 @@
1
+ require 'minitest/autorun'
2
+ require 'jewelbox/ext/time'
3
+
4
+ class JewelboxTimeTest < MiniTest::Unit::TestCase
5
+
6
+ def setup
7
+ @t = Time.parse("2012-07-30T18:00:02-07:00")
8
+ end
9
+
10
+ def test_iso_8601
11
+ assert_equal("2012-07-30T18:00:02-0700", @t.iso_8601)
12
+ end
13
+
14
+ end
15
+
16
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jewelbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Steve Jang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-debug19
16
+ requirement: &22659880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *22659880
25
+ description: A set of useful Ruby functions to be used when creating a new service
26
+ or rails application
27
+ email:
28
+ - estebanjang@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - jewelbox.gemspec
39
+ - lib/jewelbox.rb
40
+ - lib/jewelbox/config.rb
41
+ - lib/jewelbox/ext/object.rb
42
+ - lib/jewelbox/ext/time.rb
43
+ - lib/jewelbox/version.rb
44
+ - test/unit/conf/additional.yml
45
+ - test/unit/conf/service.yml
46
+ - test/unit/config_test.rb
47
+ - test/unit/ext/object_test.rb
48
+ - test/unit/ext/time_test.rb
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
62
+ - 0
63
+ hash: -1874784024432894432
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ segments:
71
+ - 0
72
+ hash: -1874784024432894432
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.11
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Contains functions that support service configuration, useful ruby methods
79
+ (such as eigenclass), and other functionality commonly used by Ruby services
80
+ test_files:
81
+ - test/unit/conf/additional.yml
82
+ - test/unit/conf/service.yml
83
+ - test/unit/config_test.rb
84
+ - test/unit/ext/object_test.rb
85
+ - test/unit/ext/time_test.rb