configue 0.1.3 → 0.1.4

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: 86aff7ac75739e3642c097da1e578ba446fb9cdf
4
- data.tar.gz: 13da63cb35000679ab923d6de5f608ca61f97623
3
+ metadata.gz: 2c8e92bfb0e0054f24a36d936fe8d6ff0aded7f0
4
+ data.tar.gz: f1dd22096a2c57bd9c4e6d2989a0462bf2cbdcc9
5
5
  SHA512:
6
- metadata.gz: 73a694a548c2e4a3d0f0e6d5ddb45af82759281289ad18ddb5b22e11415d634cf0df90890841872ac217bf4fc8adc6787587420042645ee2e0a3bfd37455b594
7
- data.tar.gz: 91a904450a5a18a26eacef5910289a2aebdf2e30e4198b6d521cd37049f2a0e573ef1105cf538d84b68718ffece338e6be652d878cfc20f69c54ac218b492f7d
6
+ metadata.gz: 9124dc2d54f63952dd029a593eac75a79852af3beb2bbc8481eee04f20433dab65e359a25351cc0c087a969a00f7e07cb0d5f31657feb5a28342362606760a52
7
+ data.tar.gz: 692fbbbded7516817eae301166073c028b6cd706aebe488e3ba7c86d7009c08b5e18cbf4cdcdfb1b13dd12db734750c3ee3ac484fd7b807f0d40be3b99ced95c
data/README.md CHANGED
@@ -20,28 +20,30 @@ end
20
20
  ### Write your settings in YAML file
21
21
  ```yaml
22
22
  # config/accounts/admin_users.yml
23
- accounts:
24
- admin_users:
25
- - grumpy
26
- - sneezy
23
+ config:
24
+ accounts:
25
+ admin_users:
26
+ - grumpy
27
+ - sneezy
27
28
  ```
28
29
 
29
30
  You can make multiple settings files.
30
31
 
31
32
  ```yaml
32
33
  # config/accounts/test_users.yml
33
- accounts:
34
- test_users:
35
- - sleepy
36
- - dopey
34
+ config:
35
+ accounts:
36
+ test_users:
37
+ - sleepy
38
+ - dopey
37
39
  ```
38
40
 
39
41
  ### Access your settings
40
42
  ```
41
- >> MyConf.accounts.admin_users
43
+ >> MyConf.config.accounts.admin_users
42
44
  => ["grumpy", "sneezy"]
43
45
 
44
- >> MyConf.accounts.test_users
46
+ >> MyConf.config.accounts.test_users
45
47
  => ["sleepy", "dopey"]
46
48
  ```
47
49
 
data/configue.gemspec CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
12
12
  s.files = `git ls-files`.split("\n")
13
13
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
14
  s.require_paths = ["lib"]
15
- s.version = "0.1.3"
16
- s.license = "MIT"
15
+ s.version = "0.1.4"
16
+ s.license = "MIT"
17
17
 
18
18
  s.add_development_dependency 'rspec'
19
19
  end
@@ -0,0 +1,107 @@
1
+ # coding: utf-8
2
+
3
+ module Configue
4
+ module ArrayNode
5
+ [ :first,
6
+ :second,
7
+ :third,
8
+ :fourth,
9
+ :fifth,
10
+ :sixth,
11
+ :seventh,
12
+ :eighth,
13
+ :ninth,
14
+ :tenth,
15
+ ].each_with_index do |m, n|
16
+ define_method(m, -> { self[n] })
17
+ end
18
+
19
+ [ :&,
20
+ :*,
21
+ :+,
22
+ :-,
23
+ :|,
24
+ :<=>,
25
+ :==,
26
+ :[],
27
+ :all?,
28
+ :any?,
29
+ :assoc,
30
+ :at,
31
+ :chunk,
32
+ :collect,
33
+ :combination,
34
+ :compact,
35
+ :count,
36
+ :cycle,
37
+ :detect,
38
+ :drop,
39
+ :drop_while,
40
+ :each,
41
+ :each_index,
42
+ :each_slice,
43
+ :each_with_index,
44
+ :each_with_object,
45
+ :empty?,
46
+ :entries,
47
+ :eql?,
48
+ :fetch,
49
+ :find,
50
+ :find_all,
51
+ :find_index,
52
+ :flat_map,
53
+ :flattern,
54
+ :frozen?,
55
+ :grep,
56
+ :group_by,
57
+ :hash,
58
+ :include?,
59
+ :index,
60
+ :inject,
61
+ :join,
62
+ :length,
63
+ :map,
64
+ :max,
65
+ :max_by,
66
+ :member?,
67
+ :min,
68
+ :min_by,
69
+ :minmax,
70
+ :minmax_by,
71
+ :none?,
72
+ :one?,
73
+ :pack,
74
+ :partition,
75
+ :product,
76
+ :reassoc,
77
+ :reduce,
78
+ :reject,
79
+ :repeated_combination,
80
+ :repeated_permutation,
81
+ :reverse,
82
+ :reverse_each,
83
+ :rindex,
84
+ :rotate,
85
+ :sample,
86
+ :select,
87
+ :shuffle,
88
+ :size,
89
+ :slice,
90
+ :slice_before,
91
+ :sort,
92
+ :sort_by,
93
+ :take,
94
+ :take_while,
95
+ :to_a,
96
+ :to_ary,
97
+ :transpose,
98
+ :uniq,
99
+ :values_at,
100
+ :zip,
101
+ ].each do |m|
102
+ define_method(m, ->(*args, &block) {
103
+ @hash.__send__(m, *args, &block)
104
+ })
105
+ end
106
+ end
107
+ end
@@ -69,7 +69,7 @@ module Configue
69
69
  # makes @instance in this line.
70
70
  @setting.load!
71
71
 
72
- if @instance.key?(name)
72
+ if @instance.respond_to?(:key?) and @instance.key?(name)
73
73
  @instance[name]
74
74
  else
75
75
  @instance.__send__(name, *args, &block)
@@ -15,13 +15,15 @@ module Configue
15
15
  end
16
16
 
17
17
  def instance=(arg)
18
- raise TypeError unless arg.respond_to?(:[]) or arg.respond_to?(:keys)
18
+ raise TypeError unless arg.respond_to?(:[])
19
19
 
20
20
  @class.instance_variable_set(:@instance, arg)
21
- sig = class << @class; self; end
22
- arg.keys.each do |k|
23
- next unless k.to_s =~ /\A\w[\w0-9]*\z/
24
- sig.__send__(:define_method, k, -> { arg[k] })
21
+ if arg.respond_to?(:keys)
22
+ sig = class << @class; self; end
23
+ arg.keys.each do |k|
24
+ next unless k.to_s =~ /\A\w[\w0-9]*\z/
25
+ sig.__send__(:define_method, k, -> { arg[k] })
26
+ end
25
27
  end
26
28
  arg
27
29
  end
@@ -0,0 +1,68 @@
1
+ # coding: utf-8
2
+
3
+ module Configue
4
+ module HashNode
5
+ def [](key)
6
+ k = key.to_s
7
+ v = @hash[k]
8
+ @hash[k] = self.class.new(v) if node?(v)
9
+ @hash[k]
10
+ end
11
+
12
+ def fetch(key)
13
+ k = key.to_s
14
+ v = @hash[key]
15
+ @hash[k] = self.class.new(v) if node?(v)
16
+ @hash.fetch(k)
17
+ end
18
+
19
+ def key?(key)
20
+ k = key.to_s
21
+ @hash.key?(k)
22
+ end
23
+ alias_method :has_key?, :key?
24
+ alias_method :include?, :key?
25
+ alias_method :member?, :key?
26
+
27
+ def assoc(key)
28
+ k = key.to_s
29
+ @hash.assoc(k)
30
+ end
31
+
32
+ def to_hash
33
+ @hash.dup
34
+ end
35
+
36
+ def values_at(*keys)
37
+ ks = keys.map {|k| k.to_s }
38
+ @hash.values_at(*ks)
39
+ end
40
+
41
+ [ :all?,
42
+ :any?,
43
+ :collect,
44
+ :count,
45
+ :cycle,
46
+ :keys,
47
+ :to_s,
48
+ :each,
49
+ :each_pair,
50
+ :each_key,
51
+ :empty?,
52
+ :value?,
53
+ :size,
54
+ :length,
55
+ :merge,
56
+ :rassoc,
57
+ :reject,
58
+ :select,
59
+ :sort,
60
+ :to_a,
61
+ :values,
62
+ ].each do |m|
63
+ define_method(m, ->(*args, &block) {
64
+ @hash.__send__(m, *args, &block)
65
+ })
66
+ end
67
+ end
68
+ end
@@ -2,19 +2,20 @@
2
2
 
3
3
  module Configue
4
4
  module Merger
5
+ MERGER = ->(key, h1, h2) {
6
+ if h1.is_a?(Hash) and h2.is_a?(Hash)
7
+ h1.merge(h2, &MERGER)
8
+ else
9
+ h2
10
+ end
11
+ }
12
+ private_constant :MERGER
13
+
5
14
  def merge(hash1, hash2)
6
15
  return hash2 unless hash1
7
16
  return hash1 unless hash2
8
17
 
9
- merger = ->(key, h1, h2) {
10
- if h1.is_a?(Hash) and h2.is_a?(Hash)
11
- h1.merge(h2, &merger)
12
- else
13
- h2
14
- end
15
- }
16
-
17
- hash1.merge(hash2, &merger)
18
+ hash1.merge(hash2, &MERGER)
18
19
  end
19
20
 
20
21
  module_function :merge
data/lib/configue/node.rb CHANGED
@@ -1,74 +1,45 @@
1
1
  # coding: utf-8
2
+ require 'configue/hash_node'
3
+ require 'configue/array_node'
2
4
 
3
5
  module Configue
4
6
  class Node
5
7
  def initialize(hash)
6
8
  raise TypeError unless hash.respond_to?(:[])
7
9
 
8
- sig = class << self; self; end
9
- @hash = hash.each.inject({}) do |h, (k, v)|
10
- sig.__send__(:define_method, k, ->{ self[k] })
11
- h[k.to_s] = v; h
10
+ node_type = hash.instance_variable_get(:@node_type) if hash.is_a? self.class
11
+ if hash.is_a? Hash or node_type == :hash
12
+ setup_as_hash_node(hash)
13
+ elsif hash.is_a? Array or node_type == :array
14
+ setup_as_array_node(hash)
12
15
  end
13
16
  self
14
17
  end
15
18
 
16
- def [](key)
17
- k = key.to_s
18
- v = @hash[k]
19
- @hash[k] = self.class.new(v) if v.is_a?(Hash)
20
- @hash[k]
21
- end
22
-
23
- def fetch(key)
24
- k = key.to_s
25
- v = @hash[key]
26
- @hash[k] = self.class.new(v) if v.is_a?(Hash)
27
- @hash.fetch(k)
28
- end
29
-
30
- def key?(key)
31
- k = key.to_s
32
- @hash.key?(k)
33
- end
34
- alias_method :has_key?, :key?
35
- alias_method :include?, :key?
36
- alias_method :member?, :key?
37
-
38
- def assoc(key)
39
- k = key.to_s
40
- @hash.assoc(k)
41
- end
42
-
43
- def to_hash
44
- @hash.dup
19
+ private
20
+ def node?(object, counter=0)
21
+ return true if object.is_a? Hash
22
+ return false if counter >= 3
23
+ return object.any? {|n| node?(n, counter + 1) } if object.is_a? Array
24
+ return false
45
25
  end
46
26
 
47
- def values_at(*keys)
48
- ks = keys.map {|k| k.to_s }
49
- @hash.values_at(*ks)
27
+ def setup_as_hash_node(hash)
28
+ sig = class << self; self; end
29
+ @hash = hash.each.inject({}) do |h, (k, v)|
30
+ sig.__send__(:define_method, k, ->{ self[k] })
31
+ h[k.to_s] = node?(v) ? self.class.new(v) : v; h
32
+ end
33
+ sig.__send__(:include, HashNode)
34
+ @node_type = :hash
50
35
  end
51
36
 
52
- [ :keys,
53
- :to_s,
54
- :each,
55
- :each_pair,
56
- :each_key,
57
- :empty?,
58
- :value?,
59
- :size,
60
- :length,
61
- :merge,
62
- :rassoc,
63
- :reject,
64
- :select,
65
- :sort,
66
- :to_a,
67
- :values,
68
- ].each do |m|
69
- define_method(m, ->(*args, &block) {
70
- @hash.__send__(m, *args, &block)
71
- })
37
+ def setup_as_array_node(array)
38
+ sig = class << self; self; end
39
+ @hash = array
40
+ @hash = array.map {|x| self.class.new(x) } if array.is_a? Array
41
+ sig.__send__(:include, ArrayNode)
42
+ @node_type = :array
72
43
  end
73
44
  end
74
45
  end
data/spec/basic_spec.rb CHANGED
@@ -62,4 +62,48 @@ describe "Configue::Container" do
62
62
  {"accounts" => ["admin_users"]}
63
63
  end
64
64
  end
65
+
66
+ context "when loading a yaml file with an array of records" do
67
+ require File.expand_path("../samples/array_records_conf", __FILE__)
68
+
69
+ describe "first record" do
70
+ describe ".has_key?" do
71
+ context 'when parameter is "name"' do
72
+ it "returns true" do
73
+ expect(ArrayRecordsConf.first).to have_key("name")
74
+ end
75
+ end
76
+
77
+ context 'when parameter is "email"' do
78
+ it "returns true" do
79
+ expect(ArrayRecordsConf.first).to have_key("email")
80
+ end
81
+ end
82
+
83
+ context "when parameter is :name" do
84
+ it "returns true" do
85
+ expect(ArrayRecordsConf.first).to have_key(:name)
86
+ end
87
+ end
88
+
89
+ context "when parameter is :email" do
90
+ it "returns true" do
91
+ expect(ArrayRecordsConf.first).to have_key(:email)
92
+ end
93
+ end
94
+
95
+ context 'when parameter is "foo"' do
96
+ it "returns false" do
97
+ expect(ArrayRecordsConf.first).not_to have_key("foo")
98
+ end
99
+ end
100
+
101
+ context "when parameter is :foo" do
102
+ it "returns false" do
103
+ expect(ArrayRecordsConf.first).not_to have_key(:foo)
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
65
109
  end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ require "configue"
3
+
4
+ class ArrayRecordsConf < Configue::Container
5
+ config.source_file "#{File.dirname(__FILE__)}/array_records_conf.yml"
6
+ end
@@ -0,0 +1,12 @@
1
+ - name: John Doe
2
+ email: john.doe@xxx.xx
3
+ friends:
4
+ - name: Jane Doe
5
+ email: jane.doe@xxx.xx
6
+ - name: Jonnie Roe
7
+ email: j.roe@xxx.xx
8
+ - name: Janie Roe
9
+ email: janie.roe@xxx.xx
10
+ friends:
11
+ - name: John Smith
12
+ email: john.smith@xxx.xx
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - hosim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-12 00:00:00.000000000 Z
11
+ date: 2014-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -37,9 +37,11 @@ files:
37
37
  - Rakefile
38
38
  - configue.gemspec
39
39
  - lib/configue.rb
40
+ - lib/configue/array_node.rb
40
41
  - lib/configue/container.rb
41
42
  - lib/configue/container_adapter.rb
42
43
  - lib/configue/criteria.rb
44
+ - lib/configue/hash_node.rb
43
45
  - lib/configue/importer.rb
44
46
  - lib/configue/merger.rb
45
47
  - lib/configue/node.rb
@@ -50,6 +52,8 @@ files:
50
52
  - spec/config_spec.rb
51
53
  - spec/configue/merger_spec.rb
52
54
  - spec/namespace_spec.rb
55
+ - spec/samples/array_records_conf.rb
56
+ - spec/samples/array_records_conf.yml
53
57
  - spec/samples/base_namespace_conf.rb
54
58
  - spec/samples/config/admin.yml
55
59
  - spec/samples/config/test.yml
@@ -95,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
99
  version: '0'
96
100
  requirements: []
97
101
  rubyforge_project:
98
- rubygems_version: 2.0.3
102
+ rubygems_version: 2.0.14
99
103
  signing_key:
100
104
  specification_version: 4
101
105
  summary: A simple configuration solution
@@ -104,6 +108,8 @@ test_files:
104
108
  - spec/config_spec.rb
105
109
  - spec/configue/merger_spec.rb
106
110
  - spec/namespace_spec.rb
111
+ - spec/samples/array_records_conf.rb
112
+ - spec/samples/array_records_conf.yml
107
113
  - spec/samples/base_namespace_conf.rb
108
114
  - spec/samples/config/admin.yml
109
115
  - spec/samples/config/test.yml