shutl_yaml_properties 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ require 'active_support/core_ext/hash'
2
+
3
+ class YamlProperties::Diff
4
+ def initialize a, b
5
+ @hash_a, @hash_b = read(a, b)
6
+ unless same?
7
+ raise ArgumentError.new(error_message)
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def error_message
14
+ "Following keys are not present in both files '#{key_diff}'"
15
+ end
16
+
17
+ def key_diff
18
+ (keys_a - keys_b) + (keys_b - keys_a)
19
+ end
20
+
21
+ def keys_a
22
+ @hash_a.keys
23
+ end
24
+
25
+ def keys_b
26
+ @hash_b.keys
27
+ end
28
+
29
+ def same?
30
+ key_diff.empty?
31
+ end
32
+
33
+ def read *files
34
+ files.map do |f|
35
+ YAML.load File.read(f)
36
+ end
37
+ end
38
+
39
+ end
40
+
@@ -0,0 +1,3 @@
1
+ module YamlProperties
2
+ VERSION = "0.0.13"
3
+ end
@@ -0,0 +1,54 @@
1
+ require "yaml_properties/version"
2
+ require "active_support/core_ext/hash"
3
+ require "yaml"
4
+ require "erb"
5
+
6
+ module YamlProperties
7
+ def properties
8
+ reset! if dev_environment
9
+
10
+ @properties ||= load_properties.with_indifferent_access
11
+ end
12
+
13
+ attr_accessor :dev_environment
14
+
15
+ def reset!
16
+ reset_properties
17
+ end
18
+
19
+ def reset_properties
20
+ @properties = nil
21
+ end
22
+
23
+ def method_missing(key, *args, &block)
24
+ return properties[key] if key_present? key
25
+
26
+ super key, *args, &block
27
+ end
28
+
29
+
30
+ def override_attribute attribute, value
31
+ unless key_present? attribute
32
+ raise ArgumentError, "Trying to override non-existent property `#{attribute}' with `#{value}'"
33
+ end
34
+ properties[attribute] = value
35
+ end
36
+
37
+ private
38
+
39
+ def key_present? key
40
+ properties.keys.include? key.to_s
41
+ end
42
+
43
+ def yaml_file
44
+ File.join %w(config properties.yml)
45
+ end
46
+
47
+ def load_properties
48
+ yaml = YAML.load ERB.new(File.read(yaml_file)).result
49
+
50
+ yaml
51
+ end
52
+
53
+ extend self
54
+ end
data/spec/diff_spec.rb ADDED
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../../lib/yaml_properties/diff.rb', __FILE__)
2
+
3
+ describe YamlProperties::Diff do
4
+ def f file
5
+ File.expand_path("../fixtures/#{file}.yml", __FILE__)
6
+ end
7
+
8
+ it "raises an exception if two YAML files have different keys" do
9
+ -> {YamlProperties::Diff.new f('file1'), f('file2')}.should raise_error
10
+ end
11
+
12
+ it "doesn't raise an exception if two YAML files have same keys" do
13
+ -> {YamlProperties::Diff.new f('file1'), f('file4') }.should_not raise_error
14
+ end
15
+
16
+ it "raises a argument error if two YAML files have present vs. non-present keys" do
17
+ argument_error = "Following keys are not present in both files '[\"d\"]'"
18
+
19
+ -> {YamlProperties::Diff.new f('file2'),f('file3')}.should(
20
+ raise_error(ArgumentError, argument_error))
21
+ end
22
+ end
23
+
@@ -0,0 +1 @@
1
+ life_the_universe_and_everything: <%= ENV['mistery_message'] %>
@@ -0,0 +1,3 @@
1
+ a: 1
2
+ b: 2
3
+ c: 3
@@ -0,0 +1,4 @@
1
+ a: 1
2
+ b: 2
3
+ c: 3
4
+ d: 4
@@ -0,0 +1,3 @@
1
+ a: 1
2
+ b: 2
3
+ c: 3
@@ -0,0 +1,3 @@
1
+ a: 1
2
+ b: 2
3
+ c: 3
@@ -0,0 +1 @@
1
+ life_the_universe_and_everything: "Nobody knows"
@@ -0,0 +1,17 @@
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"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
@@ -0,0 +1,127 @@
1
+ require File.expand_path('spec/spec_helper')
2
+ require './lib/yaml_properties'
3
+
4
+ describe YamlProperties do
5
+ context "in general" do
6
+ before do
7
+ File.stub(:open)
8
+ YamlProperties.dev_environment = false
9
+
10
+ Psych.stub(:load).and_return properties
11
+ end
12
+
13
+ let(:properties) do
14
+ {
15
+ "life_the_universe_and_everything" => 42,
16
+ "some_string_value" => "something",
17
+ "a boolean" => true,
18
+ "parent" => {
19
+ "child"=>"Egg"}
20
+ }
21
+ end
22
+
23
+ context "in non dev" do
24
+ before do
25
+ YamlProperties.dev_environment = false
26
+ YamlProperties.reset!
27
+ end
28
+
29
+ specify "it caches yaml file" do
30
+ YamlProperties.should_receive(:load_properties).
31
+ exactly(1).times.and_return properties
32
+
33
+ YamlProperties.some_string_value
34
+ YamlProperties.some_string_value
35
+ end
36
+ end
37
+
38
+ context "in dev environment" do
39
+ before do
40
+ YamlProperties.dev_environment = true
41
+ YamlProperties.reset!
42
+ end
43
+
44
+
45
+ specify "reload so Rails/Sinatra don't need restarting" do
46
+ YamlProperties.should_receive(:load_properties).at_least(2).times.and_return properties
47
+ YamlProperties.some_string_value
48
+ YamlProperties.some_string_value
49
+
50
+ end
51
+ end
52
+
53
+ specify "overriding attributes (in e.g. tests)" do
54
+ YamlProperties.override_attribute :life_the_universe_and_everything, 13
55
+ YamlProperties.life_the_universe_and_everything.should == 13
56
+ YamlProperties.reset!
57
+ YamlProperties.life_the_universe_and_everything.should == 42
58
+ end
59
+
60
+ specify "non-existent attribute overrides shouldn't work" do
61
+ ->{
62
+ YamlProperties.override_attribute :unset_attribute,
63
+ "let's not allow typos to cause us grief"}.
64
+ should raise_error ArgumentError
65
+ end
66
+
67
+ specify do
68
+ YamlProperties.properties.should == properties
69
+ end
70
+ specify do
71
+ YamlProperties.life_the_universe_and_everything.should == 42
72
+ end
73
+
74
+ specify do
75
+ YamlProperties.some_string_value.should == "something"
76
+ end
77
+
78
+ specify do
79
+ YamlProperties.send("a boolean").should == true
80
+ end
81
+
82
+ specify do
83
+ YamlProperties.parent.should == {"child" => "Egg"}
84
+ end
85
+
86
+ context "extending a module" do
87
+ module Acme
88
+ extend YamlProperties
89
+ end
90
+
91
+ specify do
92
+ Acme.life_the_universe_and_everything.should == 42
93
+ end
94
+ end
95
+ end
96
+
97
+ context "overriding yaml_file default" do
98
+ module Mystery
99
+ extend YamlProperties
100
+
101
+ def self.yaml_file
102
+ "./spec/fixtures/test.yml"
103
+ end
104
+ end
105
+
106
+ specify do
107
+ Mystery.life_the_universe_and_everything.should == "Nobody knows"
108
+ end
109
+ end
110
+
111
+ context "with erb" do
112
+ module ErbMystery
113
+ extend YamlProperties
114
+
115
+ def self.yaml_file
116
+ "./spec/fixtures/erb.yml"
117
+ end
118
+ end
119
+
120
+ before { ENV['mistery_message'] = "Everybody knows" }
121
+ after { ENV.delete 'mistery_message' }
122
+
123
+ specify do
124
+ ErbMystery.life_the_universe_and_everything.should == "Everybody knows"
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yaml_properties/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "shutl_yaml_properties"
8
+ gem.version = YamlProperties::VERSION
9
+ gem.authors = ["Mark Burns", "David Rouchy"]
10
+ gem.email = ["markthedeveloper@gmail.com", "davidr@shutl.com"]
11
+ gem.description = %q{Easily add configurable variables for app config using YAML files}
12
+ gem.executables << 'yaml_diff'
13
+
14
+ gem.summary = <<-DESCRIPTION.gsub(/^\s{4}/, '')
15
+ Example:
16
+
17
+ YamlProperties.life_the_universe_and_everything
18
+ #=> 42
19
+
20
+ In config/properties.yml
21
+
22
+ life_the_universe_and_everything: 42
23
+ DESCRIPTION
24
+
25
+ gem.homepage = ""
26
+
27
+ gem.add_dependency 'activesupport'
28
+
29
+ gem.add_development_dependency 'rspec'
30
+ gem.add_development_dependency 'debugger', '~> 1.4.0'
31
+
32
+ gem.files = `git ls-files`.split($/)
33
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
34
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
35
+ gem.require_paths = ["lib"]
36
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shutl_yaml_properties
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.13
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Burns
9
+ - David Rouchy
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-11-29 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rspec
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: debugger
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 1.4.0
63
+ description: Easily add configurable variables for app config using YAML files
64
+ email:
65
+ - markthedeveloper@gmail.com
66
+ - davidr@shutl.com
67
+ executables:
68
+ - yaml_diff
69
+ extensions: []
70
+ extra_rdoc_files: []
71
+ files:
72
+ - .gitignore
73
+ - .rspec
74
+ - .ruby-version
75
+ - Gemfile
76
+ - LICENSE.txt
77
+ - README.md
78
+ - Rakefile
79
+ - bin/yaml_diff
80
+ - config/license_finder.yml
81
+ - config/properties.yml
82
+ - doc/dependencies.csv
83
+ - doc/dependencies.db
84
+ - doc/dependencies.html
85
+ - lib/yaml_properties.rb
86
+ - lib/yaml_properties/diff.rb
87
+ - lib/yaml_properties/version.rb
88
+ - spec/diff_spec.rb
89
+ - spec/fixtures/erb.yml
90
+ - spec/fixtures/file1.yml
91
+ - spec/fixtures/file2.yml
92
+ - spec/fixtures/file3.yml
93
+ - spec/fixtures/file4.yml
94
+ - spec/fixtures/test.yml
95
+ - spec/spec_helper.rb
96
+ - spec/yaml_config_spec.rb
97
+ - yaml_properties.gemspec
98
+ homepage: ''
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: 2433610047750443937
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ segments:
120
+ - 0
121
+ hash: 2433610047750443937
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.23
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: ! 'Example: YamlProperties.life_the_universe_and_everything #=> 42 In config/properties.yml
128
+ life_the_universe_and_everything: 42'
129
+ test_files:
130
+ - spec/diff_spec.rb
131
+ - spec/fixtures/erb.yml
132
+ - spec/fixtures/file1.yml
133
+ - spec/fixtures/file2.yml
134
+ - spec/fixtures/file3.yml
135
+ - spec/fixtures/file4.yml
136
+ - spec/fixtures/test.yml
137
+ - spec/spec_helper.rb
138
+ - spec/yaml_config_spec.rb