config_plus 0.0.4 → 0.0.5

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: 6ec4ceff4f380e588b0e43d98ff99ca52877041c
4
- data.tar.gz: bed1b98ab63af2868bdb19fdf7942e6d85d828f2
3
+ metadata.gz: 189eb84bec6ebcfa3fbb181d5d548c47fb7e4c3e
4
+ data.tar.gz: 8b71d5dc1ac70f6eadc0fe5d49d78e724d63ad6c
5
5
  SHA512:
6
- metadata.gz: b780e303acb8ce8a639bba7cd7f44120d59ec132230e081c0274032c784e709e75c3a5284aa230946fa8a7718880ecdcd1f5eda8378564ee6515961c38bea243
7
- data.tar.gz: ef9f084b4b87ddb467278902b6b509f2ce7cf93b3459a2b9d57ec59fbc7a4442f3b787a2fc1cf6bb2d9317ac752025d52ac5161a604ffd21ed4dafe200de2301
6
+ metadata.gz: 4c5807c8460291543b7c536d0394034b05cc3bdef580fddccf312f56ee7af0fd69f78a3481f661e131263b9b5eae7c3141c0c55006f10c682b19a2a807d897cc
7
+ data.tar.gz: 7ccf3c2d162116d904e5d56162461cfc1f311239af62158869efbeb25d6df09a72b3035bd2797eebb0d005657829a56e9eadaca6a7c506d3580b1cdb5b6bd754
data/README.ja.md CHANGED
@@ -54,6 +54,25 @@ ConfigPlus.generate(from: '/path/to/configuration/directory')
54
54
  ConfigPlus.generate(from: ['/path/to/directory1', '/path/to/file1.yml'])
55
55
  ```
56
56
 
57
+ ERB タグを含む YAML の読み込み
58
+ --------------------------------------------------
59
+ `loader_logic` に `erb_yaml` を指定すると、ERB タグを解釈して読み込むことができます。
60
+
61
+ たとえばこのような YAML ファイルがあった場合、
62
+
63
+ ```yaml
64
+ abc:
65
+ foo: <%= (1..10).inject(&:+) %>
66
+ ```
67
+
68
+ 次のようになります。
69
+
70
+ ```ruby
71
+ ConfigPlus.generate(from: 'path/to/configuration/file.erb.yml', loader_logic: :erb_yaml)
72
+ ConfigPlus.abc.foo
73
+ #=> 55
74
+ ```
75
+
57
76
  自動マッピング
58
77
  --------------------------------------------------
59
78
  YAML の構造と同じパスを持つクラスがある場合、
@@ -148,6 +167,7 @@ end
148
167
  | `namespace` | 使用するネームスペース | |
149
168
  | `root_dir` | `source` で指定するパスの親ディレクトリのパス | |
150
169
  | `source` | 設定ファイル、またはそれが格納されているディレクトリのパス | |
170
+ | `loader_logic` | 設定ファイルの読み込みロジックの設定 | `default` |
151
171
 
152
172
  ライセンス
153
173
  --------------------------------------------------
data/README.md CHANGED
@@ -48,6 +48,25 @@ ConfigPlus.generate(from: ['/path/to/directory1', '/path/to/file1.yml'])
48
48
  ```
49
49
 
50
50
 
51
+ YAML with ERB tags
52
+ ------------------------------------------------------------
53
+ `ConfigPlus` can evaluate ERB tags when you add `loader_logic` setting.
54
+ For example, when there is a such YAML file:
55
+
56
+ ```yaml
57
+ abc:
58
+ foo: <%= (1..10).inject(&:+) %>
59
+ ```
60
+
61
+ You can get it with the following way:
62
+
63
+ ```ruby
64
+ ConfigPlus.generate(from: '/path/to/configuration/file.yml', loader_logic: :erb_yaml)
65
+ ConfigPlus.abc.foo
66
+ #=> 55
67
+ ```
68
+
69
+
51
70
  Auto Mapping
52
71
  ------------------------------------------------------------
53
72
  When data structure of loaded YAML and class structure of your
@@ -139,6 +158,9 @@ Properties you can set are following:
139
158
  relative path as `source`
140
159
  * `source`
141
160
  * a file path or a directory path or an array of them
161
+ * `loader_logic`
162
+ * logic to load YAML files. When you specify `erb_yaml`
163
+ ERB tags can be evaluated
142
164
 
143
165
 
144
166
  License
@@ -27,11 +27,14 @@ module ConfigPlus
27
27
  #
28
28
  # ConfigPlus.generate(from: '/path/to/yaml/file.yml')
29
29
  #
30
- def generate(from: nil, **properties)
31
- config.source = from if from
32
- properties.each do |k, v|
33
- attr = "#{k}="
34
- config.public_send(attr, v) if config.respond_to? attr
30
+ def generate(options={})
31
+ config.source = options.delete(:from) or options.delete('from')
32
+ options.each do |k, v|
33
+ if config.has_property?(k)
34
+ config.property_set(k, v)
35
+ else
36
+ raise "Unknown configuration property `#{k}'"
37
+ end
35
38
  end
36
39
  load
37
40
  end
@@ -50,7 +53,7 @@ module ConfigPlus
50
53
  #
51
54
  def load
52
55
  hash = config.loader.load
53
- @root = self::Node.new(hash)
56
+ @root = config.node_model.new(hash)
54
57
  end
55
58
 
56
59
  def inherited_config_of(klass)
@@ -72,12 +75,13 @@ module ConfigPlus
72
75
  own = self::Helper.config_for(base, self.root)
73
76
  inheritance = inherited_config_of(base)
74
77
 
78
+ node_model = config.node_model
75
79
  [base, base.singleton_class].each do |obj|
76
80
  obj.instance_eval do
77
81
  config = inheritance ?
78
82
  ::ConfigPlus::Merger.merge(inheritance, own || {}) : own
79
- config = ::ConfigPlus::Node.new(config)
80
- define_method method_name, -> { config }
83
+ config = node_model.new(config)
84
+ define_method method_name, lambda { config }
81
85
  end
82
86
  end
83
87
  end
@@ -1,19 +1,31 @@
1
1
  module ConfigPlus
2
2
  class Config
3
- attr_accessor :config_method,
3
+ class << self
4
+ attr_reader :properties
5
+
6
+ def prop_accessor(*names)
7
+ @properties ||= []
8
+ @properties.concat(names)
9
+ attr_accessor *names
10
+ end
11
+
12
+ private :prop_accessor
13
+ end
14
+
15
+ prop_accessor :config_method,
4
16
  :extension,
5
17
  :namespace,
18
+ :node_model,
6
19
  :root_dir,
7
- :source
8
- attr_reader :version
9
- attr_writer :loader_logic
20
+ :source,
21
+ :loader_logic
10
22
 
11
23
  def initialize
12
- @version = VERSION
13
24
  @config_method = :config
14
25
  @extension = nil
15
26
  @loader_logic = :default
16
27
  @namespace = nil
28
+ @node_model = Node
17
29
  @root_dir = nil
18
30
  @source = nil
19
31
  end
@@ -31,5 +43,17 @@ module ConfigPlus
31
43
  ::ConfigPlus::const_defined?(name)
32
44
  ::ConfigPlus::const_get(name)
33
45
  end
46
+
47
+ def version
48
+ VERSION
49
+ end
50
+
51
+ def has_property?(name)
52
+ self.class.properties.include?(name.to_sym)
53
+ end
54
+
55
+ def property_set(name, value)
56
+ instance_variable_set("@#{name}", value)
57
+ end
34
58
  end
35
59
  end
@@ -1,20 +1,26 @@
1
1
  require 'yaml'
2
2
 
3
3
  module ConfigPlus
4
+ # This loader reads configuration from the specified
5
+ # a YAML file with its ++load_from++ method.
6
+ # When a directory is specified this recurses a file
7
+ # tree and reads all YAML files.
8
+ #
4
9
  class DefaultLoaderLogic
5
- def initialize(config)
6
- @config = config
7
- end
8
-
9
- def extension
10
- @config.extension || [:yml, :yaml]
10
+ def initialize(extension)
11
+ @extension = extension || [:yml, :yaml]
11
12
  end
12
13
 
13
14
  def load_from(path)
15
+ raise Errno::ENOENT, "No such file or directory: `#{path}'" unless File.exist?(path)
14
16
  return load_file(path) if File.file?(path)
15
17
  load_dir(path)
16
18
  end
17
19
 
20
+ private
21
+
22
+ attr_reader :extension
23
+
18
24
  def load_file(filepath)
19
25
  content = open(filepath).read
20
26
  YAML.load(content).to_hash
@@ -0,0 +1,11 @@
1
+ require 'erb'
2
+
3
+ module ConfigPlus
4
+ class ErbYamlLoaderLogic < DefaultLoaderLogic
5
+ def load_file(filepath)
6
+ content = open(filepath).read
7
+ content = ERB.new(content).result
8
+ YAML.load(content).to_hash
9
+ end
10
+ end
11
+ end
@@ -6,7 +6,7 @@ module ConfigPlus
6
6
  return configs.first if configs.size <= 1
7
7
 
8
8
  if configs.all? {|c| c.is_a? Hash }
9
- configs.inject(::ConfigPlus::Node.new) {|h, conf|
9
+ configs.inject(config.class.new) {|h, conf|
10
10
  ::ConfigPlus::Merger.merge(h, conf)
11
11
  }
12
12
  else
@@ -17,8 +17,7 @@ module ConfigPlus
17
17
  def underscore(name)
18
18
  return name.underscore if name.respond_to?(:underscore)
19
19
 
20
- name.gsub(/::/, '.')
21
- .gsub(/((\A|\b)([A-Z]+))|([A-Z]+)/) do
20
+ name.gsub(/::/, '.').gsub(/((\A|\b)([A-Z]+))|([A-Z]+)/) do
22
21
  next $3.downcase if $3
23
22
  "_#{$4.downcase}"
24
23
  end
@@ -26,8 +25,8 @@ module ConfigPlus
26
25
 
27
26
  def classify(name)
28
27
  return name.classify if name.respond_to?(:classify)
29
- name.to_s.gsub(/\/+/, '::')
30
- .gsub(/_?([a-z]+)/) { $1.capitalize }
28
+ name.to_s.gsub(/\/+/, '::').
29
+ gsub(/_?([a-z]+)/) { $1.capitalize }
31
30
  end
32
31
 
33
32
  private
@@ -18,7 +18,7 @@ module ConfigPlus
18
18
  protected
19
19
 
20
20
  def loader_logic
21
- @loader_logic ||= @config.loader_logic.new(@config)
21
+ @loader_logic ||= @config.loader_logic.new(@config.extension)
22
22
  end
23
23
 
24
24
  def source_paths
@@ -44,7 +44,7 @@ module ConfigPlus
44
44
  return if respond_to?(method_name) or
45
45
  private_methods.include?(method_name.to_sym)
46
46
  singleton_class.class_eval do
47
- define_method method_name, -> { self[method_name] }
47
+ define_method method_name, lambda { self[method_name] }
48
48
  end
49
49
  end
50
50
  end
@@ -1,3 +1,3 @@
1
1
  module ConfigPlus
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
data/lib/config_plus.rb CHANGED
@@ -1,8 +1,12 @@
1
1
  require 'config_plus/base'
2
- require 'config_plus/config'
3
- require 'config_plus/default_loader_logic'
4
- require 'config_plus/helper'
5
- require 'config_plus/loader'
6
- require 'config_plus/merger'
7
- require 'config_plus/node'
8
2
  require 'config_plus/version'
3
+
4
+ module ConfigPlus
5
+ autoload :Config, 'config_plus/config'
6
+ autoload :DefaultLoaderLogic, 'config_plus/default_loader_logic'
7
+ autoload :ErbYamlLoaderLogic, 'config_plus/erb_yaml_loader_logic'
8
+ autoload :Helper, 'config_plus/helper'
9
+ autoload :Loader, 'config_plus/loader'
10
+ autoload :Merger, 'config_plus/merger'
11
+ autoload :Node, 'config_plus/node'
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - m4oda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-03 00:00:00.000000000 Z
11
+ date: 2017-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -37,6 +37,7 @@ files:
37
37
  - lib/config_plus/base.rb
38
38
  - lib/config_plus/config.rb
39
39
  - lib/config_plus/default_loader_logic.rb
40
+ - lib/config_plus/erb_yaml_loader_logic.rb
40
41
  - lib/config_plus/helper.rb
41
42
  - lib/config_plus/loader.rb
42
43
  - lib/config_plus/merger.rb
@@ -54,7 +55,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
55
  requirements:
55
56
  - - ">="
56
57
  - !ruby/object:Gem::Version
57
- version: '0'
58
+ version: 1.9.2
58
59
  required_rubygems_version: !ruby/object:Gem::Requirement
59
60
  requirements:
60
61
  - - ">="
@@ -62,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  version: '0'
63
64
  requirements: []
64
65
  rubyforge_project:
65
- rubygems_version: 2.4.5
66
+ rubygems_version: 2.5.1
66
67
  signing_key:
67
68
  specification_version: 4
68
69
  summary: An easy-to-use, powerful configuration module using YAML files