methodic 0.2 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/AUTHORS ADDED
@@ -0,0 +1,8 @@
1
+ Author :
2
+ Romain GEORGES <romain@ultragreen.net>
3
+ - code
4
+ - inline documentation
5
+ - specs
6
+ - Documentation
7
+ Ruby Gem maintener :
8
+ Romain GEORGES <romain@ultragreen.net>
data/COPYRIGHT ADDED
@@ -0,0 +1,24 @@
1
+ methodic Copyright (c) 2012 Ultragreen Software, Romain GEORGES
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
+ SUCH DAMAGE.
24
+
data/ChangeLog ADDED
@@ -0,0 +1,17 @@
1
+ methodic :
2
+ - initial dev. version
3
+ - version 0.1 (unstable)
4
+ Romain GEORGES
5
+ - initial finished release
6
+ - version 0.2 (testing, released)
7
+ Romain GEORGES
8
+ - documented, specified and tested first production release
9
+ - version 1.0 (RC)
10
+ Romain GEORGES
11
+ - WARN : DEPRECATED : (NO COMPATIBILITY) method #specify_default_value_of become #specify_default_value
12
+ - WARN : DEPRECATED : (NO COMPATIBILITY) method #specify_defaults_values_of become #specify_defaults_values
13
+ - NEW : validate_known_options flag in Mehodic::get_options (second args)
14
+ - NEW : #specify_known_option and #specify_known_options for exhaustive options control (see manual)
15
+ - patched version
16
+ - version 1.1 (RC2)
17
+ Romain GEORGES
data/Gemfile CHANGED
@@ -4,4 +4,5 @@ gem "rspec", :require => "spec"
4
4
  gem "rake"
5
5
  gem "yard"
6
6
  gem "rdoc"
7
- gem "rcov"
7
+ gem "rcov"
8
+ gem "roodi"
data/Gemfile.lock CHANGED
@@ -7,6 +7,8 @@ GEM
7
7
  rcov (1.0.0)
8
8
  rdoc (3.12)
9
9
  json (~> 1.4)
10
+ roodi (2.2.0)
11
+ ruby_parser (~> 2.3.0)
10
12
  rspec (2.9.0)
11
13
  rspec-core (~> 2.9.0)
12
14
  rspec-expectations (~> 2.9.0)
@@ -15,6 +17,9 @@ GEM
15
17
  rspec-expectations (2.9.0)
16
18
  diff-lcs (~> 1.1.3)
17
19
  rspec-mocks (2.9.0)
20
+ ruby_parser (2.3.1)
21
+ sexp_processor (~> 3.0)
22
+ sexp_processor (3.2.0)
18
23
  yard (0.7.5)
19
24
  yard-rspec (0.1)
20
25
  yard
@@ -26,6 +31,7 @@ DEPENDENCIES
26
31
  rake
27
32
  rcov
28
33
  rdoc
34
+ roodi
29
35
  rspec
30
36
  yard
31
37
  yard-rspec
data/INSTALL ADDED
@@ -0,0 +1,7 @@
1
+ How to install methodic ?
2
+ =========================
3
+
4
+ Please install the gem version via RubyGems :
5
+
6
+ $ su -
7
+ # gem ins methodic
data/Rakefile CHANGED
@@ -9,6 +9,15 @@ require 'code_statistics'
9
9
  require 'rspec/core/rake_task'
10
10
  require 'yard'
11
11
  require 'yard/rake/yardoc_task.rb'
12
+ require "rake/tasklib"
13
+ require "roodi"
14
+ require "roodi_task"
15
+
16
+
17
+ RoodiTask.new() do | t |
18
+ t.patterns = %w(lib/**/*.rb spec/**/*.rb features/**/*.rb)
19
+ t.config = "ultragreen_roodi_coding_convention.yml"
20
+ end
12
21
 
13
22
 
14
23
  CLEAN.include('*.tmp','*.old')
data/doc/manual.rdoc CHANGED
@@ -1 +1,71 @@
1
1
  =Methodic
2
+
3
+ == Content
4
+
5
+ Author:: Romain GEORGES <romain@ultragreen.net>
6
+ Version:: 1.2
7
+ WWW:: http://www.ultragreen.net/projects/methodic
8
+
9
+ == Description
10
+
11
+ Methodic is a macro-like utility to help test, validate, control options passed by an Hash param to a method, it could help you to merge with defaults values,
12
+ It raise explained exceptions and return false if the validations steps failed.
13
+
14
+ == Installation
15
+
16
+ In a valid Ruby environment :
17
+
18
+ $ sudo zsh
19
+ # gem ins methodic
20
+
21
+ == Implementation
22
+
23
+ * [Methodic]
24
+ * [Methodic::Options]
25
+
26
+ == Example
27
+
28
+ === Without known options control
29
+
30
+ require 'rubygems'
31
+ require 'methodic'
32
+ [...]
33
+ # in a method
34
+ def amethod ( _options = {})
35
+ myOptions = Methodic::get_options(_options) do |m|
36
+ m.specify_default_value_of :country => 'France'
37
+ m.specify_classes_of :name => String, :surname => String, :age => Fixnum, :country => String
38
+ m.specify_presence_of :name
39
+ m.specify_presence_of :surname
40
+ m.specify_formats_of :name => /\w+/, :surname => /\w+/, :country => /\w+/
41
+ m.merge!
42
+ end
43
+ # processing method
44
+ end
45
+ [...]
46
+
47
+ === With known options control
48
+
49
+ require 'rubygems'
50
+ require 'methodic'
51
+ [...]
52
+ # in a method
53
+ def amethod ( _options = {})
54
+ myOptions = Methodic::get_options(_options,true) do |m|
55
+ # all others definitions MUST be included in known options list (explained in Spec), so : m.specify_known_options [:country,:name,:surname, :age]
56
+ m.specify_default_value_of :country => 'France'
57
+ m.specify_classes_of :name => String, :surname => String, :age => Fixnum, :country => String
58
+ m.specify_presence_of :name
59
+ m.specify_presence_of :surname
60
+ m.specify_formats_of :name => /\w+/, :surname => /\w+/, :country => /\w+/
61
+ m.merge!
62
+ end
63
+ # processing method
64
+ end
65
+ [...]
66
+
67
+
68
+ == Copyright
69
+
70
+ <pre>Methodic (c) 2012 Romain GEORGES <romain@ultragreen.net> for Ultragreen Software </pre>
71
+
data/lib/methodic.rb CHANGED
@@ -1,64 +1,260 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright Ultragreen (c) 2012
3
+ #---
4
+ # Author : Romain GEORGES
5
+ # type : gem component library
6
+ # obj : Methodic Module
7
+ #---
1
8
 
9
+ # module Methodic
10
+ # @author Romain GEORGES <romain@ultragreen.net>
11
+ # @see http://www.ultragreen.net/projects/methodic
12
+ # @version 0.2
13
+ # @note this module include a class, but please use the module method Methodic::get_options to Build a Methodic::Options instance
14
+ # @example Complete usage
15
+ # require 'rubygems'
16
+ # require 'methodic'
17
+ # [...]
18
+ # # in a method
19
+ # def amethod ( _options = {})
20
+ # myOptions = Methodic::get_options(_options,true) do |m|
21
+ # m.specify_known_options [:country,:name,:surname,:age]
22
+ # m.specify_default_value :country => 'France'
23
+ # m.specify_classes_of :name => String, :surname => String, :age => Fixnum, :country => String
24
+ # m.specify_presence_of :name
25
+ # m.specify_presence_of :surname
26
+ # m.specify_formats_of :name => /\w+/, :surname => /\w+/, :country => /\w+/
27
+ # m.merge
28
+ # end
29
+ # # processing method
30
+ # end
31
+ # [...]
2
32
  module Methodic
33
+
34
+ # class Options
35
+ # an Options utility class
36
+ # @note please do not instantiate with Options::new use Methodic::get_options
37
+ # @example Complete usage
38
+ # require 'rubygems'
39
+ # require 'methodic'
40
+ # [...]
41
+ # # in a method
42
+ # def amethod ( _options = {})
43
+ # myOptions = Methodic::get_options(_options,true) do |m|
44
+ # m.specify_known_options [:country,:name,:surname,:age]
45
+ # m.specify_default_value :country => 'France'
46
+ # m.specify_classes_of :name => String, :surname => String, :age => Fixnum, :country => String
47
+ # m.specify_presence_of :name
48
+ # m.specify_presence_of :surname
49
+ # m.specify_formats_of :name => /\w+/, :surname => /\w+/, :country => /\w+/
50
+ # m.merge
51
+ # end
52
+ # # processing method
53
+ # end
54
+ # [...]
3
55
  class Options < Hash
56
+
57
+ # @example writing
58
+ # myOptions = Methodic::get_options(options)
59
+ # myOptions.known = [:name]
60
+ # myOptions.known.push :surname
61
+ # @example reading
62
+ # p myOptions.known
63
+ # => [ :name, :surname ]
64
+ # @attr [Hash] the list of all options assumed for a method
65
+ attr_accessor :known
4
66
 
5
- attr_accessor :classes
67
+ # @example writing
68
+ # myOptions = Methodic::get_options(options)
69
+ # myOptions.classes = {:name => String }
70
+ # myOptions.classes[:surname] = String
71
+ # @example reading
72
+ # p myOptions.classes
73
+ # => { :name => String, :surname => String }
74
+ # @attr [Hash] classes a hash table of some options associated with their corresponding types or classes
75
+ attr_accessor :classes
76
+
77
+ # @attr [Array] mandatories the full list of mandatories options
78
+ # @example writing
79
+ # myOptions = Methodic::get_options(options)
80
+ # myOptions.mandatories = [:name]
81
+ # myOptions.mandatories.push :surname
82
+ # @example reading
83
+ # p myOptions.mandatories
84
+ # => [ :name, :surname ]
6
85
  attr_accessor :mandatories
86
+
87
+ # @attr [Hash] defaults a hash table of some options with defaults values
88
+ # @example writing
89
+ # myOptions = Methodic::get_options(options)
90
+ # myOptions.defaults = {:name => 'John' }
91
+ # myOptions.defaults[:surname] = 'Doe'
92
+ # @example reading
93
+ # p myOptions.defaults
94
+ # => { :name => 'John', :surname => 'Doe' }
7
95
  attr_accessor :defaults
96
+
97
+ # @attr [Hash] formats a hash table of some options with their corresponding
98
+ # @example writing
99
+ # myOptions = Methodic::get_options(options)
100
+ # myOptions.formats = {:name => /\w+/ }
101
+ # myOptions.defaults[:surname] = /\w+/
102
+ # @example reading
103
+ # p myOptions.defaults
104
+ # => { :name => /\w+/, :surname => /\w+/ }
8
105
  attr_accessor :formats
9
106
 
10
- def initialize(_options = {})
107
+ # initializer for [Options]
108
+ # @note please do not use standalone, build from module method Methodic::get_options
109
+ # @param [Hash] _options the options hash (define for the method you would prototype)
110
+ # @note _options keys must be symbols
111
+ # @example prototype for informational only
112
+ # Methodic::Options::new({:toto => 'titi', :tutu => 'tata'})
113
+ # @return [Options] self
114
+ def initialize(_options = {},_validate_known_options = false)
115
+
116
+ raise ArgumentError::new('Argument _options must be a Hash') unless _options.class == Hash or _options.class == Methodic::Options # ;) reintrance and cascading
117
+ raise ArgumentError::new('keys must be Symbol') unless _options.keys.select{|i| i.class == Symbol }.size == _options.keys.size
11
118
  self.replace _options
12
119
  @defaults = Hash::new
13
120
  @formats = Hash::new
14
121
  @classes = Hash::new
122
+ @known = Array::new
15
123
  @mandatories = Array::new
124
+ @validate_known_options = _validate_known_options
125
+ yield self if block_given?
16
126
  end
17
127
 
128
+
129
+ # read only accessor on the [Hash] slef keys
130
+ # @return [Array] self.keys the keys of The Options object itself
131
+ # @example usage
132
+ # options = {:name => 'Doe', :surname => 'John'}
133
+ # p myOptions = Methodic::get_options(options)
134
+ # => { :name => String, :surname => String }
135
+ # p myOptions.options
136
+ # => [:name, :surname]
18
137
  def options
19
- return @classes.keys
138
+ return self.keys
20
139
  end
21
140
 
22
- def specify_default_value_of(values)
141
+
142
+ # pretty accessor for specifying the default(s) value(s) for options
143
+ # @param [Hash] values a value definition, keys are symbols
144
+ # @return [hash] @defaults merged with values
145
+ # @example usage
146
+ # myOptions = Methodic::get_options(_options)
147
+ # myOptions.specify_default_value :name => 'Doe'
148
+ # myOptions.specify_defaults_values :name => 'Doe', :surname => 'John'
149
+ def specify_default_value(values)
23
150
  @defaults.merge! values
24
- end
25
- alias :specify_defaults_values_of :specify_default_value_of
151
+ return @defaults
152
+ end
153
+ alias :specify_defaults_values :specify_default_value
26
154
 
155
+ # pretty accessor for specifying classes of options
156
+ # @param [Hash] values a value definition, keys are symbols
157
+ # @return [hash] @classes merged with values
158
+ # @note classes must be precised in Ruby not a string like Fixnum, Hash, Array, String
159
+ # @example usage
160
+ # myOptions = Methodic::get_options(_options)
161
+ # myOptions.specify_class_of :name => String
162
+ # myOptions.specify_classes_of :name => String, :surname => String
27
163
  def specify_class_of(values)
164
+
28
165
  @classes.merge! values
166
+
167
+ return @classes
168
+
29
169
  end
30
170
  alias :specify_classes_of :specify_class_of
31
171
 
172
+ # pretty accessor for specifying mandatories options
173
+ # @param [Array] values a Array of symbols or a unique symbol
174
+ # @return [Array] @mandatories merged with values
175
+ # @example usage
176
+ # myOptions = Methodic::get_options(_options)
177
+ # myOptions.specify_presence_of :name
178
+ # myOptions.specify_presences_of [ :name, :surname ]
32
179
  def specify_presence_of(values)
33
180
  @mandatories << values
34
181
  @mandatories.flatten!
182
+ @mandatories.uniq!
183
+ return @mandatories
35
184
  end
36
185
  alias :specify_presences_of :specify_presence_of
186
+
187
+
188
+ # pretty accessor for specifying known options
189
+ # @param [Array] values a Array of symbols or a unique symbol
190
+ # @return [Array] @known merged with values
191
+ # @example usage
192
+ # myOptions = Methodic::get_options(_options)
193
+ # myOptions.specify_known_option :name
194
+ # myOptions.specify_known_options [ :name, :surname ]
195
+ def specify_known_option(values)
196
+ @known << values
197
+ @known.flatten!
198
+ @known.uniq!
199
+ return @known
200
+ end
201
+ alias :specify_known_options :specify_known_option
37
202
 
203
+ # pretty accessor for specifying the format of options
204
+ # @param [Hash] values a value definition, keys are symbols
205
+ # @return [hash] @formats merged with values
206
+ # @note formats must be Regular Expression
207
+ # @example usage
208
+ # myOptions = Methodic::get_options(_options)
209
+ # myOptions.specify_format_of :name => /\w+/
210
+ # myOptions.specify_formats_of :name => /\w+/, :surname => /\w+/
38
211
  def specify_format_of(values)
39
212
  @formats.merge! values
213
+ return @formats
40
214
  end
41
215
  alias :specify_formats_of :specify_format_of
42
216
 
217
+ # pretty accessor for specifying condition for options
218
+ # @todo implementation of conditions
43
219
  def specify_condition_for
44
220
  raise NotYetImplemented
45
221
  end
46
222
  alias :specify_conditions_for :specify_condition_for
47
223
 
224
+ # default values merge method
225
+ # merge @defaults with self
226
+ # @return [self|Options]
227
+ # @example usage
228
+ # myOptions = Methodic::get_options(:name = 'Walker')
229
+ # myOptions.specify_default_value_of :surname => 'John'
230
+ # p myOptions
231
+ # # =>{:surname=>"John", :name=>"Doe"}
232
+ # myOptions.merge
233
+ # p myOptions
234
+ # # =>{:surname=>"John", :name=>"Walker"}
48
235
  def merge_with_defaults
49
- self.replace(@defaults.merge self)
236
+ self.replace( @defaults.merge self)
237
+ return self
50
238
  end
51
-
239
+ alias :merge :merge_with_defaults
240
+ # Validation method for options, start the validation for classes, options, formats, presences
241
+ # @return [true|false] if validations failed or succeed
242
+ # @raise ArgumentError for each kind of validations, that could failed
243
+ # @note order for validation and Exception raising : Options inclusion >> Classes matching >> Mandatories Options presences >> Formats matching
52
244
  def validate
53
245
  table = []
54
- table.push validate_options_and_classes unless @classes.empty?
55
- table.push validate_formats unless @formats.empty?
246
+ raise ArgumentError::new("Option : known list of options empty.") and return false if @known.empty? and @validate_known_options
247
+ table.push validate_known_options if @validate_known_options
248
+ table.push validate_classes unless @classes.empty?
56
249
  table.push validate_presences unless @mandatories.empty?
250
+ table.push validate_formats unless @formats.empty?
251
+
57
252
  return true unless table.include?(false)
58
253
  end
59
254
  alias :validate! :validate
60
255
 
61
256
  private
257
+ # private method for the formats validation step
62
258
  def validate_formats
63
259
  self.each do |option,value|
64
260
  if @formats.key? option then
@@ -69,6 +265,24 @@ module Methodic
69
265
  return true
70
266
  end
71
267
 
268
+ # private method for known options validation step
269
+ def validate_known_options
270
+ @mandatories.each do |mdt|
271
+ raise ArgumentError::new("Mandatories options :#{mdt} is not a known options") and return false unless @known.include?(mdt)
272
+ end
273
+ @classes.each do |aclass,value|
274
+ raise ArgumentError::new("Classes definition :#{aclass} => #{value} is not a known options") and return false unless @known.include?(aclass)
275
+ end
276
+ @formats.each do |format,value|
277
+ raise ArgumentError::new("Formats definition :#{format} => #{value} is not a known options") and return false unless @known.include?(format)
278
+ end
279
+ @defaults.each do |default,value|
280
+ raise ArgumentError::new("Defaults value definition :#{default} => #{value} is not a known options") and return false unless @known.include?(default)
281
+ end
282
+ return true
283
+ end
284
+
285
+ # private method for mandatories presence validation step
72
286
  def validate_presences
73
287
  @mandatories.each do |mdt|
74
288
  raise ArgumentError::new("Missing option : #{mdt}") and return false unless self.include?(mdt)
@@ -76,15 +290,29 @@ module Methodic
76
290
  return true
77
291
  end
78
292
 
79
- def validate_options_and_classes
80
- self.each do |option,value|
81
- raise ArgumentError::new("Unknown option : #{option}") and return fasle unless @classes.include?(option)
82
- raise ArgumentError::new("Option : #{option} type mismatch must be a #{@classes[option]}") and return false unless value.class == @classes[option]
293
+ # private method for classes validation step
294
+ def validate_classes
295
+ @classes.each do |option,value|
296
+ raise ArgumentError::new("Option : #{option} type mismatch must be a #{value}") and return false unless self[option].class == value
83
297
  end
84
298
  return true
85
299
  end
86
300
  end
87
- def Methodic::get_options(_options = {})
88
- return Methodic::Options::new(_options)
301
+
302
+ # Module method factory to build [Options] instance
303
+ # @return [Options] instance
304
+ # @param [Hash] _options the options [Hash] from the method, you want to prototype
305
+ # @example usage
306
+ # myOptions = Methodic::get_options({:foo => 'bar'})
307
+ # p myOptions.class
308
+ # => Options
309
+ # @note _options key must be symbols
310
+ def Methodic::get_options(_options = {},_validate_known_options=false)
311
+ return Methodic::Options::new(_options,_validate_known_options)
89
312
  end
313
+
314
+ # Exception Class for not yet implemented methods Exceptions
315
+ class NotYetImplemented < Exception; end
316
+
317
+
90
318
  end
data/methodic.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{methodic}
3
3
  s.author = "Romain GEORGES"
4
- s.version = "0.2"
5
- s.date = %q{2012-03-21}
4
+ s.version = "1.1"
5
+ s.date = %q{2013-02-14}
6
6
  s.summary = %q{Methodic : Hash table options specification and validation componant}
7
7
  s.email = %q{romain@ultragreen.net}
8
8
  s.homepage = %q{http://www.ultragreen.net}
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.bindir = nil
13
13
  s.required_ruby_version = '>= 1.8.1'
14
14
  s.add_development_dependency "rspec", ">= 2.0.0"
15
- s.rdoc_options << '--title' << 'Methodic : Ge�m documentation' << '--main' << 'doc/manual.rdoc' << '--line-numbers' << '--diagram'
15
+ s.rdoc_options << '--title' << 'Methodic : Gem documentation' << '--main' << 'doc/manual.rdoc' << '--line-numbers'
16
+ # << '--diagram'
16
17
  s.rubyforge_project = "nowarning"
17
18
  end
@@ -0,0 +1,363 @@
1
+ require'rubygems'
2
+ require'rspec'
3
+ require 'lib/methodic'
4
+
5
+ describe Methodic do
6
+
7
+ before :all do
8
+ $test_methodic_options = Methodic::get_options :name => 'Doe', :surname => 'John'
9
+ $test_methodic_options_with_known_options = Methodic::get_options({:name => 'Doe', :surname => 'John'},true)
10
+ end
11
+ before :each do
12
+ $test_methodic_options.mandatories = []
13
+ $test_methodic_options.formats = {}
14
+ $test_methodic_options.defaults = {}
15
+ $test_methodic_options.classes = {}
16
+ end
17
+ subject { Methodic }
18
+ specify { subject.should be_an_instance_of Module}
19
+ context Methodic::Options do
20
+ subject { Methodic::Options }
21
+ specify { subject.should be_an_instance_of Class }
22
+ it "should respond to all methods of a Hash" do
23
+ Hash::new.methods.each do |method|
24
+ $test_methodic_options.should respond_to(method)
25
+ end
26
+ end
27
+ context "#new(_options) (initialization)" do
28
+ context "Exception case" do
29
+ it "should raise ArgumentError if _options is not a Hash" do
30
+ [ 'a string', 12 ,Array::new,true,false].each do |item|
31
+ lambda{ Methodic::Options::new(item)}.should raise_error ArgumentError
32
+ end
33
+ end
34
+ it "should not raise ArgumentError if no arg is passed to initializer" do
35
+ lambda{ Methodic::Options::new}.should_not raise_error
36
+ end
37
+ it "should not raise ArgumentError if an Hash arg is passed to initializer" do
38
+ lambda{ Methodic::Options::new :name => 'Doe' }.should_not raise_error
39
+ end
40
+ it "should raise ArgumentError if an Hash arg is passed to initializer but with keys different of Symbol" do
41
+ lambda{ Methodic::Options::new 'titi' => 'tutu'}.should raise_error ArgumentError
42
+ end
43
+ it "should not raise ArgumentError if two arg is passed to initializer, second arg has a boolean value, must be object" do
44
+ lambda{ Methodic::Options::new({:name => 'Doe'},true) }.should_not raise_error ArgumentError
45
+ end
46
+
47
+ it "should raise ArgumentError if more than two arg is passed to initializer" do
48
+ lambda{ Methodic::Options::new({:name => 'Doe'},{:surname => 'John'},true) }.should raise_error ArgumentError
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ context "Instance Attributs" do
55
+ context "#classes R/W" do
56
+ it { $test_methodic_options.should respond_to("classes") }
57
+ it { $test_methodic_options.should respond_to("classes=") }
58
+
59
+ it "should be true that #classes must return a Hash" do
60
+ $test_methodic_options.classes.class.should eq(Hash)
61
+ end
62
+ it "#classes[] affectation must be possible and #classes must respond this affectation" do
63
+ $test_methodic_options.classes[:test] = String
64
+ $test_methodic_options.classes.should eq({:test => String})
65
+ end
66
+
67
+ end
68
+ context "#defaults R/W" do
69
+ it { $test_methodic_options.should respond_to("defaults") }
70
+ it { $test_methodic_options.should respond_to("defaults=") }
71
+
72
+ it "should be true that #defaults must return a Hash" do
73
+ $test_methodic_options.defaults.class.should eq(Hash)
74
+ end
75
+ it "#defaults[] affectation must be possible and #defaults must respond this affectation" do
76
+ $test_methodic_options.defaults[:test] = "value"
77
+ $test_methodic_options.defaults.should eq({ :test => "value"})
78
+ end
79
+ end
80
+
81
+ context "#formats R/W" do
82
+ it { $test_methodic_options.should respond_to("formats") }
83
+ it { $test_methodic_options.should respond_to("formats=") }
84
+
85
+ it "should be true that #formats must return a Hash" do
86
+ $test_methodic_options.formats.class.should eq(Hash)
87
+ end
88
+ it "#formats[] affectation must be possible and #formats must respond this affectation" do
89
+ $test_methodic_options.formats[:test] = '.*'
90
+ $test_methodic_options.formats.should eq({ :test => '.*' })
91
+ end
92
+ end
93
+
94
+ context "#mandatories R/W" do
95
+ it { $test_methodic_options.should respond_to("mandatories") }
96
+ it { $test_methodic_options.should respond_to("mandatories=") }
97
+
98
+ it "should be true that #mandatories must return a Array" do
99
+ $test_methodic_options.mandatories.class.should eq(Array)
100
+ end
101
+ it "#mandatories.push affectation must be possible and #mandatories must respond this affectation" do
102
+ $test_methodic_options.mandatories.push :test
103
+ $test_methodic_options.mandatories.should eq([:test])
104
+ end
105
+ end
106
+ context "#known R/W" do
107
+ it { $test_methodic_options.should respond_to("known") }
108
+ it { $test_methodic_options.should respond_to("known=") }
109
+
110
+ it "should be true that #known must return a Array" do
111
+ $test_methodic_options.known.class.should eq(Array)
112
+ end
113
+ it "#known.push affectation must be possible and #known must respond this affectation" do
114
+ $test_methodic_options.known.push :test
115
+ $test_methodic_options.known.should include :test
116
+ end
117
+ end
118
+ end
119
+ context "Instance methods" do
120
+
121
+ context "#options" do
122
+ it { $test_methodic_options.should respond_to("options") }
123
+ it "should be true that #options must return a Array" do
124
+ $test_methodic_options.options.class.should eq(Array)
125
+ end
126
+ it "should respond an Array of options keys" do
127
+ $test_methodic_options.options.each do |item|
128
+ [:name,:surname].should include item
129
+ end
130
+ end
131
+ end
132
+
133
+ context "#specify_default_value" do
134
+ it { $test_methodic_options.should respond_to("specify_default_value") }
135
+ it { $test_methodic_options.should respond_to("specify_defaults_values") }
136
+ it "should merge default value hash record in defaults attribut" do
137
+
138
+ $test_methodic_options.specify_default_value :test => 'value'
139
+ $test_methodic_options.defaults[:test].should eq 'value'
140
+ $test_methodic_options.defaults.count.should eq 1
141
+ end
142
+ it "should redefine a new default value for a previous key" do
143
+ $test_methodic_options.specify_default_value :test => 'value'
144
+ $test_methodic_options.defaults[:test].should eq 'value'
145
+ $test_methodic_options.specify_default_value :test => 'newvalue'
146
+ $test_methodic_options.defaults[:test].should eq 'newvalue'
147
+ end
148
+ end
149
+
150
+ context "#specify_class_of" do
151
+ it { $test_methodic_options.should respond_to("specify_class_of") }
152
+ it { $test_methodic_options.should respond_to("specify_classes_of") }
153
+ it "should merge class hash record in classes attribut" do
154
+
155
+ $test_methodic_options.specify_class_of :test => String
156
+ $test_methodic_options.classes[:test].should eq String
157
+ $test_methodic_options.classes.count.should eq 1
158
+ end
159
+ it "should redefine a new class value for a previous key" do
160
+ $test_methodic_options.specify_class_of :test => String
161
+ $test_methodic_options.classes[:test].should eq String
162
+ $test_methodic_options.specify_class_of :test => Integer
163
+ $test_methodic_options.classes[:test].should eq Integer
164
+
165
+ end
166
+ end
167
+
168
+
169
+
170
+
171
+
172
+ context "#specify_condition_for" do
173
+ it { $test_methodic_options.should respond_to("specify_condition_for") }
174
+ it { $test_methodic_options.should respond_to("specify_conditions_for") }
175
+ it { lambda{ $test_methodic_options.specify_condition_for}.should raise_error Methodic::NotYetImplemented }
176
+ end
177
+
178
+ context "#specify_format_of" do
179
+ it { $test_methodic_options.should respond_to("specify_format_of") }
180
+ it { $test_methodic_options.should respond_to("specify_formats_of") }
181
+ it "should merge format hash record in formats attribut" do
182
+ $test_methodic_options.formats = {}
183
+ $test_methodic_options.specify_format_of :test => '.*'
184
+ $test_methodic_options.formats[:test].should eq '.*'
185
+ $test_methodic_options.formats.count.should eq 1
186
+ end
187
+ it "should redefine a new format value for a previous format key" do
188
+ $test_methodic_options.specify_format_of :test => '.*'
189
+ $test_methodic_options.formats[:test].should eq '.*'
190
+ $test_methodic_options.specify_format_of :test => '\d*'
191
+ $test_methodic_options.formats[:test].should eq '\d*'
192
+ end
193
+ end
194
+
195
+ context "#specify_presence_of" do
196
+ it { $test_methodic_options.should respond_to("specify_presence_of") }
197
+ it { $test_methodic_options.should respond_to("specify_presences_of") }
198
+ it "should merge presence Array record in mandatories attribut" do
199
+ $test_methodic_options.specify_presence_of :test
200
+ $test_methodic_options.mandatories.should include(:test)
201
+ $test_methodic_options.mandatories.count.should eq 1
202
+ $test_methodic_options.specify_presence_of :test
203
+ $test_methodic_options.mandatories.should include(:test)
204
+ $test_methodic_options.mandatories.count.should eq 1
205
+ end
206
+ end
207
+
208
+ context "#specify_known_option" do
209
+ it { $test_methodic_options.should respond_to("specify_known_option") }
210
+ it { $test_methodic_options.should respond_to("specify_known_options") }
211
+ it "should merge known Array record in known attribut" do
212
+ $test_methodic_options.specify_known_option :test
213
+ $test_methodic_options.known.should include(:test)
214
+ $test_methodic_options.known.count.should eq 1
215
+ $test_methodic_options.specify_known_option :test
216
+ $test_methodic_options.known.should include(:test)
217
+ $test_methodic_options.known.count.should eq 1
218
+ end
219
+ end
220
+
221
+ context "#validate" do
222
+
223
+ it { $test_methodic_options.should respond_to("validate") }
224
+ it { $test_methodic_options.should respond_to("validate!") }
225
+
226
+
227
+ context "1/ validate known options" do
228
+ context "@validate_known_options = false (default)" do
229
+ it "should not raise ArgumentError if mandatories is not fully include in the known options list" do
230
+ $test_methodic_options.known = []
231
+ $test_methodic_options.specify_presences_of [ :name, :surname]
232
+ $test_methodic_options.specify_known_options [ :surname]
233
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
234
+ end
235
+ it "should not raise ArgumentError if an option in options list not in the known options list" do
236
+ $test_methodic_options.known = []
237
+ $test_methodic_options.specify_known_options [ :surname ]
238
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
239
+ end
240
+ it "should not raise if all given options in options list is in known options " do
241
+ $test_methodic_options.known = []
242
+ $test_methodic_options.specify_known_options [ :name, :surname, :optional ]
243
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
244
+ end
245
+ it "should not raise ArgumentError if formats made a reference to an unknown options" do
246
+ $test_methodic_options.known = []
247
+ $test_methodic_options.specify_format_of :nickname => /.*/
248
+ $test_methodic_options.specify_known_options [ :surname, :name ]
249
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
250
+ end
251
+ it "should not raise ArgumentError if classes made a reference to an unknown options" do
252
+ $test_methodic_options.known = []
253
+ $test_methodic_options[:nickname] = 'Jeedoe'
254
+ $test_methodic_options.specify_class_of :nickname => String
255
+ $test_methodic_options.specify_known_options [ :surname, :name ]
256
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
257
+ end
258
+ it "should not raise ArgumentError if default made a reference to an unknown options" do
259
+ $test_methodic_options.known = []
260
+ $test_methodic_options.specify_default_value :nickname => 'Jeedoe'
261
+ $test_methodic_options.specify_known_options [ :surname, :name ]
262
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
263
+ end
264
+
265
+ end
266
+ context "@validate_known_options = true" do
267
+ it "should raise ArgumentError if mandatories is not fully include in the known options list" do
268
+ $test_methodic_options_with_known_options.known = []
269
+ $test_methodic_options_with_known_options.specify_presences_of [ :name, :surname ]
270
+ $test_methodic_options_with_known_options.specify_known_options [ :surname ]
271
+ lambda{$test_methodic_options_with_known_options.validate!}.should raise_error ArgumentError
272
+ end
273
+ it "should raise ArgumentError if an option in options list not in the known options list" do
274
+ $test_methodic_options_with_known_options.known = []
275
+ $test_methodic_options_with_known_options.specify_known_options [ :surname]
276
+ lambda{$test_methodic_options_with_known_options.validate!}.should raise_error ArgumentError
277
+ end
278
+ it "should not raise if all given options in options list is in known options " do
279
+ $test_methodic_options_with_known_options.known = []
280
+ $test_methodic_options_with_known_options.specify_known_options [ :name, :surname,:optional]
281
+ lambda{$test_methodic_options_with_known_options.validate!}.should_not raise_error ArgumentError
282
+ end
283
+ it "should raise ArgumentError if formats made a reference to an unknown options" do
284
+ $test_methodic_options_with_known_options.known = []
285
+ $test_methodic_options_with_known_options.specify_format_of :nickname => /.*/
286
+ $test_methodic_options_with_known_options.specify_known_options [ :surname, :name ]
287
+ lambda{$test_methodic_options_with_known_options.validate!}.should raise_error ArgumentError
288
+ end
289
+ it "should raise ArgumentError if classes made a reference to an unknown options" do
290
+ $test_methodic_options_with_known_options.known = []
291
+ $test_methodic_options_with_known_options[:nickname] = 'Jeedoe'
292
+ $test_methodic_options_with_known_options.specify_class_of :nickname => String
293
+ $test_methodic_options_with_known_options.specify_known_options [ :surname, :name ]
294
+ lambda{$test_methodic_options_with_known_options.validate!}.should raise_error ArgumentError
295
+ end
296
+ it "should raise ArgumentError if default made a reference to an unknown options" do
297
+ $test_methodic_options_with_known_options.known = []
298
+ $test_methodic_options_with_known_options.specify_default_value :nickname => 'Jeedoe'
299
+ $test_methodic_options_with_known_options.specify_known_options [ :surname, :name ]
300
+ lambda{$test_methodic_options_with_known_options.validate!}.should raise_error ArgumentError
301
+ end
302
+
303
+ end
304
+
305
+ end
306
+
307
+ context "2/ validate classes" do
308
+ it "should raise ArgumentError if an options don't match a class definition" do
309
+ $test_methodic_options.specify_classes_of :name => Integer, :surname => String
310
+ lambda{$test_methodic_options.validate!}.should raise_error ArgumentError
311
+
312
+ end
313
+ it "should not raise if options match class definition" do
314
+ $test_methodic_options.specify_classes_of :name => String, :surname => String
315
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
316
+ end
317
+ end
318
+ context "3/ validate mandatories" do
319
+ it "should raise ArgumentError if a mandatory option not in options list" do
320
+ $test_methodic_options.specify_presences_of [ :name , :surname, :nickname, :age ]
321
+ lambda{$test_methodic_options.validate!}.should raise_error ArgumentError
322
+
323
+ end
324
+ it "should not raise if mandatory options and options list match" do
325
+ $test_methodic_options.specify_presences_of [ :name , :surname ]
326
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
327
+ end
328
+ end
329
+ context "4/ validate formats" do
330
+ it "should raise ArgumentError if an option in options list not have the good registered formats" do
331
+ $test_methodic_options.specify_formats_of :name => /.*/, :surname => /toto.*/
332
+ lambda{$test_methodic_options.validate!}.should raise_error ArgumentError
333
+
334
+ end
335
+ it "should not raise if all options in options list match formats definitions " do
336
+ $test_methodic_options.specify_formats_of :name => /.*/, :surname => /.*/
337
+ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError
338
+ end
339
+ end
340
+
341
+
342
+ end
343
+ context "#merge_with_defaults" do
344
+ it { $test_methodic_options.should respond_to("merge_with_defaults") }
345
+ it { $test_methodic_options.should respond_to("merge") }
346
+ it "should merge defaults values and don't override with options" do
347
+ $test_methodic_options.specify_defaults_values :name => 'Smith', :surname => 'Paul', :nickname => 'Jeedoe'
348
+ $test_methodic_options.merge_with_defaults
349
+ $test_methodic_options[:name].should eq "Doe"
350
+ $test_methodic_options[:surname].should eq "John"
351
+ $test_methodic_options[:nickname].should eq "Jeedoe"
352
+
353
+ end
354
+ end
355
+ end
356
+ end
357
+ context "Methodic::get_options" do
358
+ it "should return a Methodic::Options Object" do
359
+ Methodic::get_options({:name => 'Doe', :surname => 'John'}).should be_an_instance_of Methodic::Options
360
+ end
361
+ end
362
+
363
+ end
@@ -0,0 +1,25 @@
1
+ AssignmentInConditionalCheck:
2
+ CaseMissingElseCheck:
3
+ ClassLineCountCheck:
4
+ line_count: 300
5
+ ClassNameCheck:
6
+ pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
7
+ #ClassVariableCheck:
8
+ CyclomaticComplexityBlockCheck:
9
+ complexity: 4
10
+ CyclomaticComplexityMethodCheck:
11
+ complexity: 10
12
+ EmptyRescueBodyCheck:
13
+ ForLoopCheck:
14
+ MethodLineCountCheck:
15
+ line_count: 30
16
+ MethodNameCheck:
17
+ pattern: !ruby/regexp /^[_a-z<>=\[|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/
18
+ # MissingForeignKeyIndexCheck:
19
+ ModuleLineCountCheck:
20
+ line_count: 500
21
+ ModuleNameCheck:
22
+ pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
23
+ ParameterNumberCheck:
24
+ parameter_count: 5
25
+
metadata CHANGED
@@ -1,12 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: methodic
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
- - 0
8
- - 2
9
- version: "0.2"
6
+ - 1
7
+ - 1
8
+ version: "1.1"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Romain GEORGES
@@ -14,18 +13,16 @@ autorequire:
14
13
  bindir:
15
14
  cert_chain: []
16
15
 
17
- date: 2012-03-21 00:00:00 +01:00
16
+ date: 2013-02-14 00:00:00 +01:00
18
17
  default_executable:
19
18
  dependencies:
20
19
  - !ruby/object:Gem::Dependency
21
20
  name: rspec
22
21
  prerelease: false
23
22
  requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
23
  requirements:
26
24
  - - ">="
27
25
  - !ruby/object:Gem::Version
28
- hash: 15
29
26
  segments:
30
27
  - 2
31
28
  - 0
@@ -46,10 +43,16 @@ files:
46
43
  - lib/methodic.rb
47
44
  - spec/init_spec.rb
48
45
  - spec/spec_helper.rb
46
+ - spec/methodic_spec.rb
49
47
  - Gemfile
50
48
  - Gemfile.lock
51
49
  - methodic.gemspec
52
50
  - Rakefile
51
+ - COPYRIGHT
52
+ - INSTALL
53
+ - AUTHORS
54
+ - ChangeLog
55
+ - ultragreen_roodi_coding_convention.yml
53
56
  has_rdoc: true
54
57
  homepage: http://www.ultragreen.net
55
58
  licenses: []
@@ -57,37 +60,32 @@ licenses: []
57
60
  post_install_message:
58
61
  rdoc_options:
59
62
  - --title
60
- - "Methodic : Ge\xC3m documentation"
63
+ - "Methodic : Gem documentation"
61
64
  - --main
62
65
  - doc/manual.rdoc
63
66
  - --line-numbers
64
- - --diagram
65
67
  require_paths:
66
68
  - lib
67
69
  required_ruby_version: !ruby/object:Gem::Requirement
68
- none: false
69
70
  requirements:
70
71
  - - ">="
71
72
  - !ruby/object:Gem::Version
72
- hash: 53
73
73
  segments:
74
74
  - 1
75
75
  - 8
76
76
  - 1
77
77
  version: 1.8.1
78
78
  required_rubygems_version: !ruby/object:Gem::Requirement
79
- none: false
80
79
  requirements:
81
80
  - - ">="
82
81
  - !ruby/object:Gem::Version
83
- hash: 3
84
82
  segments:
85
83
  - 0
86
84
  version: "0"
87
85
  requirements: []
88
86
 
89
87
  rubyforge_project: nowarning
90
- rubygems_version: 1.4.2
88
+ rubygems_version: 1.3.6
91
89
  signing_key:
92
90
  specification_version: 3
93
91
  summary: "Methodic : Hash table options specification and validation componant"