isomer 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -30,9 +30,9 @@ Or install it yourself as:
30
30
  For most configurations, you can use the simple setup approach
31
31
 
32
32
  ```ruby
33
- MY_FANCY_CONFIGURATION = Isomer.configure(:yaml, base: Rails.env) do |config|
34
- config.parameter :url # defaults to { required: false, from: 'url', default: nil }
35
- config.parameter :api_key
33
+ MY_FANCY_CONFIGURATION = Isomer.configure(:yaml, file: File.join('config', 'fancy.yml')) do |config|
34
+ config.parameter :url, name: 'host'
35
+ config.parameter :api_key, required: true
36
36
  config.parameter :timeout
37
37
 
38
38
  config.parameter :logger, default: Rails.logger
@@ -43,24 +43,63 @@ If you have a more complex setup, or want add extra methods to your configuratio
43
43
 
44
44
  ```ruby
45
45
  class MyFancyConfiguration < Isomer::Base
46
- parameter :url # defaults to { required: false, name: 'url', default: nil }
46
+ parameter :url, name: 'host'
47
47
  parameter :api_key, required: true
48
48
  parameter :timeout
49
49
 
50
50
  parameter :logger, default: Rails.logger
51
51
  end
52
+
53
+ ...
54
+
55
+ # In some initializer
56
+ MY_FANCY_CONFIG = MyFancyConfiguration.from(:yaml, file: File.join('config', 'fancy.yml'))
52
57
  ```
53
58
 
59
+ ## Parameters
60
+
61
+ The core of Isomer is allowing you to define the parameters to pull out of your configuration source. It takes a parameter name along with a number of options.
62
+
63
+ The name will be how you access the value from the configuration object. It also is the default value used when trying to find the value of the parameter in the source.
64
+
65
+ You can also specify a number of parameters:
66
+
67
+ * `required`: Means this parameter is required. If any are missing, `from` will raise a Isomer::RequiredParameterError. Defaults to `false`
68
+ * `name`: If the configuration source uses a different name than that of the parameter
69
+ * `default`: If you want to specify a default value
70
+
71
+ ## Sources
72
+
54
73
  ### Initialize with a YAML file
55
74
 
75
+ #### Options
76
+
77
+ * `file`: Path to the YAML file (required)
78
+ * `root`: If there are multiple configurations (eg. for different environments), you can specify which one to use
79
+ * `required`: If the configuration file is required
80
+
81
+
82
+ #### Example
83
+
56
84
  ```ruby
57
- MY_FANCY_CONFIGURATION = MyFancyConfiguration.from(:yaml, file: Rails.root.join('config', 'app_card.yml'), base: Rails.env)
85
+ MY_FANCY_CONFIGURATION = MyFancyConfiguration.from(:yaml, file: Rails.root.join('config', 'fancy.yml'), base: Rails.env) do |config|
86
+ ...
87
+ end
58
88
  ```
59
89
 
60
90
  ### Initialize with environment variables
61
91
 
92
+ #### Options
93
+
94
+ * `convert_case`: Should we automatically uppercase any parameter names when looking for the environment variable (defaults to `true`)
95
+ * `prefix`: Add a prefix when looking up all parameters in the environment
96
+
97
+ #### Example
98
+
62
99
  ```ruby
63
- MY_FANCY_CONFIGURATION = MyFancyConfiguration.from(:environment, prefix: 'FANCY_CONFIG_')
100
+ MY_FANCY_CONFIGURATION = MyFancyConfiguration.from(:environment, prefix: 'FANCY_CONFIG_') do |config|
101
+ ...
102
+ end
64
103
  ```
65
104
 
66
105
  ## Contributing
data/lib/isomer/errors.rb CHANGED
@@ -1,2 +1,5 @@
1
- class Isomer::RequiredParameterError < StandardError
1
+ class Isomer::Error < StandardError
2
+ end
3
+
4
+ class Isomer::RequiredParameterError < Isomer::Error
2
5
  end
@@ -4,12 +4,12 @@ class Isomer::Parameter
4
4
  def initialize(id, options)
5
5
  @id = id
6
6
  @required = options[:required] || false
7
- @from = options[:from]
7
+ @name = options[:name]
8
8
  @default = options[:default]
9
9
  end
10
10
 
11
11
  def name
12
- (@from || @id).to_s
12
+ (@name || @id).to_s
13
13
  end
14
14
 
15
15
  def required?
@@ -1,11 +1,14 @@
1
1
  require 'yaml'
2
2
 
3
3
  class Isomer::Sources::Yaml < Isomer::Sources::Base
4
- attr_reader :file, :base
4
+ attr_reader :file, :base, :required
5
5
 
6
6
  def initialize(parameters, options={})
7
7
  @file = options[:file]
8
+ raise Isomer::Error, "YAML source requires the 'file' parameter" if file.nil? || file.empty?
9
+
8
10
  @base = options[:base]
11
+ @required = !!options[:required]
9
12
 
10
13
  super(parameters)
11
14
  end
@@ -18,6 +21,9 @@ class Isomer::Sources::Yaml < Isomer::Sources::Base
18
21
  else
19
22
  @configuration = values
20
23
  end
24
+ else
25
+ raise Isomer::Error, "Missing required configuration file '#{file}'" if required
26
+ @configuration = {}
21
27
  end
22
28
  end
23
29
  end
@@ -1,3 +1,3 @@
1
1
  module Isomer
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -18,7 +18,7 @@ describe Isomer::Parameter do
18
18
 
19
19
  context 'when there is a from option' do
20
20
  it 'returns the from value as a string' do
21
- parameter = Isomer::Parameter.new(:bar, {from: :baz})
21
+ parameter = Isomer::Parameter.new(:bar, {name: :baz})
22
22
  parameter.name.should == 'baz'
23
23
  end
24
24
  end
@@ -1,6 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Isomer::Sources::Yaml do
4
+ describe '.new' do
5
+ it 'blows up if the file parameter is missing' do
6
+ expect {
7
+ Isomer::Sources::Yaml.new(anything)
8
+ }.to raise_error(Isomer::Error, "YAML source requires the 'file' parameter")
9
+ end
10
+ end
11
+
4
12
  describe '#load' do
5
13
  it 'loads the YAML file configuration file' do
6
14
  File.stub(:exists?).and_return(true)
@@ -12,9 +20,28 @@ describe Isomer::Sources::Yaml do
12
20
  source.configuration.should == {'foo' => 'bar'}
13
21
  end
14
22
 
15
- it 'does not raise if the file does not exist' do
16
- source = Isomer::Sources::Yaml.new(anything, file: '/home/configuration.yml')
17
- expect { source.load }.to_not raise_error
23
+ context 'when the file does not exist' do
24
+ context 'when it is not required' do
25
+ it 'does not blow up' do
26
+ source = Isomer::Sources::Yaml.new(anything, file: '/home/configuration.yml')
27
+ expect { source.load }.to_not raise_error
28
+ end
29
+
30
+ it 'sets the configuration to an empty hash' do
31
+ source = Isomer::Sources::Yaml.new(anything, file: '/home/configuration.yml')
32
+ source.load
33
+ source.configuration.should == {}
34
+ end
35
+ end
36
+
37
+ context 'when it is required' do
38
+ it 'raises an error' do
39
+ source = Isomer::Sources::Yaml.new(anything, file: '/home/configuration.yml', required: true)
40
+ expect {
41
+ source.load
42
+ }.to raise_error(Isomer::Error, "Missing required configuration file '/home/configuration.yml'")
43
+ end
44
+ end
18
45
  end
19
46
 
20
47
  context 'with a base' do
@@ -22,7 +49,7 @@ describe Isomer::Sources::Yaml do
22
49
  File.stub(:exists?).and_return(true)
23
50
  YAML.stub(:load_file).and_return( 'production' => {'limit' => 100} )
24
51
 
25
- source = Isomer::Sources::Yaml.new(anything, base: 'production')
52
+ source = Isomer::Sources::Yaml.new(anything, file: 'filish.yml', base: 'production')
26
53
  source.load
27
54
 
28
55
  source.configuration.should == {'limit' => 100}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isomer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
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: 2013-08-08 00:00:00.000000000 Z
12
+ date: 2013-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -107,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  segments:
109
109
  - 0
110
- hash: -4134372492786935908
110
+ hash: 1846134878671279424
111
111
  required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  none: false
113
113
  requirements:
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  segments:
118
118
  - 0
119
- hash: -4134372492786935908
119
+ hash: 1846134878671279424
120
120
  requirements: []
121
121
  rubyforge_project:
122
122
  rubygems_version: 1.8.25