hashmake 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.
- data/ChangeLog.rdoc +5 -1
- data/README.rdoc +21 -3
- data/lib/hashmake/hash_makeable.rb +3 -1
- data/lib/hashmake/version.rb +1 -1
- data/spec/hash_makeable_spec.rb +52 -1
- metadata +3 -3
data/ChangeLog.rdoc
CHANGED
@@ -16,4 +16,8 @@ Removed README.md so YARD could run without the redcarpet gem.
|
|
16
16
|
=== 0.1.3 / 2013-02-06
|
17
17
|
|
18
18
|
For the ArgSpec class, change :array key to :container, which can be set to any value included in ArgSpec::CONTAINERS.
|
19
|
-
In hash_make, add support for Hash containers which contain objects of type :type. Suppor for Array container is still in place.
|
19
|
+
In hash_make, add support for Hash containers which contain objects of type :type. Suppor for Array container is still in place.
|
20
|
+
|
21
|
+
=== 0.1.4 / 2013-02-06
|
22
|
+
|
23
|
+
Add tests to verify array and hash container support.
|
data/README.rdoc
CHANGED
@@ -8,15 +8,33 @@
|
|
8
8
|
|
9
9
|
Make hash-based object initialization easy!
|
10
10
|
|
11
|
-
Provides hash_make method that can consider parameter name, type, default value, validation,
|
12
|
-
|
13
|
-
Also, by default assigns to instance variable of same name as parameter.
|
11
|
+
Provides hash_make method that can consider parameter name, type, default value, validation, requiredd/not, and container type (none/array/hash) according to the specification provided. By default it the value given with the arg key to to an instance variable of same name as parameter.
|
14
12
|
|
15
13
|
== Features
|
16
14
|
|
17
15
|
== Examples
|
18
16
|
|
19
17
|
require 'hashmake'
|
18
|
+
|
19
|
+
class MyClass
|
20
|
+
include Hashmake::HashMakeable
|
21
|
+
|
22
|
+
ARG_SPECS = [
|
23
|
+
ArgSpec.new(:key => :x, :reqd => true, :type => Float, :validator => ->(a){ a.between?(0.0,1.0) }),
|
24
|
+
ArgSpec.new(:key => :y, :reqd => false, :type => Float, :validator => ->(a){ a.between?(0.0,1.0) }, :default => 0.0),
|
25
|
+
]
|
26
|
+
|
27
|
+
attr_reader :x, :y
|
28
|
+
|
29
|
+
def initialize hashed_args
|
30
|
+
hash_make(ARG_SPECS, hashed_args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
a = MyClass.new :x => 0.5 # a.x => 0.5, a.y => 0.0
|
35
|
+
a = MyClass.new :x => 0.5, :y => 0.2 # a.x => 0.5, a.y => 0.2
|
36
|
+
a = MyClass.new # raise ArgumentError because :x is reqd and not given
|
37
|
+
a = MyClass.new :y => 0.5 # raise ArgumentError because :x is reqd and not given
|
20
38
|
|
21
39
|
== Requirements
|
22
40
|
|
@@ -50,20 +50,22 @@ module HashMakeable
|
|
50
50
|
raise ArgumentError, "val #{val} is not an array" unless val.is_a?(Array)
|
51
51
|
val.each do |item|
|
52
52
|
raise ArgumentError, "array item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
|
53
|
+
raise ArgumentError, "array item #{item} is not valid" unless arg_spec.validator.call(item)
|
53
54
|
end
|
54
55
|
when ArgSpec::CONTAINER_HASH
|
55
56
|
raise ArgumentError, "val #{val} is not a hash" unless val.is_a?(Hash)
|
56
57
|
val.values.each do |item|
|
57
58
|
raise ArgumentError, "hash item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
|
59
|
+
raise ArgumentError, "hash item #{item} is not valid" unless arg_spec.validator.call(item)
|
58
60
|
end
|
59
61
|
when ArgSpec::CONTAINER_NONE
|
60
62
|
raise ArgumentError, "val #{val} is not a #{arg_spec.type}" unless val.is_a?(arg_spec.type)
|
63
|
+
raise ArgumentError, "val #{val} is not valid" unless arg_spec.validator.call(val)
|
61
64
|
else
|
62
65
|
raise ArgumentError, "arg_spec.container #{arg_spec.container} is not valid"
|
63
66
|
end
|
64
67
|
|
65
68
|
if assign_args
|
66
|
-
raise ArgumentError, "value #{val} is not valid" unless arg_spec.validator.call(val)
|
67
69
|
self.instance_variable_set("@#{key.to_s}".to_sym, val)
|
68
70
|
end
|
69
71
|
end
|
data/lib/hashmake/version.rb
CHANGED
data/spec/hash_makeable_spec.rb
CHANGED
@@ -7,9 +7,11 @@ describe Hashmake::HashMakeable do
|
|
7
7
|
HASHED_ARG_SPECS = [
|
8
8
|
ArgSpec.new(:reqd => true, :key => :reqd_string, :type => String, :validator => ->(a){ a.length < 10 }),
|
9
9
|
ArgSpec.new(:reqd => false, :key => :not_reqd_float, :type => Float, :default => 0.0, :validator => ->(a){ a.between?(0.0,1.0) }),
|
10
|
+
ArgSpec.new(:reqd => false, :key => :not_reqd_array_of_float, :type => Float, :container => ArgSpec::CONTAINER_ARRAY, :default => ->(){Array.new}, :validator => ->(a){ a.between?(0.0,1.0) }),
|
11
|
+
ArgSpec.new(:reqd => false, :key => :not_reqd_hash_of_float, :type => Float, :container => ArgSpec::CONTAINER_HASH, :default => ->(){Hash.new}, :validator => ->(a){ a.between?(0.0,1.0) }),
|
10
12
|
]
|
11
13
|
|
12
|
-
attr_reader :reqd_string, :not_reqd_float
|
14
|
+
attr_reader :reqd_string, :not_reqd_float, :not_reqd_array_of_float, :not_reqd_hash_of_float
|
13
15
|
|
14
16
|
def initialize hashed_args = {}
|
15
17
|
hash_make MyTestClass::HASHED_ARG_SPECS, hashed_args
|
@@ -53,5 +55,54 @@ describe Hashmake::HashMakeable do
|
|
53
55
|
a.not_reqd_float.should eq(0.321)
|
54
56
|
end
|
55
57
|
end
|
58
|
+
|
59
|
+
context 'array container arg' do
|
60
|
+
it 'should be empty by default' do
|
61
|
+
a = MyTestClass.new :reqd_string => ""
|
62
|
+
a.not_reqd_array_of_float.should be_empty
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should assign array if it contains valid values of correct type' do
|
66
|
+
a = MyTestClass.new :reqd_string => "", :not_reqd_array_of_float => [ 0.5, 0.75 ]
|
67
|
+
a.not_reqd_array_of_float.should eq([ 0.5, 0.75 ])
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should raise ArgumentError if it contains invalid values of correct type' do
|
71
|
+
lambda do
|
72
|
+
a = MyTestClass.new :reqd_string => "", :not_reqd_array_of_float => [ -0.5, 2.5 ]
|
73
|
+
end.should raise_error(ArgumentError)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should raise ArgumentError if it contains valid values of incorrect type' do
|
77
|
+
lambda do
|
78
|
+
a = MyTestClass.new :reqd_string => "", :not_reqd_array_of_float => [ "", 2.5 ]
|
79
|
+
end.should raise_error(ArgumentError)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'hash container arg' do
|
84
|
+
it 'should be empty by default' do
|
85
|
+
a = MyTestClass.new :reqd_string => ""
|
86
|
+
a.not_reqd_hash_of_float.should be_empty
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should assign array if it contains valid values of correct type' do
|
90
|
+
a = MyTestClass.new :reqd_string => "", :not_reqd_hash_of_float => { :a => 0.5, :b => 0.75 }
|
91
|
+
a.not_reqd_hash_of_float.should eq({ :a => 0.5, :b => 0.75 })
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should raise ArgumentError if it contains invalid values of correct type' do
|
95
|
+
lambda do
|
96
|
+
a = MyTestClass.new :reqd_string => "", :not_reqd_hash_of_float => { :a => -0.5, :b => 1.75 }
|
97
|
+
end.should raise_error(ArgumentError)
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should raise ArgumentError if it contains valid values of incorrect type' do
|
101
|
+
lambda do
|
102
|
+
a = MyTestClass.new :reqd_string => "", :not_reqd_hash_of_float => { :a => "", :b => 0.75 }
|
103
|
+
end.should raise_error(ArgumentError)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
56
107
|
end
|
57
108
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashmake
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -133,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
segments:
|
135
135
|
- 0
|
136
|
-
hash:
|
136
|
+
hash: 581566081
|
137
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
138
|
none: false
|
139
139
|
requirements:
|
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
version: '0'
|
143
143
|
segments:
|
144
144
|
- 0
|
145
|
-
hash:
|
145
|
+
hash: 581566081
|
146
146
|
requirements: []
|
147
147
|
rubyforge_project:
|
148
148
|
rubygems_version: 1.8.23
|