chozo 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,218 @@
1
+ module Chozo::Mixin
2
+ # Borrowed and modified from: {https://raw.github.com/opscode/chef/11.4.0/lib/chef/mixin/params_validate.rb}
3
+ #
4
+ # Copyright:: Copyright (c) 2008 Opscode, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ module ParamsValidate
18
+ # Takes a hash of options, along with a map to validate them. Returns the original
19
+ # options hash, plus any changes that might have been made (through things like setting
20
+ # default values in the validation map)
21
+ #
22
+ # For example:
23
+ #
24
+ # validate({ :one => "neat" }, { :one => { :kind_of => String }})
25
+ #
26
+ # Would raise an exception if the value of :one above is not a kind_of? string. Valid
27
+ # map options are:
28
+ #
29
+ # :default:: Sets the default value for this parameter.
30
+ # :callbacks:: Takes a hash of Procs, which should return true if the argument is valid.
31
+ # The key will be inserted into the error message if the Proc does not return true:
32
+ # "Option #{key}'s value #{value} #{message}!"
33
+ # :kind_of:: Ensure that the value is a kind_of?(Whatever). If passed an array, it will ensure
34
+ # that the value is one of those types.
35
+ # :respond_to:: Ensure that the value has a given method. Takes one method name or an array of
36
+ # method names.
37
+ # :required:: Raise an exception if this parameter is missing. Valid values are true or false,
38
+ # by default, options are not required.
39
+ # :regex:: Match the value of the paramater against a regular expression.
40
+ # :equal_to:: Match the value of the paramater with ==. An array means it can be equal to any
41
+ # of the values.
42
+ def validate(opts, map)
43
+ #--
44
+ # validate works by taking the keys in the validation map, assuming it's a hash, and
45
+ # looking for _pv_:symbol as methods. Assuming it find them, it calls the right
46
+ # one.
47
+ #++
48
+ raise ArgumentError, "Options must be a hash" unless opts.kind_of?(Hash)
49
+ raise ArgumentError, "Validation Map must be a hash" unless map.kind_of?(Hash)
50
+
51
+ map.each do |key, validation|
52
+ unless key.kind_of?(Symbol) || key.kind_of?(String)
53
+ raise ArgumentError, "Validation map keys must be symbols or strings!"
54
+ end
55
+ case validation
56
+ when true
57
+ _pv_required(opts, key)
58
+ when false
59
+ true
60
+ when Hash
61
+ validation.each do |check, carg|
62
+ check_method = "_pv_#{check.to_s}"
63
+ if self.respond_to?(check_method, true)
64
+ self.send(check_method, opts, key, carg)
65
+ else
66
+ raise ArgumentError, "Validation map has unknown check: #{check}"
67
+ end
68
+ end
69
+ end
70
+ end
71
+ opts
72
+ end
73
+
74
+ def set_or_return(symbol, arg, validation)
75
+ iv_symbol = "@#{symbol.to_s}".to_sym
76
+ map = {
77
+ symbol => validation
78
+ }
79
+
80
+ if arg == nil && self.instance_variable_defined?(iv_symbol) == true
81
+ self.instance_variable_get(iv_symbol)
82
+ else
83
+ opts = validate({ symbol => arg }, { symbol => validation })
84
+ self.instance_variable_set(iv_symbol, opts[symbol])
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ # Return the value of a parameter, or nil if it doesn't exist.
91
+ def _pv_opts_lookup(opts, key)
92
+ if opts.has_key?(key.to_s)
93
+ opts[key.to_s]
94
+ elsif opts.has_key?(key.to_sym)
95
+ opts[key.to_sym]
96
+ else
97
+ nil
98
+ end
99
+ end
100
+
101
+ # Raise an exception if the parameter is not found.
102
+ def _pv_required(opts, key, is_required=true)
103
+ if is_required
104
+ if (opts.has_key?(key.to_s) && !opts[key.to_s].nil?) ||
105
+ (opts.has_key?(key.to_sym) && !opts[key.to_sym].nil?)
106
+ true
107
+ else
108
+ raise ValidationFailed, "Required argument #{key} is missing!"
109
+ end
110
+ end
111
+ end
112
+
113
+ def _pv_equal_to(opts, key, to_be)
114
+ value = _pv_opts_lookup(opts, key)
115
+ unless value.nil?
116
+ passes = false
117
+ Array(to_be).each do |tb|
118
+ passes = true if value == tb
119
+ end
120
+ unless passes
121
+ raise ValidationFailed, "Option #{key} must be equal to one of: #{to_be.join(", ")}! You passed #{value.inspect}."
122
+ end
123
+ end
124
+ end
125
+
126
+ # Raise an exception if the parameter is not a kind_of?(to_be)
127
+ def _pv_kind_of(opts, key, to_be)
128
+ value = _pv_opts_lookup(opts, key)
129
+ unless value.nil?
130
+ passes = false
131
+ Array(to_be).each do |tb|
132
+ passes = true if value.kind_of?(tb)
133
+ end
134
+ unless passes
135
+ raise ValidationFailed, "Option #{key} must be a kind of #{to_be}! You passed #{value.inspect}."
136
+ end
137
+ end
138
+ end
139
+
140
+ # Raise an exception if the parameter does not respond to a given set of methods.
141
+ def _pv_respond_to(opts, key, method_name_list)
142
+ value = _pv_opts_lookup(opts, key)
143
+ unless value.nil?
144
+ Array(method_name_list).each do |method_name|
145
+ unless value.respond_to?(method_name)
146
+ raise ValidationFailed, "Option #{key} must have a #{method_name} method!"
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ # Assert that parameter returns false when passed a predicate method.
153
+ # For example, :cannot_be => :blank will raise a ValidationFailed
154
+ # error value.blank? returns a 'truthy' (not nil or false) value.
155
+ #
156
+ # Note, this will *PASS* if the object doesn't respond to the method.
157
+ # So, to make sure a value is not nil and not blank, you need to do
158
+ # both :cannot_be => :blank *and* :cannot_be => :nil (or :required => true)
159
+ def _pv_cannot_be(opts, key, predicate_method_base_name)
160
+ value = _pv_opts_lookup(opts, key)
161
+ predicate_method = (predicate_method_base_name.to_s + "?").to_sym
162
+
163
+ if value.respond_to?(predicate_method)
164
+ if value.send(predicate_method)
165
+ raise ValidationFailed, "Option #{key} cannot be #{predicate_method_base_name}"
166
+ end
167
+ end
168
+ end
169
+
170
+ # Assign a default value to a parameter.
171
+ def _pv_default(opts, key, default_value)
172
+ value = _pv_opts_lookup(opts, key)
173
+ if value == nil
174
+ opts[key] = default_value
175
+ end
176
+ end
177
+
178
+ # Check a parameter against a regular expression.
179
+ def _pv_regex(opts, key, regex)
180
+ value = _pv_opts_lookup(opts, key)
181
+ if value != nil
182
+ passes = false
183
+ [ regex ].flatten.each do |r|
184
+ if value != nil
185
+ if r.match(value.to_s)
186
+ passes = true
187
+ end
188
+ end
189
+ end
190
+ unless passes
191
+ raise ValidationFailed, "Option #{key}'s value #{value} does not match regular expression #{regex.inspect}"
192
+ end
193
+ end
194
+ end
195
+
196
+ # Check a parameter against a hash of proc's.
197
+ def _pv_callbacks(opts, key, callbacks)
198
+ raise ArgumentError, "Callback list must be a hash!" unless callbacks.kind_of?(Hash)
199
+ value = _pv_opts_lookup(opts, key)
200
+ if value != nil
201
+ callbacks.each do |message, zeproc|
202
+ if zeproc.call(value) != true
203
+ raise ValidationFailed, "Option #{key}'s value #{value} #{message}!"
204
+ end
205
+ end
206
+ end
207
+ end
208
+
209
+ # Allow a parameter to default to @name
210
+ def _pv_name_attribute(opts, key, is_name_attribute=true)
211
+ if is_name_attribute
212
+ if opts[key] == nil
213
+ opts[key] = self.instance_variable_get("@name")
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end
data/lib/chozo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chozo
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chozo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
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-02-26 00:00:00.000000000 Z
12
+ date: 2013-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -94,6 +94,7 @@ files:
94
94
  - lib/chozo/hashie_ext/mash.rb
95
95
  - lib/chozo/mixin.rb
96
96
  - lib/chozo/mixin/from_file.rb
97
+ - lib/chozo/mixin/params_validate.rb
97
98
  - lib/chozo/platform.rb
98
99
  - lib/chozo/ruby_engine.rb
99
100
  - lib/chozo/varia_model.rb
@@ -131,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
132
  version: '0'
132
133
  segments:
133
134
  - 0
134
- hash: 2558961002303153299
135
+ hash: -1813290551789113009
135
136
  requirements: []
136
137
  rubyforge_project:
137
138
  rubygems_version: 1.8.24