config_file 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/config_file.gemspec +2 -0
- data/lib/config_file.rb +8 -33
- data/lib/config_file/file.rb +32 -0
- data/lib/config_file/include.rb +37 -0
- data/lib/config_file/version.rb +2 -4
- data/spec/config/base.yml +2 -0
- data/spec/config/include_test.yaml +2 -0
- data/spec/config_file_spec.rb +10 -0
- data/spec/include_spec.rb +21 -0
- data/spec/spec_helper.rb +18 -0
- metadata +29 -5
data/.rspec
ADDED
data/config_file.gemspec
CHANGED
data/lib/config_file.rb
CHANGED
@@ -2,39 +2,14 @@ require "config_file/version"
|
|
2
2
|
require 'yaml'
|
3
3
|
require 'pathname'
|
4
4
|
require 'erb'
|
5
|
+
require 'blankslate'
|
5
6
|
|
6
|
-
|
7
|
+
require 'config_file/file'
|
8
|
+
require 'config_file/include'
|
9
|
+
|
10
|
+
module ConfigFile
|
7
11
|
class UnknownConfigFile < ArgumentError; end
|
8
12
|
class UnknownKey < StandardError; end
|
9
|
-
attr_writer :raise_on_missing_key
|
10
|
-
|
11
|
-
def initialize file
|
12
|
-
@file = file
|
13
|
-
@raise_on_missing_key = false
|
14
|
-
end
|
15
|
-
|
16
|
-
def method_missing(key,*args)
|
17
|
-
@data = nil if ConfigFile.dev?
|
18
|
-
if data.has_key?(key)
|
19
|
-
data[key]
|
20
|
-
elsif data.has_key?(key.to_s)
|
21
|
-
data[key.to_s]
|
22
|
-
else
|
23
|
-
@raise_on_missing_key ? raise(UnknownKey, "#{key} couldn't be found in #{@file}") : nil
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def __data__
|
28
|
-
@data ||= begin
|
29
|
-
d = nil
|
30
|
-
File.open(@file) do |f|
|
31
|
-
d = YAML.load ERB.new(f.read).result
|
32
|
-
end
|
33
|
-
d = d[ConfigFile.env] if d.has_key? ConfigFile.env
|
34
|
-
d
|
35
|
-
end
|
36
|
-
end
|
37
|
-
alias data __data__
|
38
13
|
|
39
14
|
ALL = {}
|
40
15
|
class << self
|
@@ -55,7 +30,7 @@ class ConfigFile < BlankSlate
|
|
55
30
|
when defined?(Rails)
|
56
31
|
Rails.root
|
57
32
|
else
|
58
|
-
Pathname.new(File.expand_path('.'))
|
33
|
+
::Pathname.new(File.expand_path('.'))
|
59
34
|
end
|
60
35
|
end
|
61
36
|
|
@@ -69,7 +44,7 @@ class ConfigFile < BlankSlate
|
|
69
44
|
|
70
45
|
def method_missing(name,*args)
|
71
46
|
ALL[name] ||= if file = find_file(name)
|
72
|
-
new(file)
|
47
|
+
File.new(file)
|
73
48
|
else
|
74
49
|
raise UnknownConfigFile, "couldn't find file at 'config/#{name}.yml'"
|
75
50
|
end
|
@@ -84,7 +59,7 @@ class ConfigFile < BlankSlate
|
|
84
59
|
files << root+"#{dir}/#{name}.yaml"
|
85
60
|
end
|
86
61
|
files.find do |f|
|
87
|
-
File.exist?(f)
|
62
|
+
::File.exist?(f)
|
88
63
|
end
|
89
64
|
end
|
90
65
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module ConfigFile
|
2
|
+
class File < BlankSlate
|
3
|
+
attr_writer :raise_on_missing_key
|
4
|
+
|
5
|
+
def initialize file
|
6
|
+
@file = file
|
7
|
+
@raise_on_missing_key = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(key,*args)
|
11
|
+
@data = nil if ConfigFile.dev?
|
12
|
+
if val = (data[key] || data[key.to_s])
|
13
|
+
val
|
14
|
+
else
|
15
|
+
@raise_on_missing_key ? raise(UnknownKey, "#{key} couldn't be found in #{@file}") : nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def __data__
|
20
|
+
@data ||= begin
|
21
|
+
d = nil
|
22
|
+
::File.open(@file) do |f|
|
23
|
+
d = YAML.load ERB.new(f.read).result
|
24
|
+
end
|
25
|
+
d = d[ConfigFile.env] if d.has_key? ConfigFile.env
|
26
|
+
d
|
27
|
+
end
|
28
|
+
end
|
29
|
+
alias data __data__
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module ConfigFile
|
2
|
+
module ClassMethods
|
3
|
+
attr_reader :config_file
|
4
|
+
|
5
|
+
def config_file_name=file
|
6
|
+
@config_file = ConfigFile.send(file)
|
7
|
+
end
|
8
|
+
|
9
|
+
def __setup_config_methods__
|
10
|
+
@config_file.__data__.each do |k,v|
|
11
|
+
instance_eval <<-RUBY
|
12
|
+
def #{k}
|
13
|
+
@config_file.#{k}
|
14
|
+
end
|
15
|
+
RUBY
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
module InstanceMethods
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.included(receiver)
|
25
|
+
word = receiver.to_s
|
26
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
27
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
28
|
+
word.tr!("-", "_")
|
29
|
+
word.downcase!
|
30
|
+
|
31
|
+
receiver.extend ClassMethods
|
32
|
+
receiver.send :include, InstanceMethods
|
33
|
+
|
34
|
+
receiver.config_file_name = word
|
35
|
+
receiver.__setup_config_methods__
|
36
|
+
end
|
37
|
+
end
|
data/lib/config_file/version.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module IncludeTest
|
4
|
+
include ConfigFile
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "ConfigFile" do
|
8
|
+
it "should look for include_test.yml" do
|
9
|
+
IncludeTest.config_file.should_not be_a(ConfigFile::File)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should define methods for each key" do
|
13
|
+
IncludeTest.should respond_to(:key1)
|
14
|
+
IncludeTest.should respond_to(:key2)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return expected value" do
|
18
|
+
IncludeTest.key1.should == 'key1'
|
19
|
+
IncludeTest.key2.should == 'key2'
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
# f = File.expand_path('../../lib',__FILE__)
|
8
|
+
# $:.unshift(f) unless $:.include?(f)
|
9
|
+
|
10
|
+
require File.expand_path('lib/config_file')
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
14
|
+
config.run_all_when_everything_filtered = true
|
15
|
+
config.filter_run :focus
|
16
|
+
end
|
17
|
+
|
18
|
+
ConfigFile.root = Pathname.new(File.expand_path('..',__FILE__))
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_file
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: blankslate
|
16
|
-
requirement: &
|
16
|
+
requirement: &70167225021520 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,18 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70167225021520
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70167225020880 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70167225020880
|
25
36
|
description: Gem for quickly reading from config files
|
26
37
|
email:
|
27
38
|
- me@tal.by
|
@@ -30,13 +41,21 @@ extensions: []
|
|
30
41
|
extra_rdoc_files: []
|
31
42
|
files:
|
32
43
|
- .gitignore
|
44
|
+
- .rspec
|
33
45
|
- Gemfile
|
34
46
|
- LICENSE
|
35
47
|
- README.md
|
36
48
|
- Rakefile
|
37
49
|
- config_file.gemspec
|
38
50
|
- lib/config_file.rb
|
51
|
+
- lib/config_file/file.rb
|
52
|
+
- lib/config_file/include.rb
|
39
53
|
- lib/config_file/version.rb
|
54
|
+
- spec/config/base.yml
|
55
|
+
- spec/config/include_test.yaml
|
56
|
+
- spec/config_file_spec.rb
|
57
|
+
- spec/include_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
40
59
|
homepage: http://github.com/talby/config_file
|
41
60
|
licenses: []
|
42
61
|
post_install_message:
|
@@ -61,4 +80,9 @@ rubygems_version: 1.8.10
|
|
61
80
|
signing_key:
|
62
81
|
specification_version: 3
|
63
82
|
summary: Gem for quickly reading from config files
|
64
|
-
test_files:
|
83
|
+
test_files:
|
84
|
+
- spec/config/base.yml
|
85
|
+
- spec/config/include_test.yaml
|
86
|
+
- spec/config_file_spec.rb
|
87
|
+
- spec/include_spec.rb
|
88
|
+
- spec/spec_helper.rb
|