rails_config_loader 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,7 +31,7 @@ module MainApp
31
31
 
32
32
  # load json
33
33
  def addresses locale = :da
34
- @config ||= load 'data/addresses.json', :locale => get_locale(locale)
34
+ @config ||= load_content '/data/addresses.json', :locale => get_locale(locale)
35
35
  end
36
36
 
37
37
  def payment_provider
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.4
@@ -8,17 +8,24 @@ module ConfigLoader
8
8
  def initialize file_path, options = {}
9
9
  @path = File.dirname file_path
10
10
  @file_name = File.basename file_path
11
- parts = file_name.split(/(ya?ml$)/)
12
- name = parts.first
11
+ name = parts.first.sub(/\.$/, '')
13
12
  @ext = parts.last
14
- @locale = options[:locale]
13
+ @locale = options[:locale] unless blank?(options[:locale])
15
14
 
16
- @file_path = @locale ? File.join(@path, "#{@name}.#{@locale}.#{@ext}") : file_path
15
+ @file_path = file_path
17
16
 
17
+ unless blank? @locale
18
+ @file_name = [name, @locale, @ext].compact.join('.')
19
+ @file_path = File.join(@path, @file_name)
20
+ end
21
+
22
+ @dir = '' if file_path[0] == '/'
18
23
  @dir = options[:dir] if options[:dir]
19
24
  @root = (options[:root] || file_name.split('.').first).to_s
20
25
  @root = nil unless mashie.send(@root)
21
26
  @mashie = mashie.send(@root) if @root
27
+ rescue NoMethodError # if no mashie
28
+ @root = nil
22
29
  end
23
30
 
24
31
  def as_hash
@@ -30,6 +37,10 @@ module ConfigLoader
30
37
 
31
38
  protected
32
39
 
40
+ def blank? obj
41
+ !obj || obj.empty?
42
+ end
43
+
33
44
  def file_content
34
45
  File.open(config_file_path)
35
46
  end
@@ -5,5 +5,9 @@ module ConfigLoader
5
5
  def content
6
6
  @content ||= JSON.parse file_content.read
7
7
  end
8
+
9
+ def parts
10
+ @parts ||= file_name.split(/(json)$/)
11
+ end
8
12
  end
9
13
  end
@@ -5,5 +5,9 @@ module ConfigLoader
5
5
  def content
6
6
  @content ||= ::YAML::load file_content
7
7
  end
8
+
9
+ def parts
10
+ @parts ||= file_name.split(/(ya?ml$)/)
11
+ end
8
12
  end
9
13
  end
@@ -11,7 +11,11 @@ module ConfigLoader
11
11
  when :yml, :yaml
12
12
  ConfigLoader::Yaml
13
13
  end
14
- loader.new file_path, options = {}
14
+ loader.new file_path, options
15
+ end
16
+
17
+ def load_content file_path, options = {}
18
+ load(file_path, options).content
15
19
  end
16
20
 
17
21
  def load_hash file_path, options = {}
@@ -25,4 +29,8 @@ module ConfigLoader
25
29
  def load_json file_path, options = {}
26
30
  ConfigLoader::Json.new file_path, options = {}
27
31
  end
32
+
33
+ def load_json_content file_path, options = {}
34
+ load_json(file_path, options).content
35
+ end
28
36
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rails_config_loader"
8
- s.version = "0.1.2"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = "2012-05-09"
12
+ s.date = "2012-05-20"
13
13
  s.description = "Load yaml and json config files and \n make the structures easily available for use in the app."
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -31,11 +31,16 @@ Gem::Specification.new do |s|
31
31
  "lib/config_loader/yaml.rb",
32
32
  "lib/rails_config_loader.rb",
33
33
  "rails_config_loader.gemspec",
34
+ "spec/config_loader/delegator_spec.rb",
35
+ "spec/config_loader/json_spec.rb",
34
36
  "spec/config_loader/yaml_spec.rb",
37
+ "spec/config_loader_spec.rb",
35
38
  "spec/fixtures/config/app.yml",
36
39
  "spec/fixtures/config/facebook.yml",
37
40
  "spec/fixtures/config/htc.yml",
38
41
  "spec/fixtures/config/payment_gateway/quickpay.da.yml",
42
+ "spec/fixtures/config/property.yml",
43
+ "spec/fixtures/data/addresses.da.json",
39
44
  "spec/fixtures/db/config/seed.yml",
40
45
  "spec/spec_helper.rb",
41
46
  "spec/support/main_app.rb"
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Delegator' do
4
+ subject { config }
5
+ let(:config) { MainApp.config }
6
+
7
+ specify { subject.as_hash.name.should == 'HTC' }
8
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ConfigLoader::Json do
6
+ subject { config }
7
+ let(:config) { ConfigLoader::Json.new '/data/addresses.json', :locale => 'da' }
8
+
9
+ its(:file_name) { should == 'addresses.da.json' }
10
+ its(:root) { should be_nil }
11
+ its(:ext) { should == 'json' }
12
+
13
+ its(:content) { should_not be_nil }
14
+ its(:content) { should == [{"number"=>"004", "street"=>"Bøllemosegårdsvej", "city"=>"Øbro", "region"=>"København"}, {"number"=>"001", "street"=>"Bøllemosegårdsvej", "city"=>"Venners", "region"=>"København"}, {"number"=>"002", "street"=>"Havlyngbuen", "city"=>"Amager", "region"=>"København"}, {"number"=>"001", "street"=>"Havlyngbuen", "city"=>"Amager", "region"=>"København"}, {"number"=>"051", "street"=>"Hedegaardsvej", "city"=>"Engdraget", "region"=>"København"}] }
15
+ end
16
+
17
+ describe '.load' do
18
+ subject { addresses }
19
+ let(:addresses) { MainApp.config.addresses }
20
+
21
+ specify do
22
+ addresses.first.should == {"number"=>"004", "street"=>"Bøllemosegårdsvej", "city"=>"Øbro", "region"=>"København"}
23
+ end
24
+ end
@@ -1,15 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- module Rails
4
- def self.root
5
- File.join SPEC_DIR, 'fixtures'
6
- end
7
- end
8
-
9
- class LoadMe
10
- include ConfigLoader
11
- end
12
-
13
3
  describe ConfigLoader::Yaml do
14
4
  subject { config }
15
5
  let(:config) { ConfigLoader::Yaml.new('facebook.yml') }
@@ -40,25 +30,11 @@ describe ConfigLoader::Yaml do
40
30
  end
41
31
  end
42
32
 
43
- describe ConfigLoader do
44
- subject { loadme }
45
-
46
- let(:loadme) { LoadMe.new }
47
-
48
- describe '.load' do
49
- specify { subject.load('htc.yml').as_hash.domain.should == 'www.htc.com' }
50
- end
51
-
52
- describe '.load_hash' do
53
- specify { subject.load_hash('htc.yml').domain.should == 'www.htc.com' }
54
- end
55
- end
56
-
57
- describe 'Delegator' do
58
- subject { config }
59
- let(:config) { MainApp.config }
33
+ describe '.load_hash' do
34
+ subject { property }
35
+ let(:property) { MainApp.config.property }
60
36
 
61
- specify { subject.as_hash.name.should == 'HTC' }
37
+ specify { subject.type.house_boat.should == 'house boat' }
62
38
  end
63
39
 
64
40
  describe '.load' do
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe ConfigLoader do
4
+ subject { loadme }
5
+
6
+ let(:loadme) { LoadMe.new }
7
+
8
+ describe '.load' do
9
+ specify { subject.load('htc.yml').as_hash.domain.should == 'www.htc.com' }
10
+ end
11
+
12
+ describe '.load_hash' do
13
+ specify { subject.load_hash('htc.yml').domain.should == 'www.htc.com' }
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ property:
2
+ type:
3
+ room: room
4
+ apartment: apartment
5
+ town_house: town house
6
+ independent_house: independent_house
7
+ office: office
8
+ house_boat: house boat
9
+ rooms:
10
+ one: one
11
+ two: two
12
+ three: three
13
+ four: four
14
+ five: five
15
+ six: six
16
+ seven: seven or more
@@ -0,0 +1,32 @@
1
+ [
2
+ {
3
+ "number": "004",
4
+ "street": "Bøllemosegårdsvej",
5
+ "city": "Øbro",
6
+ "region": "København"
7
+ },
8
+ {
9
+ "number": "001",
10
+ "street": "Bøllemosegårdsvej",
11
+ "city": "Venners",
12
+ "region": "København"
13
+ },
14
+ {
15
+ "number": "002",
16
+ "street": "Havlyngbuen",
17
+ "city": "Amager",
18
+ "region": "København"
19
+ },
20
+ {
21
+ "number": "001",
22
+ "street": "Havlyngbuen",
23
+ "city": "Amager",
24
+ "region": "København"
25
+ },
26
+ {
27
+ "number": "051",
28
+ "street": "Hedegaardsvej",
29
+ "city": "Engdraget",
30
+ "region": "København"
31
+ }
32
+ ]
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,16 @@ require 'rails_config_loader'
3
3
 
4
4
  SPEC_DIR = File.dirname(__FILE__)
5
5
 
6
+ module Rails
7
+ def self.root
8
+ File.join SPEC_DIR, 'fixtures'
9
+ end
10
+ end
11
+
12
+ class LoadMe
13
+ include ConfigLoader
14
+ end
15
+
6
16
  # Requires supporting files with custom matchers and macros, etc,
7
17
  # in ./support/ and its subdirectories.
8
18
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -15,6 +15,14 @@ module MainApp
15
15
  @seed ||= load_yaml('config/seed.yml', :dir => 'db')
16
16
  end
17
17
 
18
+ def property
19
+ @property ||= load_hash 'property.yml'
20
+ end
21
+
22
+ def addresses locale = :da
23
+ @addresses ||= load_content '/data/addresses.json', :locale => locale
24
+ end
25
+
18
26
  # config for the app
19
27
  # any missing method on this is delegated to the Hashie wrapping this # loaded content
20
28
  def config
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_config_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-09 00:00:00.000000000 Z
12
+ date: 2012-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie
@@ -146,11 +146,16 @@ files:
146
146
  - lib/config_loader/yaml.rb
147
147
  - lib/rails_config_loader.rb
148
148
  - rails_config_loader.gemspec
149
+ - spec/config_loader/delegator_spec.rb
150
+ - spec/config_loader/json_spec.rb
149
151
  - spec/config_loader/yaml_spec.rb
152
+ - spec/config_loader_spec.rb
150
153
  - spec/fixtures/config/app.yml
151
154
  - spec/fixtures/config/facebook.yml
152
155
  - spec/fixtures/config/htc.yml
153
156
  - spec/fixtures/config/payment_gateway/quickpay.da.yml
157
+ - spec/fixtures/config/property.yml
158
+ - spec/fixtures/data/addresses.da.json
154
159
  - spec/fixtures/db/config/seed.yml
155
160
  - spec/spec_helper.rb
156
161
  - spec/support/main_app.rb
@@ -169,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
174
  version: '0'
170
175
  segments:
171
176
  - 0
172
- hash: -2060976259246285744
177
+ hash: 889680361015495165
173
178
  required_rubygems_version: !ruby/object:Gem::Requirement
174
179
  none: false
175
180
  requirements: