hashmake 0.1.8 → 0.1.9

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.
@@ -35,4 +35,8 @@ Add #find_arg_specs and #make_hash methods to HashMakeable.
35
35
 
36
36
  === 0.1.7 / 2013-06-21
37
37
 
38
- In HashMakeable.make_hash, only make objects into hashes if the object class matches the arg spec type (and, still, if it is hash makeable).
38
+ In HashMakeable.make_hash, only make objects into hashes if the object class matches the arg spec type (and, still, if it is hash makeable).
39
+
40
+ === 0.1.7 / 2013-06-21
41
+
42
+ Add :allow_nil option to ArgSpec.new. If an arg spec allows nil, it mean that if a hashed arg is nil, no ArgumentError will be raised and the validator won't be called.
@@ -20,9 +20,10 @@ class ArgSpec
20
20
  :validator => ->(a){true},
21
21
  :container => nil,
22
22
  :type => Object,
23
+ :allow_nil => false
23
24
  }
24
25
 
25
- attr_reader :type, :validator, :reqd, :default, :container
26
+ attr_reader :type, :validator, :reqd, :default, :container, :allow_nil
26
27
 
27
28
  # A new instance of ArgSpec.
28
29
  #
@@ -56,6 +57,8 @@ class ArgSpec
56
57
  @container = new_args[:container]
57
58
  raise ArgumentError, "CONTAINERS does not include container #{@container}" unless CONTAINERS.include?(@container)
58
59
 
60
+ @allow_nil = new_args[:allow_nil]
61
+
59
62
  unless @reqd
60
63
  msg = "if hashed arg is not required, a default value or value generator (proc) must be defined via :default key"
61
64
  raise ArgumentError, msg unless new_args.has_key?(:default)
@@ -86,18 +86,30 @@ module HashMakeable
86
86
  if arg_spec.container == Array
87
87
  raise ArgumentError, "val #{val} is not an array" unless val.is_a?(Array)
88
88
  val.each do |item|
89
- raise ArgumentError, "array item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
90
- raise ArgumentError, "array item #{item} is not valid" unless arg_spec.validator.call(item)
89
+ if item.nil?
90
+ raise ArgumentError, "nil was given" unless arg_spec.allow_nil
91
+ else
92
+ raise ArgumentError, "array item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
93
+ raise ArgumentError, "array item #{item} is not valid" unless arg_spec.validator.call(item)
94
+ end
91
95
  end
92
96
  elsif arg_spec.container == Hash
93
97
  raise ArgumentError, "val #{val} is not a hash" unless val.is_a?(Hash)
94
98
  val.values.each do |item|
95
- raise ArgumentError, "hash item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
96
- raise ArgumentError, "hash item #{item} is not valid" unless arg_spec.validator.call(item)
99
+ if item.nil?
100
+ raise ArgumentError, "nil was given" unless arg_spec.allow_nil
101
+ else
102
+ raise ArgumentError, "hash item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
103
+ raise ArgumentError, "hash item #{item} is not valid" unless arg_spec.validator.call(item)
104
+ end
97
105
  end
98
106
  elsif arg_spec.container.nil?
99
- raise ArgumentError, "val #{val} is not a #{arg_spec.type}" unless val.is_a?(arg_spec.type)
100
- raise ArgumentError, "val #{val} is not valid" unless arg_spec.validator.call(val)
107
+ if val.nil?
108
+ raise ArgumentError, "nil was given" unless arg_spec.allow_nil
109
+ else
110
+ raise ArgumentError, "val #{val} is not a #{arg_spec.type}" unless val.is_a?(arg_spec.type)
111
+ raise ArgumentError, "val #{val} is not valid" unless arg_spec.validator.call(val)
112
+ end
101
113
  else
102
114
  raise ArgumentError, "arg_spec.container #{arg_spec.container} is not valid"
103
115
  end
@@ -4,5 +4,5 @@
4
4
  #
5
5
  module Hashmake
6
6
  # hashmake version
7
- VERSION = "0.1.8"
7
+ VERSION = "0.1.9"
8
8
  end
@@ -43,6 +43,20 @@ describe Hashmake::HashMakeable do
43
43
  end
44
44
  end
45
45
 
46
+ class TestAllowNil
47
+ include HashMakeable
48
+
49
+ ARG_SPECS = {
50
+ :allows_nil => arg_spec(:reqd => false, :default => ->(){ "123" }, :type => String, :allow_nil => true),
51
+ :disallows_nil => arg_spec(:reqd => false, :default => ->(){ "123" }, :type => String, :allow_nil => false)
52
+ }
53
+
54
+ attr_reader :allows_nil, :disallows_nil
55
+ def initialize args
56
+ hash_make ARG_SPECS, args
57
+ end
58
+ end
59
+
46
60
  describe '#hash_make' do
47
61
  context 'for a reqd arg' do
48
62
  it 'should raise an ArgumentError if not given in the hash' do
@@ -136,6 +150,14 @@ describe Hashmake::HashMakeable do
136
150
  a.also_hash_makeable.a_number.should eq(a_number)
137
151
  end
138
152
  end
153
+
154
+ it 'should not raise ArgumentError if nil is given when nil is allowed' do
155
+ lambda { TestAllowNil.new(:allows_nil => nil) }.should_not raise_error
156
+ end
157
+
158
+ it 'should raise ArgumentError if nil is given when nil is not allowed' do
159
+ lambda { TestAllowNil.new(:disallows_nil => nil) }.should raise_error(ArgumentError)
160
+ end
139
161
  end
140
162
 
141
163
  describe '#make_hash' do
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.8
4
+ version: 0.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-21 00:00:00.000000000 Z
12
+ date: 2013-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -133,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
133
  version: '0'
134
134
  segments:
135
135
  - 0
136
- hash: -1512058762089527342
136
+ hash: -1181723344412730535
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: -1512058762089527342
145
+ hash: -1181723344412730535
146
146
  requirements: []
147
147
  rubyforge_project:
148
148
  rubygems_version: 1.8.23