ablerc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
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
+ .ablerc
19
+ able.rc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ablerc.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Spencer Markowski
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.
@@ -0,0 +1,121 @@
1
+ ablerc
2
+ ======
3
+
4
+ Add "rc" capabilities into your ruby libraries
5
+
6
+ Installation
7
+ ------
8
+
9
+ ```ruby
10
+ gem 'ablerc'
11
+ ```
12
+
13
+ Usage
14
+ ------
15
+
16
+ Add an ablerc.rb file to the root of your project.
17
+
18
+ If you use the option DSL you will be able to generate stub rc files and validate option values. However, you're free to
19
+ simply provide an `rc_file_name` and any option defined in that rc file will be available to you.
20
+
21
+ The scheme will control how your application's options are loaded and in what order.
22
+
23
+ ```ruby
24
+ Ablerc.setup do
25
+ # The name of the file to be used for the configuration of this app
26
+ # Include a dot if you expect hidden file behavior.
27
+ rc_file_name = '.ablerc'
28
+
29
+ # Describes the order in which configuration files are loaded.
30
+ # Default is <tt>:global</tt>, <tt>:user</tt>, <tt>:local</tt> which
31
+ # will search for <tt>rc_file_name</tt> in these directories:
32
+ # /etc/.ablerc
33
+ # ~/.ablerc
34
+ # ./.ablerc
35
+ #
36
+ # Configuration options cascade and override previously loaded options.
37
+ scheme :global, :user, :local
38
+
39
+ # Describe the options available
40
+ #
41
+ # ==== Parameters
42
+ # * <tt>name</tt> - A valid name for the option
43
+ # * <tt>behaviors</tt> - Behaviors used to for this option
44
+ # * <tt>block</tt> - A proc that should be run against the option value.
45
+ # ==== Options
46
+ # * <tt>allow</tt> - The option value must be in this list
47
+ # * <tt>boolean</tt> - The option will accept <tt>true</tt>, <tt>false</tt>, <tt>0</tt>, <tt>1</tt>
48
+ option :username, :description => "Your github username", :disabled => true
49
+ option :color, :allow => [:red, :green, :blue], :default => :red
50
+ option :save_on_exit, :boolean
51
+ option :seven_digit_number do |number|
52
+ number =~ ^\d{7}$
53
+ end
54
+ option :memory_limit, :default => '10G', :description => 'If memory usage exceeds 10G the internet reboots'
55
+ end
56
+ ```
57
+
58
+ Accessing Configuration Options
59
+ ----------
60
+ Options defined in the rc file are accessed through the `#configuration` method. All uncommented key-pair values within the rc file are parsed and made available through `#configuration` (or the alias `#config`), regardless of if you're using
61
+ the options DSL to define specific options.
62
+
63
+
64
+ ```ruby
65
+ Ablerc.configuration.username
66
+ #=> "esmarkowski"
67
+
68
+ Ablerc.configuration
69
+ #=> {username: 'esmarkowski', color: 'red', save_on_exit: true, seven_digit_number: '1234567'}
70
+ ```
71
+
72
+ Options DSL
73
+ ----------
74
+
75
+ ### option(:name, behaviors = {})
76
+
77
+ | Option Behaviors | Description |
78
+ | ------------ | ------------ |
79
+ | `:allow` | An array of valid values. |
80
+ | `:refuse` | An array of invalid values. |
81
+ | `:boolean` | Only allows option to accept true, false, 1 or 0 |
82
+ | `:default` | The default value of this option |
83
+ | `:description` | Printed above the option as a comment |
84
+ | `:disabled` | Controls if option is commented out in rc file |
85
+
86
+
87
+ Providing Stub Configuration Files
88
+ ----------
89
+ If you're using the option DSL you can expose `Ablerc.stub.generate` via
90
+ a rake task. `Ablerc.stub.generate`
91
+ accepts an optional scheme argument if you wish to place the rc file in a specific context.
92
+
93
+ ```ruby
94
+ namespace :your_app do
95
+ task :stub
96
+ Ablerc.stub.generate :local
97
+ end
98
+ end
99
+ ```
100
+
101
+ Running this task will place your rc file in the local context (`./.ablerc`) and describe available options.
102
+
103
+ ```conf
104
+ # Your github username.
105
+ #username = ''
106
+
107
+ # color accepts :red, :blue or :green
108
+ #color = 'red'
109
+
110
+ # save_on_exit accepts true, false, 1 or 0
111
+ #save_on_exit = false
112
+
113
+ #seven_digit_number = ''
114
+
115
+ # If memory usage exceeds 10G the internet reboots
116
+ memory_limit = 10G
117
+ ```
118
+
119
+ License
120
+ -------
121
+ MIT License. Copyright 2012 The Able Few, LLC. http://theablefew.com
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ablerc/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ablerc"
8
+ gem.version = Ablerc::VERSION
9
+ gem.authors = ["Spencer Markowski"]
10
+ gem.email = ["spencer@theablefew.com"]
11
+ gem.description = %q{Provides cascading configuration files and helpers for generating configuration stubs.}
12
+ gem.summary = %q{Quickly add "rc" capabilities into your ruby applications.}
13
+ gem.homepage = "http://github.com/theablefew/ablerc"
14
+
15
+ gem.add_dependency "hashie"
16
+ gem.add_dependency "rainbow"
17
+ gem.add_dependency "activesupport"
18
+ #gem.add_development_dependency ""
19
+
20
+ gem.files = `git ls-files`.split($/)
21
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
+ gem.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,97 @@
1
+ require "ablerc/version"
2
+ require "active_support/core_ext"
3
+ require "rainbow"
4
+
5
+ module Ablerc
6
+ autoload :Option, 'ablerc/option'
7
+ autoload :DSL, 'ablerc/dsl'
8
+ autoload :Context, 'ablerc/context'
9
+ autoload :Configuration, 'ablerc/configuration'
10
+ autoload :StubGenerator, 'ablerc/stub_generator'
11
+ autoload :Errors, 'ablerc/errors'
12
+
13
+
14
+ ABLE_RC_FILE = 'able.rc'
15
+
16
+
17
+ mattr_accessor :scheme
18
+ self.scheme = []
19
+
20
+
21
+ mattr_accessor :options
22
+ self.options = []
23
+
24
+
25
+ mattr_accessor :rc_file_name
26
+ self.rc_file_name = ''
27
+
28
+
29
+ mattr_accessor :dsl
30
+ self.dsl = Ablerc::DSL.new
31
+
32
+
33
+ mattr_accessor :contexts
34
+ self.contexts = Ablerc::Context
35
+
36
+
37
+ mattr_accessor :stub_options
38
+ self.stub_options = {}
39
+
40
+
41
+ class << self
42
+
43
+
44
+ # Iniatializes Ablerc with values from DSL
45
+ def setup(&block)
46
+ Ablerc.dsl.instance_eval(&block)
47
+ scheme.each { |c| dsl.context(c, Ablerc::Context::DEFAULTS[c]) unless contexts.exists? c}
48
+ end
49
+
50
+
51
+ # Exposes option values from parsed rc files.
52
+ # Aliased as <tt>#config</tt>
53
+ def configuration
54
+ Ablerc::Configuration.instance
55
+ end
56
+
57
+
58
+ alias :config :configuration
59
+
60
+
61
+ # Prepares a stub rcfile with defined options
62
+ def stub
63
+ Ablerc::StubGenerator.new({:options => options}.merge(stub_options))
64
+ end
65
+
66
+
67
+ # Loads the rc files in the order and locations specified by scheme
68
+ def load_scheme
69
+ raise RcFileMissing, "You must provide a value to rc_file_name" if rc_file_name.blank?
70
+ self.scheme.each do |scheme|
71
+ configuration.load File.expand_path(File.join( contexts[scheme].path, rc_file_name))
72
+ end
73
+ end
74
+
75
+ def load!(path)
76
+ load_able_rc! File.expand_path( File.join( path, ABLE_RC_FILE))
77
+ end
78
+
79
+ private
80
+
81
+
82
+ def gem_root
83
+ File.expand_path '../..', __FILE__
84
+ end
85
+
86
+
87
+ def load_able_rc!(path)
88
+ instance_eval(File.read( path ))
89
+ load_scheme
90
+ return configuration
91
+ end
92
+ end
93
+
94
+ # Immediatly load options and rc file configurations
95
+ #load_able_rc!
96
+
97
+ end
@@ -0,0 +1,43 @@
1
+ require 'hashie/mash'
2
+ require 'singleton'
3
+
4
+ module Ablerc
5
+ # Represents an rc file which consists
6
+ # of a series of key value pairs
7
+ class Configuration
8
+ include Singleton
9
+ attr_accessor :store
10
+
11
+
12
+ class << self
13
+ def load(path); self.instance.load(path); end
14
+ end
15
+
16
+
17
+ def read_values(&rc_file)
18
+ rc_file.call
19
+ yield(self)
20
+ end
21
+
22
+
23
+ def initialize
24
+ @store ||= Hashie::Mash.new
25
+ end
26
+
27
+
28
+ def method_missing(method, *args, &block)
29
+ store.send(method, *args, &block)
30
+ end
31
+
32
+
33
+ def load(path)
34
+ return unless File.exists? path
35
+ self.instance_eval do
36
+ File.read(File.expand_path(path)).each_line do |line|
37
+ next if line =~ /^[?\s]*\#/ || line.blank?
38
+ store.instance_eval("self."+line)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,53 @@
1
+ require 'hashie/mash'
2
+ module Ablerc
3
+ class Context
4
+
5
+ DEFAULTS = {global: {path: '/etc/'},
6
+ user: {path: '~/'},
7
+ local: {path: './'}}
8
+
9
+
10
+ attr_accessor :path, :name
11
+
12
+
13
+ def initialize(name, options={})
14
+ @name = name
15
+ @path = options.delete(:path)
16
+ Ablerc::Context << self
17
+ end
18
+
19
+
20
+ class << self
21
+
22
+ @@contexts = Hashie::Mash.new
23
+
24
+
25
+ def <<(context)
26
+ @@contexts.merge!(context.name => context)
27
+ end
28
+
29
+
30
+ def paths
31
+ @@contexts.collect { |name, context| context.path }
32
+ end
33
+
34
+
35
+ def list
36
+ @@contexts
37
+ end
38
+
39
+ def names
40
+ @@contexts.keys
41
+ end
42
+
43
+
44
+ def exists?(context)
45
+ @@contexts.has_key? context
46
+ end
47
+
48
+ def method_missing(method_name, *args, &block)
49
+ @@contexts.send(method_name, *args, &block) || super
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,61 @@
1
+ module Ablerc
2
+ class DSL
3
+
4
+
5
+ # The name of the file to be used for the configuration of this app
6
+ # Include a dot if you expect hidden file behavior.
7
+ def rc_file_name(file_name)
8
+ Ablerc.rc_file_name = file_name
9
+ end
10
+
11
+
12
+ # Describes the order in which configuration files are loaded.
13
+ # Default is <tt>:global</tt>, <tt>:user</tt>, <tt>:local</tt> which
14
+ # will search for <tt>rc_file_name</tt> in these directories:
15
+ # /etc/.ablerc
16
+ # ~/.ablerc
17
+ # ./.ablerc
18
+ #
19
+ # Configuration options cascade and override previously loaded options.
20
+ # scheme :global, :user, :local
21
+ def scheme(*contexts)
22
+ Ablerc.scheme = contexts
23
+ end
24
+
25
+
26
+ # Describe the options available
27
+ #
28
+ # ==== Parameters
29
+ # * <tt>name</tt> - A valid name for the option
30
+ # * <tt>behaviors</tt> - Behaviors used to for this option
31
+ # * <tt>block</tt> - A proc that should be run against the option value.
32
+ # ==== Options
33
+ # * <tt>allow</tt> - The option value must be in this list
34
+ # * <tt>boolean</tt> - The option will accept <tt>true</tt>, <tt>false</tt>, <tt>0</tt>, <tt>1</tt>
35
+ def option(name, behaviors = {}, &block)
36
+ Ablerc.options << Ablerc::Option.new(name, behaviors, &block)
37
+ end
38
+
39
+
40
+ def context(name, options)
41
+ Ablerc::Context.new(name, options)
42
+ end
43
+
44
+ # Configures stub options for #generate
45
+ #
46
+ # ==== Options
47
+ # * <tt>header</tt> - Text to be included at the beginning of the rc file
48
+ # * <tt>footer</tt> - Text to be included at the end of the rc file
49
+ def stub(options)
50
+ Ablerc.stub_options = options
51
+ end
52
+
53
+
54
+ def method_missing(method_name, *args, &block)
55
+ raise "You tried to call the method #{method_name}. There is no such method."
56
+ end
57
+
58
+
59
+ def respond_to? *args; super; end
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ module Ablerc
2
+ class RcFileMissing < Exception; end
3
+ class AbleRCConfigMissing < Exception; end
4
+ end
@@ -0,0 +1,77 @@
1
+ require 'hashie/dash'
2
+ module Ablerc
3
+
4
+
5
+ # An option is a declared attribute within an rc file,
6
+ # in other words, an uncommented key-value pair.
7
+ #
8
+ # Options define how an attribute behaves (via behavior
9
+ # parameters), which are used to validate, explain and
10
+ # control default values for the option.
11
+ class Option < Hashie::Dash
12
+
13
+
14
+ property :name, :required => true
15
+ attr_accessor :allows, :refuses, :boolean, :description, :default, :disabled
16
+
17
+ # Initialize the option
18
+ #
19
+ # ==== Parameters
20
+ # * <tt>name</tt> - A valid name for the option
21
+ # * <tt>behaviors</tt> - Behaviors used to for this option
22
+ # * <tt>block</tt> - A proc that should be run against the option value.
23
+ # ==== Options
24
+ # * <tt>allow</tt> - The option value must be in this list
25
+ # * <tt>boolean</tt> - The option will accept <tt>true</tt>, <tt>false</tt>, <tt>0</tt>, <tt>1</tt>
26
+ def initialize(name, behaviors = {}, &block)
27
+ self.name = name
28
+ extract_behaviors( behaviors )
29
+ end
30
+
31
+
32
+ def to_stub
33
+ stub = "## #{name}\n"
34
+ stub << "# #{description}\n" unless description.nil?
35
+ stub << "#{entry_for_refuse_allow_behavior}\n" unless refuses.nil? and allows.nil?
36
+ stub << "#{entry_for_key_value}\n"
37
+ stub << "\n"
38
+ end
39
+
40
+ private
41
+
42
+ def entry_for_key_value
43
+ line = ""
44
+ line << "# Default: #{default}\n" unless default.nil?
45
+ line << "#" if disabled
46
+ line << "#{name} = "
47
+ line << default unless default.nil?
48
+ line
49
+ end
50
+
51
+
52
+ def entry_for_boolean
53
+ line = ""
54
+ line << "# Accepts boolean values of true, false, 0 or 1" unless boolean.nil?
55
+ line
56
+ end
57
+
58
+
59
+ def entry_for_refuse_allow_behavior
60
+ line = ""
61
+ line << "# Allows \n# #{allows.join(',')}" unless allows.nil?
62
+ line << "# Refuses \n# #{refuses.join(',')}" unless refuses.nil?
63
+ line
64
+ end
65
+
66
+ def extract_behaviors(behaviors)
67
+ behaviors.tap do |b|
68
+ self.allows = b.delete :allow
69
+ self.refuses = b.delete :refuses
70
+ self.boolean = b.delete :boolean
71
+ self.description = b.delete :description
72
+ self.default = b.delete :default
73
+ self.disabled = b.delete(:disabled) || true
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,44 @@
1
+ module Ablerc
2
+ class StubGenerator
3
+
4
+ attr_accessor :options, :path, :header, :footer
5
+
6
+
7
+ def initialize(options)
8
+ self.header = options.delete :header
9
+ self.footer = options.delete :footer
10
+ self.options = options.delete :options
11
+ end
12
+
13
+
14
+ def generate(context = nil)
15
+ begin
16
+ context ||= Ablerc.contexts.names.last.to_sym
17
+ path = File.expand_path(Ablerc.contexts.send(context.to_sym).path)
18
+ rescue
19
+ raise "Context does not exist"
20
+ end
21
+
22
+ puts "Generating Stub for #{context} in #{path + '/' + Ablerc.rc_file_name}".color :green
23
+
24
+ begin
25
+ File.open(path + '/' + Ablerc.rc_file_name, 'w') do |rc_file|
26
+ rc_file << header.to_s unless header.nil?
27
+ self.options.each do |option|
28
+ rc_file << option.to_stub
29
+ end
30
+ rc_file << footer.to_s unless footer.nil?
31
+ end
32
+ rescue e
33
+ puts "Error".color :red
34
+ puts e
35
+ end
36
+
37
+ true
38
+ end
39
+
40
+ def write_description(description)
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Ablerc
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ablerc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spencer Markowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rainbow
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Provides cascading configuration files and helpers for generating configuration
63
+ stubs.
64
+ email:
65
+ - spencer@theablefew.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - ablerc.gemspec
76
+ - lib/ablerc.rb
77
+ - lib/ablerc/configuration.rb
78
+ - lib/ablerc/context.rb
79
+ - lib/ablerc/dsl.rb
80
+ - lib/ablerc/errors.rb
81
+ - lib/ablerc/option.rb
82
+ - lib/ablerc/stub_generator.rb
83
+ - lib/ablerc/version.rb
84
+ homepage: http://github.com/theablefew/ablerc
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.24
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Quickly add "rc" capabilities into your ruby applications.
108
+ test_files: []