kelredd-pathsconfig 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +48 -0
- data/Rakefile +63 -0
- data/lib/pathsconfig.rb +7 -0
- data/lib/pathsconfig/base.rb +48 -0
- data/lib/pathsconfig/configuration.rb +35 -0
- data/lib/pathsconfig/exceptions.rb +12 -0
- data/lib/pathsconfig/extensions.rb +43 -0
- data/lib/pathsconfig/version.rb +13 -0
- metadata +63 -0
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= Pathsconfig
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
A ruby gem to help configure dynamic file paths for classes in a easy way.
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
sudo gem install kelredd-pathsconfig --source http://gems.github.com
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
# 1. Require the library
|
14
|
+
require 'pathsconfig'
|
15
|
+
|
16
|
+
# 2. Create a paths config file in ./config/paths.yml or ./paths.yml
|
17
|
+
|
18
|
+
# 3. Have a class that needs those paths
|
19
|
+
class Foo
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
# 4. Get sweet methods to generate those paths dynamically!
|
24
|
+
|
25
|
+
== License
|
26
|
+
|
27
|
+
Copyright (c) 2009 Kelly Redding
|
28
|
+
|
29
|
+
Permission is hereby granted, free of charge, to any person
|
30
|
+
obtaining a copy of this software and associated documentation
|
31
|
+
files (the "Software"), to deal in the Software without
|
32
|
+
restriction, including without limitation the rights to use,
|
33
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
34
|
+
copies of the Software, and to permit persons to whom the
|
35
|
+
Software is furnished to do so, subject to the following
|
36
|
+
conditions:
|
37
|
+
|
38
|
+
The above copyright notice and this permission notice shall be
|
39
|
+
included in all copies or substantial portions of the Software.
|
40
|
+
|
41
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
42
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
43
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
44
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
45
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
46
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
47
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
48
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
require 'lib/pathsconfig/version'
|
6
|
+
|
7
|
+
spec = Gem::Specification.new do |s|
|
8
|
+
s.name = 'pathsconfig'
|
9
|
+
s.version = Pathsconfig::Version.to_s
|
10
|
+
s.has_rdoc = true
|
11
|
+
s.extra_rdoc_files = %w(README.rdoc)
|
12
|
+
s.rdoc_options = %w(--main README.rdoc)
|
13
|
+
s.summary = "A ruby gem to help configure dynamic file paths for classes in a easy way."
|
14
|
+
s.author = 'Kelly Redding'
|
15
|
+
s.email = 'kelly@kelredd.com'
|
16
|
+
s.homepage = 'http://code.kelredd.com'
|
17
|
+
s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib}/**/*")
|
18
|
+
# s.executables = ['pathsconfig']
|
19
|
+
|
20
|
+
# s.add_dependency('gem_name', '~> 0.0.1')
|
21
|
+
end
|
22
|
+
|
23
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
24
|
+
pkg.gem_spec = spec
|
25
|
+
end
|
26
|
+
|
27
|
+
Rake::TestTask.new do |t|
|
28
|
+
t.libs << 'test'
|
29
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
30
|
+
t.verbose = true
|
31
|
+
end
|
32
|
+
|
33
|
+
begin
|
34
|
+
require 'rcov/rcovtask'
|
35
|
+
|
36
|
+
Rcov::RcovTask.new(:coverage) do |t|
|
37
|
+
t.libs = ['test']
|
38
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
39
|
+
t.verbose = true
|
40
|
+
t.rcov_opts = ['--text-report', "-x #{Gem.path}", '-x /Library/Ruby', '-x /usr/lib/ruby']
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :coverage
|
44
|
+
|
45
|
+
rescue LoadError
|
46
|
+
warn "\n**** Install rcov (sudo gem install relevance-rcov) to get coverage stats ****\n"
|
47
|
+
task :default => :test
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
desc 'Generate the gemspec to serve this gem'
|
52
|
+
task :gemspec do
|
53
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
54
|
+
File.open(file, 'w') {|f| f << spec.to_ruby }
|
55
|
+
puts "Created gemspec: #{file}"
|
56
|
+
end
|
57
|
+
|
58
|
+
require 'cucumber'
|
59
|
+
require 'cucumber/rake/task'
|
60
|
+
|
61
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
62
|
+
t.cucumber_opts = "test/features --format pretty"
|
63
|
+
end
|
data/lib/pathsconfig.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Pathsconfig
|
2
|
+
module Base
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
module InstanceMethods
|
9
|
+
|
10
|
+
def path_array(path_name)
|
11
|
+
raise Pathsconfig::ConfigError, "Pathsconfig has not been configured." unless Pathsconfig.config
|
12
|
+
raise Pathsconfig::ModelError, "class '#{self.class.name}' does not respond to the 'path_config' method." unless self.class.respond_to?(:path_config)
|
13
|
+
path_value = Pathsconfig.config[self.class.path_config.to_sym][path_name.to_sym]
|
14
|
+
if path_value.kind_of?(Hash)
|
15
|
+
raise Pathsconfig::ModelError, "multiple path types are configured and this model does not respond to the 'path_type' method." unless self.respond_to?(:path_type)
|
16
|
+
raise Pathsconfig::ModelError, "an '#{self.path_type}' type of the '#{path_name}' path has not been configured" if path_value[self.path_type.to_sym].nil?
|
17
|
+
path_value[self.path_type.to_sym].collect{ |exp| eval(exp) }.flatten
|
18
|
+
elsif path_value.kind_of?(Array)
|
19
|
+
path_value.collect{ |exp| eval(exp) }.flatten
|
20
|
+
elsif path_value.blank?
|
21
|
+
raise Pathsconfig::ModelError, "'#{path_name}' is not configured for #{self.class.name}"
|
22
|
+
else
|
23
|
+
raise Pathsconfig::ModelError, "'#{path_name}' is incorrectly configured for #{self.class.name}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def path_string(path_name, options={})
|
28
|
+
options ||= {}
|
29
|
+
options[:type] ||= :system
|
30
|
+
options[:root] ||= Pathsconfig.root
|
31
|
+
|
32
|
+
case options[:type].to_sym
|
33
|
+
when :system
|
34
|
+
File.join((options[:root] + self.path_array(path_name)).compact)
|
35
|
+
else
|
36
|
+
""
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.included(receiver)
|
43
|
+
receiver.extend ClassMethods
|
44
|
+
receiver.send :include, InstanceMethods
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Pathsconfig
|
2
|
+
|
3
|
+
@@config = nil
|
4
|
+
@@root = ""
|
5
|
+
|
6
|
+
def self.config=(path)
|
7
|
+
begin
|
8
|
+
@@config = YAML.load_file(File.expand_path(path))
|
9
|
+
raise Pathsconfig::ConfigError, "Could not load paths configuration file: the config must be entered as a Hash" unless @@config.kind_of?(Hash)
|
10
|
+
@@config.symbolize_keys!
|
11
|
+
rescue Errno::ENOENT => err
|
12
|
+
raise Pathsconfig::ConfigError, "Could not load paths configuration file: #{err.message}"
|
13
|
+
rescue ArgumentError => err
|
14
|
+
raise Pathsconfig::ConfigError, "Could not load paths configuration file: #{err.message}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.config
|
19
|
+
@@config
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.clear
|
23
|
+
@@config = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.root=(path)
|
27
|
+
@@root = path
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.root
|
31
|
+
@@root
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Pathsconfig
|
2
|
+
module Extensions
|
3
|
+
module Hash
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
unless ::Hash.respond_to?('symbolize_keys')
|
8
|
+
# inspired by from ActiveSupport::CoreExtensions::Hash::Keys (http://api.rubyonrails.org/)
|
9
|
+
def symbolize_keys(hash)
|
10
|
+
hash.keys.each{ |key| hash[(key.to_sym rescue key)] ||= hash.delete(key) }
|
11
|
+
hash
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
module InstanceMethods
|
18
|
+
|
19
|
+
unless {}.respond_to?('symbolize_keys')
|
20
|
+
# Return a new hash with all keys converted to strings.
|
21
|
+
def symbolize_keys
|
22
|
+
self.class.symbolize_keys(self.clone)
|
23
|
+
end
|
24
|
+
# Destructively convert all keys to strings.
|
25
|
+
def symbolize_keys!
|
26
|
+
self.class.symbolize_keys(self)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.included(receiver)
|
33
|
+
receiver.extend ClassMethods
|
34
|
+
receiver.send :include, InstanceMethods
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Hash
|
42
|
+
include Pathsconfig::Extensions::Hash
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kelredd-pathsconfig
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kelly Redding
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-30 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: kelly@kelredd.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- lib/pathsconfig
|
28
|
+
- lib/pathsconfig/base.rb
|
29
|
+
- lib/pathsconfig/configuration.rb
|
30
|
+
- lib/pathsconfig/exceptions.rb
|
31
|
+
- lib/pathsconfig/extensions.rb
|
32
|
+
- lib/pathsconfig/version.rb
|
33
|
+
- lib/pathsconfig.rb
|
34
|
+
has_rdoc: false
|
35
|
+
homepage: http://code.kelredd.com
|
36
|
+
licenses:
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --main
|
40
|
+
- README.rdoc
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: A ruby gem to help configure dynamic file paths for classes in a easy way.
|
62
|
+
test_files: []
|
63
|
+
|