singlettings 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +73 -0
  6. data/Rakefile +15 -0
  7. data/lib/singlettings.rb +5 -0
  8. data/lib/singlettings/base.rb +108 -0
  9. data/lib/singlettings/exceptions.rb +5 -0
  10. data/lib/singlettings/rails.rb +56 -0
  11. data/lib/singlettings/version.rb +3 -0
  12. data/myapp/.gitignore +16 -0
  13. data/myapp/Gemfile +47 -0
  14. data/myapp/README.rdoc +28 -0
  15. data/myapp/Rakefile +6 -0
  16. data/myapp/app/assets/images/.keep +0 -0
  17. data/myapp/app/assets/javascripts/application.js +16 -0
  18. data/myapp/app/assets/stylesheets/application.css +13 -0
  19. data/myapp/app/controllers/application_controller.rb +5 -0
  20. data/myapp/app/controllers/concerns/.keep +0 -0
  21. data/myapp/app/helpers/application_helper.rb +2 -0
  22. data/myapp/app/mailers/.keep +0 -0
  23. data/myapp/app/models/.keep +0 -0
  24. data/myapp/app/models/concerns/.keep +0 -0
  25. data/myapp/app/views/layouts/application.html.erb +14 -0
  26. data/myapp/bin/bundle +3 -0
  27. data/myapp/bin/rails +4 -0
  28. data/myapp/bin/rake +4 -0
  29. data/myapp/config.ru +4 -0
  30. data/myapp/config/application.rb +28 -0
  31. data/myapp/config/boot.rb +4 -0
  32. data/myapp/config/database.yml +25 -0
  33. data/myapp/config/environment.rb +5 -0
  34. data/myapp/config/environments/development.rb +29 -0
  35. data/myapp/config/environments/production.rb +80 -0
  36. data/myapp/config/environments/test.rb +36 -0
  37. data/myapp/config/initializers/backtrace_silencers.rb +7 -0
  38. data/myapp/config/initializers/filter_parameter_logging.rb +4 -0
  39. data/myapp/config/initializers/inflections.rb +16 -0
  40. data/myapp/config/initializers/mime_types.rb +5 -0
  41. data/myapp/config/initializers/secret_token.rb +12 -0
  42. data/myapp/config/initializers/session_store.rb +3 -0
  43. data/myapp/config/initializers/wrap_parameters.rb +14 -0
  44. data/myapp/config/locales/en.yml +23 -0
  45. data/myapp/config/routes.rb +56 -0
  46. data/myapp/config/singletting.yml +25 -0
  47. data/myapp/config/singlettings/setting.yml +25 -0
  48. data/myapp/config/yetting.yml +25 -0
  49. data/myapp/db/seeds.rb +7 -0
  50. data/myapp/lib/assets/.keep +0 -0
  51. data/myapp/lib/tasks/.keep +0 -0
  52. data/myapp/log/.keep +0 -0
  53. data/myapp/public/404.html +58 -0
  54. data/myapp/public/422.html +58 -0
  55. data/myapp/public/500.html +57 -0
  56. data/myapp/public/favicon.ico +0 -0
  57. data/myapp/public/robots.txt +5 -0
  58. data/myapp/vendor/assets/javascripts/.keep +0 -0
  59. data/myapp/vendor/assets/stylesheets/.keep +0 -0
  60. data/singlettings.gemspec +24 -0
  61. data/spec/sample.yml +39 -0
  62. data/spec/singletting_spec.rb +79 -0
  63. data/spec/spec_helper.rb +11 -0
  64. metadata +151 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61a5391f730590158f913a9e2b7143fcb9985408
4
+ data.tar.gz: 20c11138eb0c8873d266b508bf0dc0f35c66e0ab
5
+ SHA512:
6
+ metadata.gz: aebf3b18a23d2f863e5e9f799da83027e423712afa17dff8e7f6a581eacf76d83b92f19a7d0161366e6c4c287de8a6d87bcb2fb909d0630c9a69ba6625b8d6b1
7
+ data.tar.gz: f079732f7edb8e2d8d4d9bd47a458377da26c7c4e0c7288abb93e830a94b436f51bdbb7b49b6f856d9b12931dc325049160c90f7e5cb37fb2a916131da6366da
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in singlettings.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jingkai He
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,73 @@
1
+ # Singlettings
2
+
3
+ A simple YML to singleton class solution for ruby programming language. It can be seamless integrated with Rails, Rack, sinatra and any other ruby apps.
4
+
5
+ It greatly inspired from [Yettings](https://github.com/charlotte-ruby/yettings).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'singlettings', git: "git@github.com:mycolorway/singlettings.git"
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install singlettings
20
+
21
+ ## Usage
22
+
23
+ ### Convention Usage for Rails App.
24
+
25
+ - Create the yaml configuration file named 'singletting.yml' or 'yetting.yml' in 'config' directory. Then you can invoke it by calling ```Singletting.balh_blah_blah``` or ```Yetting.balh_blah_blah```
26
+
27
+ - Also you can put the configuration files in the 'config/singlettings' directory, the singleton classes will be mechanically generated as described above.
28
+
29
+ ### Convention for other apps, such as rack & sinatra
30
+
31
+ - Place the configuration files anywhere you want. If the YAML file is named 'Setting', then:
32
+
33
+ ```(ruby)
34
+ class Setting < Singlettings::Base
35
+ source "#{dir_to_config_file}"
36
+ ns :your_namespace # Well, a bit clojure flavour...
37
+ end
38
+ ```
39
+
40
+ - Or Just in the lazy way, which will read the setting.yml file in the root directory:
41
+ ```(ruby)
42
+ class Setting < Singlettings::Base
43
+ end
44
+ ```
45
+
46
+ - Last but not least, you can use it as an global variable by calling:
47
+ ```(ruby)
48
+ SETTING = Singlettings::Base.new("config/settings.yml", "development")
49
+ ```
50
+
51
+ ### Operations on the Singletting Object
52
+
53
+
54
+ You can invoke the object by calling as a hash:
55
+ ```(ruby)
56
+ Setting["key1"] # => value1
57
+ Setting["key1"]["key2"] # => value2
58
+ Setting[:key1][:key2][:key3] # => value3
59
+ ```
60
+
61
+ Also by calling:
62
+ ```(ruby)
63
+ Setting.key1 # => value1
64
+ Setting.key1.key2 # => value2
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run an IRB session with Singlettings preloaded"
5
+ task :console do
6
+ exec "irb -I lib -r singlettings"
7
+ end
8
+
9
+ desc "Run the test suite"
10
+ RSpec::Core::RakeTask.new(:rspec) do |t|
11
+ t.pattern = FileList['spec/**/*_spec.rb']
12
+ t.rspec_opts = %w|--color --format doc|
13
+ end
14
+
15
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ module Singlettings
2
+ require_relative "singlettings/version"
3
+ require_relative "singlettings/base"
4
+ require_relative "singlettings/rails"
5
+ end
@@ -0,0 +1,108 @@
1
+ require 'singlettings/exceptions'
2
+ require 'yaml'
3
+
4
+ require 'erb'
5
+ module Singlettings
6
+ class Base < Hash
7
+ attr_reader :source, :ns
8
+ class << self
9
+ # Basic Usage of source in class
10
+ # source "config/setting.yml"
11
+ # source
12
+ def source(directory = nil)
13
+ filename = directory ? directory : "#{self.ancestors[0].to_s.downcase}.yml"
14
+ @erb_content = ERB.new(File.read(filename)).result # Set it in memory
15
+ @source = @erb_content ? YAML.load(@erb_content) : {}
16
+ end
17
+
18
+ # Set the default namespace
19
+ # Basic Usage of ns in class:
20
+ # ns :development
21
+ # ns Rails.env
22
+ def ns(value = nil)
23
+ @ns = value.to_s if value
24
+ end
25
+
26
+ # Override the hash method
27
+ def [](key)
28
+ value = current_branch.fetch key.to_s, nil
29
+ if value.is_a? Hash
30
+ self.new value
31
+ else
32
+ value
33
+ end
34
+ end
35
+
36
+ def current_branch
37
+ source unless @source
38
+ return @source unless @ns
39
+ if @source[@ns] == nil
40
+ raise NoSuchNamespaceError, "Source is empty. The namespace does not exist"
41
+ else
42
+ @source[@ns]
43
+ end
44
+ end
45
+
46
+ def method_missing(method, *args, &block)
47
+ key = method.to_s
48
+ if current_branch.keys.include? key
49
+ value = current_branch[key]
50
+ if value.is_a? Hash
51
+ self.new value
52
+ else
53
+ value
54
+ end
55
+ else
56
+ raise NoSuchKeyError, "#{key} does not exist"
57
+ end
58
+ end
59
+ end # class self
60
+
61
+ def initialize(directory_or_hash, default_namespace= nil)
62
+ @ns = self.class.ns default_namespace
63
+
64
+ raise FileNotSpecifiedError, "You should choose a file to initialize" unless directory_or_hash
65
+
66
+ if directory_or_hash.is_a? Hash
67
+ @source = directory_or_hash
68
+ else
69
+ @source = self.class.source directory_or_hash
70
+ end
71
+
72
+ self.replace current_branch
73
+ end
74
+
75
+ def current_branch
76
+ return source unless ns
77
+ if source[ns] == nil
78
+ raise NoSuchNamespaceError, "Source is empty. The namespace does not exist"
79
+ else
80
+ source[ns]
81
+ end
82
+ end
83
+
84
+ def [](key)
85
+ value = current_branch.fetch key.to_s, nil
86
+ if value.is_a? Hash
87
+ self.class.new value
88
+ else
89
+ value
90
+ end
91
+ end
92
+
93
+ def method_missing(method, *args, &block)
94
+ key = method.to_s
95
+ if current_branch.keys.include? key
96
+ value = current_branch[key]
97
+ if value.is_a? Hash
98
+ self.class.new value
99
+ else
100
+ value
101
+ end
102
+ else
103
+ raise NoSuchKeyError, "#{key} does not exist"
104
+ end
105
+ end
106
+ end
107
+
108
+ end
@@ -0,0 +1,5 @@
1
+ module Singlettings
2
+ class NoSuchKeyError < StandardError; end
3
+ class NoSuchNamespaceError < StandardError; end
4
+ class FileNotSpecifiedError < StandardError; end
5
+ end
@@ -0,0 +1,56 @@
1
+ require "singlettings/base"
2
+
3
+ module Singlettings
4
+ def self.hook_rails!
5
+ if defined?(::Rails)
6
+ config_path = "#{::Rails.root.to_s}/config/"
7
+ Singlettings.load_yaml_files! config_path
8
+ end
9
+ end
10
+
11
+ class Rails < ::Rails::Engine
12
+ initializer 'singlettings' do
13
+ Singlettings.hook_rails!
14
+ end
15
+ end if defined?(::Rails)
16
+
17
+ private
18
+ def self.load_yaml_files!(load_path)
19
+ # Load singletting.yml
20
+ files = []
21
+
22
+ singletting = "#{load_path}singletting.yml"
23
+ files << singletting if File.exists?(singletting)
24
+
25
+ # Add MyColorWay Flavoured yetting.yml
26
+ yetting = "#{load_path}yetting.yml"
27
+ files << yetting if File.exists? yetting
28
+
29
+ # Add namespaced files
30
+ files += Dir.glob("#{load_path}singlettings/**/*.yml")
31
+
32
+ files.each do |file|
33
+ Singlettings.eval_yaml file
34
+ end
35
+ end
36
+
37
+ # This method needs ActiveSupport
38
+ def self.eval_yaml(file)
39
+ base_name = File.basename(file).gsub(".yml", "").camelize
40
+ if base_name == "Singletting" or base_name == "Yetting"
41
+ klass_name = base_name
42
+ else
43
+ klass_name = "Singletting#{base_name}"
44
+ end
45
+ Object.const_set(klass_name, Singlettings.eval_yaml_class(file))
46
+ end
47
+
48
+ # Return specified anonymous class
49
+ def self.eval_yaml_class(file)
50
+ klass = Class.new(Singlettings::Base) do
51
+ source file
52
+ ns ::Rails.env
53
+ end
54
+ return klass
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Singlettings
2
+ VERSION = "0.0.2"
3
+ end
data/myapp/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
data/myapp/Gemfile ADDED
@@ -0,0 +1,47 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
4
+ gem 'rails', '4.0.0'
5
+
6
+ # Use sqlite3 as the database for Active Record
7
+ gem 'sqlite3'
8
+
9
+ # Use SCSS for stylesheets
10
+ gem 'sass-rails', '~> 4.0.0'
11
+
12
+ # Use Uglifier as compressor for JavaScript assets
13
+ gem 'uglifier', '>= 1.3.0'
14
+
15
+ # Use CoffeeScript for .js.coffee assets and views
16
+ gem 'coffee-rails', '~> 4.0.0'
17
+
18
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
19
+ # gem 'therubyracer', platforms: :ruby
20
+
21
+ # Use jquery as the JavaScript library
22
+ gem 'jquery-rails'
23
+
24
+ # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
25
+ gem 'turbolinks'
26
+
27
+ # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
28
+ gem 'jbuilder', '~> 1.2'
29
+
30
+ # gem 'singlettings', path: '..'
31
+ gem 'singlettings', path: ".."
32
+ group :doc do
33
+ # bundle exec rake doc:rails generates the API under doc/api.
34
+ gem 'sdoc', require: false
35
+ end
36
+
37
+ # Use ActiveModel has_secure_password
38
+ # gem 'bcrypt-ruby', '~> 3.0.0'
39
+
40
+ # Use unicorn as the app server
41
+ # gem 'unicorn'
42
+
43
+ # Use Capistrano for deployment
44
+ # gem 'capistrano', group: :development
45
+
46
+ # Use debugger
47
+ # gem 'debugger', group: [:development, :test]
data/myapp/README.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
data/myapp/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Myapp::Application.load_tasks
File without changes
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .