easy_settings 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b709efa47d7dddceee7ab9a1b1e37dd8cbe3f4ec
4
- data.tar.gz: b261837ca1c99072433d6cc17fbdf934c112444e
3
+ metadata.gz: 5e7a4cd755414c8b9ac73537415a9354c216cfbe
4
+ data.tar.gz: bb8c676c3225d497595cc0524566c268e97fb444
5
5
  SHA512:
6
- metadata.gz: 626795da3c3d70ceec31ae5348b54c9401ec780cb8b73b7b6ec2c4c47de517e7ec92695694654ba79168bf8af34baaa04719866ec4459f1fb4e38bbcdd0c07f6
7
- data.tar.gz: 5b84a6e1630eadbbddd70b6b7ee1169896e4eb17e4a56d3fa9619167188de956fe61953bab142191c322d716ea032740c6dfb9a26b40b1953ce1a42eba1abd50
6
+ metadata.gz: 4e84cede6e0deb01912732e031d493cba0fb2f33cbfc45d77ee9a36496e20e969fe5d6172780d760599b04e9b47bc77606310b2488fa724a9ede9b904121646f
7
+ data.tar.gz: 5c84841fb7e8c092a80ebaac569fbc7c3079e9d960b03a77773941b8baeefee7e6dcdccc47c4e4920a013250d11101270ff57b59f8af2273504227467d03982d
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "easy_settings"
4
+ require "easy_settings_version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "easy_settings"
8
- spec.version = EasySettings::VERSION
8
+ spec.version = EasySettingsVersion::VERSION
9
9
  spec.authors = ["nownabe"]
10
10
  spec.email = ["nownabe@gmail.com"]
11
11
  spec.summary = %q{EasySettings is a simple settings with YAML file.}
data/lib/easy_settings.rb CHANGED
@@ -2,20 +2,20 @@ require "hashie"
2
2
  require "yaml"
3
3
  require "erb"
4
4
 
5
+ require "easy_settings_version"
6
+
5
7
  class EasySettings < Hashie::Mash
6
- VERSION = "0.0.1"
8
+ include EasySettingsVersion
9
+
10
+ class SourceFileNotExist < StandardError; end
11
+
12
+ DEFAULT_FILES = %w(settings.yml config/settings.yml)
7
13
 
8
14
  class << self
9
15
  attr_accessor :source_hash, :source_file, :namespace
10
16
 
11
- def method_missing(method_name, *args, &blk)
12
- p method_name
13
- p args
14
- instance.send(method_name, *args, &blk)
15
- end
16
-
17
17
  def instance
18
- @instance ||= new(source_hash || source_file, nil, namespace)
18
+ @instance ||= new(namespace ? _source[namespace] : _source)
19
19
  end
20
20
 
21
21
  def load!
@@ -27,33 +27,43 @@ class EasySettings < Hashie::Mash
27
27
  @instance = nil
28
28
  load!
29
29
  end
30
- end
31
30
 
32
- def initialize(source = nil, default = nil, namespace = nil, &blk)
33
- source = load_source_file(source) if source.nil? or source.is_a?(String)
34
- source = source[namespace] if namespace
35
- super(source, default)
36
- end
31
+ private
37
32
 
38
- def assign_property(name, value)
39
- super
40
- self[name]
41
- end
33
+ def _source
34
+ return source_hash.to_hash if source_hash
35
+ return _source_from_file if source_file
36
+ _source_from_default_file
37
+ end
42
38
 
43
- def find_file(file)
44
- return file if file and FileTest.exist?(file)
45
- ["settings.yml", "config/settings.yml"].each do |f|
46
- path = File.expand_path(f, Dir.pwd)
47
- return path if FileTest.exist?(path)
39
+ def _load_file(file)
40
+ content = File.read(file)
41
+ content.empty? ? nil : YAML.load(ERB.new(content).result).to_hash
42
+ end
43
+
44
+ def _source_from_file
45
+ unless FileTest.exist?(source_file)
46
+ raise(SourceFileNotExist, "Your source file '#{file}' does not exist.")
47
+ end
48
+ _load_file(source_file)
49
+ end
50
+
51
+ def _source_from_default_file
52
+ DEFAULT_FILES.each do |f|
53
+ path = File.expand_path(f, Dir.pwd)
54
+ return _load_file(path) if FileTest.exist?(path)
55
+ end
56
+ nil
57
+ end
58
+
59
+ def method_missing(method_name, *args, &blk)
60
+ instance.send(method_name, *args, &blk)
48
61
  end
49
- nil
50
62
  end
51
63
 
52
- def load_source_file(source)
53
- file = find_file(source)
54
- return nil unless file # = find_file(source)
55
- content = File.read(file)
56
- content.empty? ? {} : YAML.load(ERB.new(content).result).to_hash
64
+ def assign_property(name, value)
65
+ super
66
+ self[name]
57
67
  end
58
68
 
59
69
  def method_missing(method_name, *args, &blk)
@@ -0,0 +1,3 @@
1
+ module EasySettingsVersion
2
+ VERSION = "0.0.2"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - nownabe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -94,6 +94,7 @@ files:
94
94
  - Rakefile
95
95
  - easy_settings.gemspec
96
96
  - lib/easy_settings.rb
97
+ - lib/easy_settings_version.rb
97
98
  homepage: https://github.com/nownabe/easy_settings
98
99
  licenses:
99
100
  - MIT