canfig 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87d7a71f3425215cb0226cd0f6eeabc7912dd552
4
- data.tar.gz: 378a6def629c7ed263384211bf8361fbe28338a6
3
+ metadata.gz: 19205ebc7b976f9e5d9f15a0a891699687f48c28
4
+ data.tar.gz: a1832b75a3870d72dbc3c303c2a1c154bd51db34
5
5
  SHA512:
6
- metadata.gz: cbf3b023b2f8853dcbd0d5731baa98f9de2995ece01982da2718fe7919724efb7b4de7d350054330cf0749aaa7a5cd3c362f74db340e42c4521ca993800bbe09
7
- data.tar.gz: bb8e16f4eaee8db347246e7e45e5abcf16a673c1bb1eacab89b76f930a8eeac3d4abee81c638d208ef061e1b2115286bab8608078f5d09919ef949a3da65c14d
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[@state.map { |key,val| [key, [@saved_state[key], val]] if @saved_state[key] != val }.compact]
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.map(&:to_sym)
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 :cattr_reader, :configuration
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
@@ -1,11 +1,12 @@
1
1
  module Canfig
2
2
  class OpenConfig < Config
3
- def allowed?(opt)
4
- true
3
+ def set(key, val)
4
+ super(key, val)
5
+ @allowed << key unless @allowed.include?(key)
5
6
  end
6
7
 
7
- def to_h
8
- @state.to_h
8
+ def allowed?(opt)
9
+ true
9
10
  end
10
11
  end
11
12
  end
@@ -1,3 +1,3 @@
1
1
  module Canfig
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -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
@@ -1,6 +1,7 @@
1
1
  require 'active_support/core_ext/array/extract_options'
2
2
  require 'active_support/core_ext/hash/keys'
3
3
  require 'canfig/version'
4
+ require 'canfig/yaml'
4
5
  require 'canfig/config'
5
6
  require 'canfig/open_config'
6
7
  require 'canfig/module'
@@ -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
@@ -0,0 +1,3 @@
1
+ foo: 'foo'
2
+ bar: 'bar'
3
+ baz: 'baz'
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.5
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: 2015-05-21 00:00:00.000000000 Z
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.2.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