canfig 0.0.5 → 0.0.6
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 +4 -4
- data/lib/canfig/config.rb +10 -3
- data/lib/canfig/module.rb +1 -1
- data/lib/canfig/open_config.rb +5 -4
- data/lib/canfig/version.rb +1 -1
- data/lib/canfig/yaml.rb +25 -0
- data/lib/canfig.rb +1 -0
- data/spec/canfig/yaml_spec.rb +38 -0
- data/spec/fixtures/config.yml +3 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19205ebc7b976f9e5d9f15a0a891699687f48c28
|
4
|
+
data.tar.gz: a1832b75a3870d72dbc3c303c2a1c154bd51db34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffce29c8ae20dfc4714b4a447f542cb4310f8fa12297b3eb35afc8669b5853d82df314fc7b2244ccf4c3a0877df5f389edd245345954b10f2f6c468e29552dc4
|
7
|
+
data.tar.gz: 4c60c5327c9207c5811d08bd73700f5ac3cdc8ff0f817123b3935ff557b7d7c1ee42ce59955c4e6b9f9850657930fdb985568e23ecf54b60f91a109372f374d6
|
data/lib/canfig/config.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
1
|
module Canfig
|
2
2
|
class Config
|
3
3
|
|
4
|
-
def configure(argh={}, &block)
|
4
|
+
def configure(argh={}, file=nil, &block)
|
5
5
|
save_state! do
|
6
|
+
configure_with_file file unless file.nil?
|
6
7
|
configure_with_args argh
|
7
8
|
configure_with_block &block
|
8
9
|
end
|
9
10
|
self
|
10
11
|
end
|
11
12
|
|
13
|
+
def configure_with_file(file)
|
14
|
+
save_state! do
|
15
|
+
configure_with_args(Canfig::YAML.new(file).load)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
12
19
|
def configure_with_args(argh)
|
13
20
|
save_state! do
|
14
21
|
argh.symbolize_keys.each { |key,val| set(key, val) }
|
@@ -47,7 +54,7 @@ module Canfig
|
|
47
54
|
end
|
48
55
|
|
49
56
|
def changed
|
50
|
-
Hash[@
|
57
|
+
Hash[@allowed.map { |key| [key, [@saved_state[key], @state[key]]] if @saved_state[key] != @state[key] }.compact]
|
51
58
|
end
|
52
59
|
|
53
60
|
def changed?(key)
|
@@ -102,7 +109,7 @@ module Canfig
|
|
102
109
|
|
103
110
|
def initialize(*args, &block)
|
104
111
|
options = args.extract_options!
|
105
|
-
@allowed = (args + options.symbolize_keys.keys).uniq
|
112
|
+
@allowed = (args.map(&:to_sym) + options.symbolize_keys.keys).uniq
|
106
113
|
@state = {}
|
107
114
|
enable_state_saves!
|
108
115
|
configure options, &block
|
data/lib/canfig/module.rb
CHANGED
@@ -3,7 +3,7 @@ require 'active_support/core_ext/module/attribute_accessors'
|
|
3
3
|
module Canfig
|
4
4
|
module Module
|
5
5
|
def self.included(base)
|
6
|
-
base.send :
|
6
|
+
base.send :mattr_reader, :configuration
|
7
7
|
base.send :class_variable_set, :@@configuration, Canfig::OpenConfig.new
|
8
8
|
base.send :extend, ClassMethods
|
9
9
|
end
|
data/lib/canfig/open_config.rb
CHANGED
data/lib/canfig/version.rb
CHANGED
data/lib/canfig/yaml.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Canfig
|
2
|
+
class YAML
|
3
|
+
attr_reader :file
|
4
|
+
|
5
|
+
def initialize(file)
|
6
|
+
@file = file
|
7
|
+
end
|
8
|
+
|
9
|
+
def load
|
10
|
+
@data ||= ::YAML.load_file(file).symbolize_keys
|
11
|
+
end
|
12
|
+
alias_method :data, :load
|
13
|
+
|
14
|
+
def reload
|
15
|
+
@data = nil
|
16
|
+
self.load
|
17
|
+
end
|
18
|
+
|
19
|
+
def write
|
20
|
+
File.open(file, 'w') do |f|
|
21
|
+
f.write(::YAML.dump(data))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/canfig.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Canfig::YAML do
|
4
|
+
subject { described_class.new('spec/fixtures/config.yml') }
|
5
|
+
|
6
|
+
describe '#load' do
|
7
|
+
it 'loads the file into a hash' do
|
8
|
+
expect(subject.load).to be_a(Hash)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#write' do
|
13
|
+
subject { described_class.new('spec/fixtures/test.yml') }
|
14
|
+
|
15
|
+
before do
|
16
|
+
FileUtils.cp 'spec/fixtures/config.yml', 'spec/fixtures/test.yml'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'writes the hash back to the file in YAML' do
|
20
|
+
subject.data[:foo] = 'bar'
|
21
|
+
subject.write
|
22
|
+
subject.reload
|
23
|
+
expect(subject.data[:foo]).to eq('bar')
|
24
|
+
end
|
25
|
+
|
26
|
+
after do
|
27
|
+
FileUtils.rm 'spec/fixtures/test.yml'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#reload' do
|
32
|
+
it 'reloads the data from the file' do
|
33
|
+
subject.data[:foo] = 'bar'
|
34
|
+
subject.reload
|
35
|
+
expect(subject.data[:foo]).to eq('foo')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -66,9 +66,12 @@ files:
|
|
66
66
|
- lib/canfig/module.rb
|
67
67
|
- lib/canfig/open_config.rb
|
68
68
|
- lib/canfig/version.rb
|
69
|
+
- lib/canfig/yaml.rb
|
69
70
|
- spec/canfig/config_spec.rb
|
70
71
|
- spec/canfig/open_config_spec.rb
|
72
|
+
- spec/canfig/yaml_spec.rb
|
71
73
|
- spec/canfig_spec.rb
|
74
|
+
- spec/fixtures/config.yml
|
72
75
|
- spec/spec_helper.rb
|
73
76
|
homepage: http://github.com/markrebec/canfig
|
74
77
|
licenses: []
|
@@ -89,12 +92,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
92
|
version: '0'
|
90
93
|
requirements: []
|
91
94
|
rubyforge_project:
|
92
|
-
rubygems_version: 2.
|
95
|
+
rubygems_version: 2.4.8
|
93
96
|
signing_key:
|
94
97
|
specification_version: 4
|
95
98
|
summary: Dead simple canned configuration for gems or whatever
|
96
99
|
test_files:
|
97
100
|
- spec/canfig/config_spec.rb
|
98
101
|
- spec/canfig/open_config_spec.rb
|
102
|
+
- spec/canfig/yaml_spec.rb
|
99
103
|
- spec/canfig_spec.rb
|
104
|
+
- spec/fixtures/config.yml
|
100
105
|
- spec/spec_helper.rb
|