parameters 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +1 -1
- data/ChangeLog.md +11 -0
- data/Rakefile +2 -0
- data/lib/parameters/class_methods.rb +4 -0
- data/lib/parameters/param.rb +50 -5
- data/lib/parameters/version.rb +1 -1
- data/parameters.gemspec +45 -40
- data/spec/class_param_spec.rb +32 -0
- data/spec/classes/custom_type.rb +9 -0
- data/spec/classes/module_parameters.rb +5 -0
- data/spec/instance_param_spec.rb +32 -0
- data/spec/parameters_spec.rb +17 -2
- metadata +35 -16
data/.yardopts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--markup markdown --title 'Parameters Documentation' --protected --files ChangeLog.md,LICENSE.txt
|
1
|
+
--quiet --markup markdown --title 'Parameters Documentation' --protected --files ChangeLog.md,LICENSE.txt
|
data/ChangeLog.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
### 0.2.1 / 2010-05-05
|
2
|
+
|
3
|
+
* Added {Parameters::ClassMethods#included}, so that
|
4
|
+
{Parameters::ClassMethods} can be re-extended by mixin modules which
|
5
|
+
include {Parameters}.
|
6
|
+
* Added {Parameters::Param#coerce_hash}.
|
7
|
+
* Allow a `Proc` to be given as the coercion type for
|
8
|
+
{Parameters::Param#coerce_type}.
|
9
|
+
* Allow custom classes to be used for coercion types.
|
10
|
+
* Ruby 1.8.6 bug fixes for {Parameters::Param#coerce_type}.
|
11
|
+
|
1
12
|
### 0.2.0 / 2010-02-18
|
2
13
|
|
3
14
|
* Migrated to [Jeweler](http://github.com/technicalpickles/jeweler)
|
data/Rakefile
CHANGED
@@ -7,6 +7,7 @@ begin
|
|
7
7
|
Jeweler::Tasks.new do |gem|
|
8
8
|
gem.name = 'parameters'
|
9
9
|
gem.version = Parameters::VERSION
|
10
|
+
gem.license = 'MIT'
|
10
11
|
gem.summary = %Q{Allows you to add annotated variables to your classes}
|
11
12
|
gem.description = %Q{Parameters allows you to add annotated variables to your classes which may have configurable default values.}
|
12
13
|
gem.email = 'postmodern.mod3@gmail.com'
|
@@ -16,6 +17,7 @@ begin
|
|
16
17
|
gem.add_development_dependency 'yard', '>= 0.5.3'
|
17
18
|
gem.has_rdoc = 'yard'
|
18
19
|
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
19
21
|
rescue LoadError
|
20
22
|
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
23
|
end
|
data/lib/parameters/param.rb
CHANGED
@@ -36,6 +36,7 @@ module Parameters
|
|
36
36
|
|
37
37
|
# Type classes and their coercion methods
|
38
38
|
TYPE_COERSION = {
|
39
|
+
Hash => :coerce_hash,
|
39
40
|
Set => :coerce_set,
|
40
41
|
Array => :coerce_array,
|
41
42
|
URI => :coerce_uri,
|
@@ -52,8 +53,9 @@ module Parameters
|
|
52
53
|
#
|
53
54
|
# Coerces a given value into a specific type.
|
54
55
|
#
|
55
|
-
# @param [Class] type
|
56
|
-
# The type to coerce the value into.
|
56
|
+
# @param [Class, Proc] type
|
57
|
+
# The type to coerce the value into. If a Proc is given, it will be
|
58
|
+
# called with the value to coerce.
|
57
59
|
#
|
58
60
|
# @param [Object] value
|
59
61
|
# The value to coerce.
|
@@ -66,18 +68,34 @@ module Parameters
|
|
66
68
|
def coerce_type(type,value)
|
67
69
|
if value.nil?
|
68
70
|
nil
|
71
|
+
elsif type.kind_of?(Hash)
|
72
|
+
key_type, value_type = type.entries.first
|
73
|
+
new_hash = {}
|
74
|
+
|
75
|
+
coerce_hash(Hash,value).each do |key,value|
|
76
|
+
key = coerce_type(key_type,key)
|
77
|
+
value = coerce_type(value_type,value)
|
78
|
+
|
79
|
+
new_hash[key] = value
|
80
|
+
end
|
81
|
+
|
82
|
+
return new_hash
|
69
83
|
elsif type.kind_of?(Set)
|
70
84
|
coerce_array(Array,value).map { |element|
|
71
|
-
coerce_type(type.first,element)
|
85
|
+
coerce_type(type.entries.first,element)
|
72
86
|
}.to_set
|
73
87
|
elsif type.kind_of?(Array)
|
74
88
|
coerce_array(Array,value).map do |element|
|
75
|
-
coerce_type(type.first,element)
|
89
|
+
coerce_type(type.entries.first,element)
|
76
90
|
end
|
91
|
+
elsif type.kind_of?(Proc)
|
92
|
+
type.call(value)
|
77
93
|
elsif (method_name = TYPE_COERSION[type])
|
78
94
|
self.send(method_name,type,value)
|
79
|
-
|
95
|
+
elsif (type.nil? || type == Object)
|
80
96
|
value
|
97
|
+
else
|
98
|
+
type.new(value)
|
81
99
|
end
|
82
100
|
end
|
83
101
|
|
@@ -96,6 +114,33 @@ module Parameters
|
|
96
114
|
coerce_type(@type,value)
|
97
115
|
end
|
98
116
|
|
117
|
+
#
|
118
|
+
# Coerces a given value into a `Hash`.
|
119
|
+
#
|
120
|
+
# @param [Hash{Class => Class}] type
|
121
|
+
# An optional `Set` containing the type to coerce the keys and values
|
122
|
+
# of the given value to.
|
123
|
+
#
|
124
|
+
# @param [Hash, #to_hash, Object] value
|
125
|
+
# The value to coerce into a `Hash`.
|
126
|
+
#
|
127
|
+
# @return [Hash]
|
128
|
+
# The coerced value.
|
129
|
+
#
|
130
|
+
# @since 0.2.1
|
131
|
+
#
|
132
|
+
def coerce_hash(type,value)
|
133
|
+
if value.kind_of?(Hash)
|
134
|
+
value
|
135
|
+
elsif value.kind_of?(Array)
|
136
|
+
Hash[*value]
|
137
|
+
elsif value.respond_to?(:to_hash)
|
138
|
+
value.to_hash
|
139
|
+
else
|
140
|
+
{value => true}
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
99
144
|
#
|
100
145
|
# Coerces a given value into a `Set`.
|
101
146
|
#
|
data/lib/parameters/version.rb
CHANGED
data/parameters.gemspec
CHANGED
@@ -5,64 +5,69 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{parameters}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Postmodern"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-05-05}
|
13
13
|
s.description = %q{Parameters allows you to add annotated variables to your classes which may have configurable default values.}
|
14
14
|
s.email = %q{postmodern.mod3@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"ChangeLog.md",
|
17
|
-
|
18
|
-
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.md"
|
19
19
|
]
|
20
20
|
s.files = [
|
21
21
|
".gitignore",
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
22
|
+
".specopts",
|
23
|
+
".yardopts",
|
24
|
+
"ChangeLog.md",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"lib/parameters.rb",
|
29
|
+
"lib/parameters/class_methods.rb",
|
30
|
+
"lib/parameters/class_param.rb",
|
31
|
+
"lib/parameters/exceptions.rb",
|
32
|
+
"lib/parameters/exceptions/missing_param.rb",
|
33
|
+
"lib/parameters/exceptions/param_not_found.rb",
|
34
|
+
"lib/parameters/extensions.rb",
|
35
|
+
"lib/parameters/extensions/meta.rb",
|
36
|
+
"lib/parameters/extensions/meta/object.rb",
|
37
|
+
"lib/parameters/instance_param.rb",
|
38
|
+
"lib/parameters/param.rb",
|
39
|
+
"lib/parameters/parameters.rb",
|
40
|
+
"lib/parameters/version.rb",
|
41
|
+
"parameters.gemspec",
|
42
|
+
"spec/class_param_spec.rb",
|
43
|
+
"spec/classes/custom_parameters.rb",
|
44
|
+
"spec/classes/custom_type.rb",
|
45
|
+
"spec/classes/inherited_parameters.rb",
|
46
|
+
"spec/classes/module_parameters.rb",
|
47
|
+
"spec/classes/other_parameters.rb",
|
48
|
+
"spec/classes/test_parameters.rb",
|
49
|
+
"spec/instance_param_spec.rb",
|
50
|
+
"spec/parameters_spec.rb",
|
51
|
+
"spec/spec_helper.rb"
|
50
52
|
]
|
51
53
|
s.has_rdoc = %q{yard}
|
52
54
|
s.homepage = %q{http://github.com/postmodern/parameters}
|
55
|
+
s.licenses = ["MIT"]
|
53
56
|
s.rdoc_options = ["--charset=UTF-8"]
|
54
57
|
s.require_paths = ["lib"]
|
55
|
-
s.rubygems_version = %q{1.3.
|
58
|
+
s.rubygems_version = %q{1.3.6}
|
56
59
|
s.summary = %q{Allows you to add annotated variables to your classes}
|
57
60
|
s.test_files = [
|
58
|
-
"spec/parameters_spec.rb",
|
59
61
|
"spec/spec_helper.rb",
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
"spec/class_param_spec.rb",
|
63
|
+
"spec/parameters_spec.rb",
|
64
|
+
"spec/instance_param_spec.rb",
|
65
|
+
"spec/classes/inherited_parameters.rb",
|
66
|
+
"spec/classes/custom_type.rb",
|
67
|
+
"spec/classes/test_parameters.rb",
|
68
|
+
"spec/classes/module_parameters.rb",
|
69
|
+
"spec/classes/custom_parameters.rb",
|
70
|
+
"spec/classes/other_parameters.rb"
|
66
71
|
]
|
67
72
|
|
68
73
|
if s.respond_to? :specification_version then
|
data/spec/class_param_spec.rb
CHANGED
@@ -1,10 +1,25 @@
|
|
1
1
|
require 'parameters/class_param'
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
|
+
require 'classes/custom_type'
|
4
5
|
require 'set'
|
5
6
|
|
6
7
|
describe Parameters::ClassParam do
|
7
8
|
describe "type coercion" do
|
9
|
+
it "should coerce values into Hashes" do
|
10
|
+
param = Parameters::ClassParam.new(:x,Hash)
|
11
|
+
|
12
|
+
param.value = 2
|
13
|
+
param.value.should == {2 => true}
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should coerce keys and values within Hashes" do
|
17
|
+
param = Parameters::ClassParam.new(:x,Hash[Symbol => Object])
|
18
|
+
|
19
|
+
param.value = {'a' => true}
|
20
|
+
param.value.should == {:a => true}
|
21
|
+
end
|
22
|
+
|
8
23
|
it "should coerce values into Sets" do
|
9
24
|
param = Parameters::ClassParam.new(:x,Set)
|
10
25
|
|
@@ -152,6 +167,23 @@ describe Parameters::ClassParam do
|
|
152
167
|
param.value.should == false
|
153
168
|
end
|
154
169
|
|
170
|
+
it "should coerce values using a Proc for the type" do
|
171
|
+
coercion_logic = lambda { |x| x.to_i.floor }
|
172
|
+
param = Parameters::ClassParam.new(:x,coercion_logic)
|
173
|
+
|
174
|
+
param.value = 2.5
|
175
|
+
param.value.should == 2
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should coerce values using custom Classes" do
|
179
|
+
original_value = Object.new
|
180
|
+
param = Parameters::ClassParam.new(:x,CustomType)
|
181
|
+
|
182
|
+
param.value = original_value
|
183
|
+
param.value.class.should == CustomType
|
184
|
+
param.value.value.should == original_value
|
185
|
+
end
|
186
|
+
|
155
187
|
it "should not coerce nil into a type" do
|
156
188
|
param = Parameters::ClassParam.new(:x,String)
|
157
189
|
obj1 = Object.new
|
data/spec/instance_param_spec.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'parameters/instance_param'
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
|
+
require 'classes/custom_type'
|
4
5
|
require 'set'
|
5
6
|
|
6
7
|
describe Parameters::InstanceParam do
|
@@ -25,6 +26,20 @@ describe Parameters::InstanceParam do
|
|
25
26
|
@obj = Object.new
|
26
27
|
end
|
27
28
|
|
29
|
+
it "should coerce values into Hashes" do
|
30
|
+
param = Parameters::InstanceParam.new(@obj,:x,Hash)
|
31
|
+
|
32
|
+
param.value = 2
|
33
|
+
param.value.should == {2 => true}
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should coerce keys and values within Hashes" do
|
37
|
+
param = Parameters::InstanceParam.new(@obj,:x,Hash[Symbol => Object])
|
38
|
+
|
39
|
+
param.value = {'a' => true}
|
40
|
+
param.value.should == {:a => true}
|
41
|
+
end
|
42
|
+
|
28
43
|
it "should coerce values into Sets" do
|
29
44
|
param = Parameters::InstanceParam.new(@obj,:x,Set)
|
30
45
|
|
@@ -172,6 +187,23 @@ describe Parameters::InstanceParam do
|
|
172
187
|
param.value.should == false
|
173
188
|
end
|
174
189
|
|
190
|
+
it "should coerce values using a Proc for the type" do
|
191
|
+
coercion_logic = lambda { |x| x.to_i.floor }
|
192
|
+
param = Parameters::InstanceParam.new(@obj,:x,coercion_logic)
|
193
|
+
|
194
|
+
param.value = 2.5
|
195
|
+
param.value.should == 2
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should coerce values using custom Classes" do
|
199
|
+
original_value = Object.new
|
200
|
+
param = Parameters::InstanceParam.new(@obj,:x,CustomType)
|
201
|
+
|
202
|
+
param.value = original_value
|
203
|
+
param.value.class.should == CustomType
|
204
|
+
param.value.value.should == original_value
|
205
|
+
end
|
206
|
+
|
175
207
|
it "should not coerce nil into a type" do
|
176
208
|
param = Parameters::InstanceParam.new(@obj,:x,String)
|
177
209
|
|
data/spec/parameters_spec.rb
CHANGED
@@ -2,13 +2,28 @@ require 'parameters/parameters'
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
+
require 'classes/module_parameters'
|
5
6
|
require 'classes/test_parameters'
|
6
7
|
require 'classes/custom_parameters'
|
7
8
|
require 'classes/inherited_parameters'
|
8
9
|
require 'classes/other_parameters'
|
9
10
|
|
10
11
|
describe Parameters do
|
11
|
-
|
12
|
+
context "in a Module" do
|
13
|
+
it "should provide parameters" do
|
14
|
+
ModuleParameters.should have_param(:mixin_var)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should re-extend ClassMethods when including the module" do
|
18
|
+
new_class = Class.new do
|
19
|
+
include ModuleParameters
|
20
|
+
end
|
21
|
+
|
22
|
+
new_class.should have_param(:mixin_var)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "in a Class" do
|
12
27
|
it "should provide parameters" do
|
13
28
|
TestParameters.params.should_not be_empty
|
14
29
|
end
|
@@ -76,7 +91,7 @@ describe Parameters do
|
|
76
91
|
end
|
77
92
|
end
|
78
93
|
|
79
|
-
|
94
|
+
context "in an Object" do
|
80
95
|
before(:each) do
|
81
96
|
@test = TestParameters.new
|
82
97
|
@test_inherited = InheritedParameters.new
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Postmodern
|
@@ -9,29 +14,37 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-05-05 00:00:00 -07:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: rspec
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
30
|
+
- 0
|
23
31
|
version: 1.3.0
|
24
|
-
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
25
34
|
- !ruby/object:Gem::Dependency
|
26
35
|
name: yard
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 5
|
44
|
+
- 3
|
33
45
|
version: 0.5.3
|
34
|
-
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
35
48
|
description: Parameters allows you to add annotated variables to your classes which may have configurable default values.
|
36
49
|
email: postmodern.mod3@gmail.com
|
37
50
|
executables: []
|
@@ -66,7 +79,9 @@ files:
|
|
66
79
|
- parameters.gemspec
|
67
80
|
- spec/class_param_spec.rb
|
68
81
|
- spec/classes/custom_parameters.rb
|
82
|
+
- spec/classes/custom_type.rb
|
69
83
|
- spec/classes/inherited_parameters.rb
|
84
|
+
- spec/classes/module_parameters.rb
|
70
85
|
- spec/classes/other_parameters.rb
|
71
86
|
- spec/classes/test_parameters.rb
|
72
87
|
- spec/instance_param_spec.rb
|
@@ -74,8 +89,8 @@ files:
|
|
74
89
|
- spec/spec_helper.rb
|
75
90
|
has_rdoc: yard
|
76
91
|
homepage: http://github.com/postmodern/parameters
|
77
|
-
licenses:
|
78
|
-
|
92
|
+
licenses:
|
93
|
+
- MIT
|
79
94
|
post_install_message:
|
80
95
|
rdoc_options:
|
81
96
|
- --charset=UTF-8
|
@@ -85,27 +100,31 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
100
|
requirements:
|
86
101
|
- - ">="
|
87
102
|
- !ruby/object:Gem::Version
|
103
|
+
segments:
|
104
|
+
- 0
|
88
105
|
version: "0"
|
89
|
-
version:
|
90
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
107
|
requirements:
|
92
108
|
- - ">="
|
93
109
|
- !ruby/object:Gem::Version
|
110
|
+
segments:
|
111
|
+
- 0
|
94
112
|
version: "0"
|
95
|
-
version:
|
96
113
|
requirements: []
|
97
114
|
|
98
115
|
rubyforge_project:
|
99
|
-
rubygems_version: 1.3.
|
116
|
+
rubygems_version: 1.3.6
|
100
117
|
signing_key:
|
101
118
|
specification_version: 3
|
102
119
|
summary: Allows you to add annotated variables to your classes
|
103
120
|
test_files:
|
104
|
-
- spec/parameters_spec.rb
|
105
121
|
- spec/spec_helper.rb
|
106
122
|
- spec/class_param_spec.rb
|
123
|
+
- spec/parameters_spec.rb
|
107
124
|
- spec/instance_param_spec.rb
|
108
125
|
- spec/classes/inherited_parameters.rb
|
126
|
+
- spec/classes/custom_type.rb
|
109
127
|
- spec/classes/test_parameters.rb
|
128
|
+
- spec/classes/module_parameters.rb
|
110
129
|
- spec/classes/custom_parameters.rb
|
111
130
|
- spec/classes/other_parameters.rb
|