ruby-configurable 1.0.0 → 1.0.1
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.
- data/History.txt +10 -0
- data/README.txt +41 -24
- data/lib/configurable.rb +1 -1
- data/lib/configurable/config_struct.rb +17 -5
- data/lib/configurable/core_ext/extract_options.rb +20 -4
- data/test/test_config_struct.rb +20 -0
- metadata +7 -5
data/History.txt
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
=== 1.0.1 / 2011-05-06
|
2
|
+
|
3
|
+
* 2 bugfixes
|
4
|
+
|
5
|
+
* to_args now returns a Hash with symbols as keys. This lets you pass
|
6
|
+
your configuration directly to methods that expect option-style hash
|
7
|
+
arguments.
|
8
|
+
|
9
|
+
* to_args now excludes nil values from the resulting hash.
|
10
|
+
|
1
11
|
=== 1.0.0 / 2011-05-03
|
2
12
|
|
3
13
|
* 1 major enhancement
|
data/README.txt
CHANGED
@@ -40,51 +40,68 @@ Lets you make your Ruby class configurable with a simple mixin.
|
|
40
40
|
|
41
41
|
== SYNOPSIS:
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
require 'configurable'
|
44
|
+
class Doodad
|
45
|
+
include Configurable
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
# Declare the allowed options and their default values
|
48
|
+
configurable_options :foo => 'default',
|
49
|
+
:bar => {:quux => 42, :wibble => nil},
|
50
|
+
:baz => nil
|
51
|
+
end
|
52
52
|
|
53
|
-
|
54
|
-
|
53
|
+
Doodad::Config # => a ConfigStruct::Struct
|
54
|
+
Doodad::Config::Bar # => another ConfigStruct::Struct
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
56
|
+
Doodad.default_config # => #<struct Doodad::Config
|
57
|
+
# bar=#<struct Doodad::Config::Bar
|
58
|
+
# quux=42, wibble=nil>,
|
59
|
+
# baz=nil,
|
60
|
+
# foo="default">
|
61
61
|
|
62
|
-
|
63
|
-
|
62
|
+
Doodad.config # => a (deep) copy of default_config,
|
63
|
+
# ready for use
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
65
|
+
Doodad.config.replace( # replaces config with passed values
|
66
|
+
:foo => 'mine',
|
67
|
+
:bar => {:quux => 9})
|
68
68
|
|
69
|
-
|
70
|
-
|
69
|
+
Doodad.config.update( # loads config from a YAML file
|
70
|
+
YAML.load('config.yml'))
|
71
71
|
|
72
72
|
See the documentation on Configurable for an overview and links to more
|
73
73
|
specifics.
|
74
74
|
|
75
|
+
=== Using with Rails
|
76
|
+
|
77
|
+
If you want to make a Rails class configurable, you'll need some special
|
78
|
+
mojo to reconfigure the class when it gets reloaded in development mode.
|
79
|
+
You can do this with Rails.application.config.to_prepare. For example,
|
80
|
+
to have your class configuration reloaded from a YAML file, create an
|
81
|
+
initializer like so:
|
82
|
+
|
83
|
+
Rails.application.config.to_prepare do
|
84
|
+
begin
|
85
|
+
Doodad.config.update(
|
86
|
+
YAML.load_file(File.join(Rails.root, 'config', 'doodad.yml')) || {})
|
87
|
+
rescue Errno::ENOENT
|
88
|
+
# handle missing config file
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
75
92
|
== REQUIREMENTS:
|
76
93
|
|
77
94
|
* Ruby
|
78
95
|
|
79
96
|
== INSTALL:
|
80
97
|
|
81
|
-
|
98
|
+
$ gem install ruby-configurable
|
82
99
|
|
83
100
|
== DEVELOPERS:
|
84
101
|
|
85
102
|
After checking out the source, run:
|
86
103
|
|
87
|
-
|
104
|
+
$ rake newb
|
88
105
|
|
89
106
|
This task will install any missing dependencies, run the tests/specs,
|
90
107
|
and generate the RDoc.
|
data/lib/configurable.rb
CHANGED
@@ -116,11 +116,12 @@ module ConfigStruct
|
|
116
116
|
#
|
117
117
|
def to_hash
|
118
118
|
members.inject({}) do |hsh, k|
|
119
|
+
v = self[k]
|
119
120
|
hsh.tap do |h|
|
120
|
-
h[k] = if
|
121
|
-
|
121
|
+
h[k] = if v.is_a? Struct
|
122
|
+
v.to_hash
|
122
123
|
else
|
123
|
-
|
124
|
+
v
|
124
125
|
end
|
125
126
|
end
|
126
127
|
end
|
@@ -129,8 +130,19 @@ module ConfigStruct
|
|
129
130
|
# Puts config values into an array suitable for expanding into a
|
130
131
|
# parameter list. This is for convenience in recursive traversals of
|
131
132
|
# a struct instance.
|
132
|
-
def to_args
|
133
|
-
|
133
|
+
def to_args(box = true)
|
134
|
+
args = each_pair.inject({}) do |hsh, pair|
|
135
|
+
k, v = pair
|
136
|
+
hsh.tap do |h|
|
137
|
+
next if v.nil?
|
138
|
+
h[k.to_sym] = if v.is_a? Struct
|
139
|
+
v.to_args(false)
|
140
|
+
else
|
141
|
+
v
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
box ? args.to_args : args
|
134
146
|
end
|
135
147
|
|
136
148
|
|
@@ -22,7 +22,7 @@ class Array
|
|
22
22
|
|
23
23
|
# Returns the array in a form suitable for expanding into a parameter
|
24
24
|
# list, that is, just the array itself.
|
25
|
-
def to_args
|
25
|
+
def to_args(box = true)
|
26
26
|
self
|
27
27
|
end
|
28
28
|
end
|
@@ -39,11 +39,27 @@ class Hash
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
# needed for to_args
|
43
|
+
unless {}.respond_to? :symbolize_keys!
|
44
|
+
# Destructively convert all keys to symbols, as long as they respond
|
45
|
+
# to +to_sym+.
|
46
|
+
def symbolize_keys!
|
47
|
+
keys.each do |key|
|
48
|
+
self[(key.to_sym rescue key) || key] = delete(key)
|
49
|
+
end
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def symbolize_keys
|
54
|
+
dup.symbolize_keys!
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
42
58
|
# Puts the hash into an array suitable for expanding into a parameter
|
43
59
|
# list, such that it will be interpreted as keyword parameters if the
|
44
60
|
# method you're calling expects that.
|
45
|
-
def to_args
|
46
|
-
|
61
|
+
def to_args(box = true)
|
62
|
+
args = self.symbolize_keys
|
63
|
+
box ? [args] : args
|
47
64
|
end
|
48
65
|
end
|
49
|
-
|
data/test/test_config_struct.rb
CHANGED
@@ -27,6 +27,26 @@ describe ConfigStruct::Struct do
|
|
27
27
|
assert_equal 42, inst.to_hash['a']
|
28
28
|
end
|
29
29
|
|
30
|
+
describe 'to_args' do
|
31
|
+
before do
|
32
|
+
@inst = @struct.new(42, 'foobar')
|
33
|
+
@args = @inst.to_args
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should convert to option args' do
|
37
|
+
# should be a hash wrapped in an array
|
38
|
+
assert_respond_to @args, :[]
|
39
|
+
assert_respond_to @args.first, :keys
|
40
|
+
assert_respond_to @args.first, :to_hash
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should have symbol keys' do
|
44
|
+
for key in @args.first.keys
|
45
|
+
assert_kind_of Symbol, key
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
30
50
|
describe 'replace' do
|
31
51
|
before do
|
32
52
|
@inst = @struct.new(5, 3)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-configurable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kevin R. Bullock
|
@@ -15,7 +15,8 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-06 00:00:00 -05:00
|
19
|
+
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: rubyforge
|
@@ -74,6 +75,7 @@ files:
|
|
74
75
|
- lib/configurable/core_ext/inflections.rb
|
75
76
|
- test/test_config_struct.rb
|
76
77
|
- test/test_configurable.rb
|
78
|
+
has_rdoc: true
|
77
79
|
homepage: http://bitbucket.org/krbullock/configurable
|
78
80
|
licenses: []
|
79
81
|
|
@@ -104,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
106
|
requirements: []
|
105
107
|
|
106
108
|
rubyforge_project: handtools
|
107
|
-
rubygems_version: 1.
|
109
|
+
rubygems_version: 1.6.1
|
108
110
|
signing_key:
|
109
111
|
specification_version: 3
|
110
112
|
summary: Lets you make your Ruby class configurable with a simple mixin.
|