methodic 1.2 → 1.3
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.
- checksums.yaml +7 -0
- data/ChangeLog +3 -1
- data/Gemfile +0 -1
- data/{doc/manual.rdoc → README.md} +26 -16
- data/Rakefile +13 -7
- data/lib/methodic.rb +60 -48
- data/methodic.gemspec +15 -10
- data/spec/methodic_spec.rb +138 -197
- data/spec/spec_helper.rb +0 -1
- metadata +138 -77
- data/Gemfile.lock +0 -37
- data/INSTALL +0 -7
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1707f3f27d5350def0846240d4c4f7582b05b2b1f3b6aac5b7d9415be25f81fc
|
4
|
+
data.tar.gz: 74cbf27c2cbbb5cb62864200b8589d68ecc9a40ea723a1dbe97dfaa0b0a5e630
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b9bbf2c528d99307321a8d687be03907aabbd8cba955feddfe739744ae1fc22f020a8f95cb5b30cd003700774bee08945a1a82f4d10f1d533c45719a4f04bf1
|
7
|
+
data.tar.gz: 8749230565c40f51376ac2a444e7422297f87186184e2f14edd3769b02cbd3960df19f00fc5cef84083826def98615ccc0655f15c367b079fd98874fb07bd4ba
|
data/ChangeLog
CHANGED
@@ -16,6 +16,8 @@ methodic :
|
|
16
16
|
- version 1.1 (RC2)
|
17
17
|
Romain GEORGES
|
18
18
|
- stable version ( production ready)
|
19
|
-
- version 1.2 (
|
19
|
+
- version 1.2 (depricated)
|
20
20
|
Romain GEORGES
|
21
21
|
- NEW : conditions support see manual
|
22
|
+
- version 1.3 (STABLE)
|
23
|
+
- update for compatibility
|
data/Gemfile
CHANGED
@@ -1,32 +1,39 @@
|
|
1
|
-
|
1
|
+
# Methodic
|
2
2
|
|
3
|
-
|
3
|
+
## Content
|
4
4
|
|
5
|
-
Author
|
6
|
-
Version
|
7
|
-
WWW
|
5
|
+
* Author : Romain GEORGES <romain@ultragreen.net>
|
6
|
+
* Version : 1.2
|
7
|
+
* WWW : http://www.ultragreen.net/projects/methodic
|
8
8
|
|
9
|
-
|
9
|
+
|
10
|
+
[](https://travis-ci.org/lecid/methodic) [](https://codeclimate.com/github/lecid/methodic)
|
11
|
+
[](https://gemnasium.com/lecid/methodic)
|
12
|
+
[](http://badge.fury.io/rb/methodic)
|
13
|
+
|
14
|
+
## Description
|
10
15
|
|
11
16
|
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
17
|
It raise explained exceptions and return false if the validations steps failed.
|
13
18
|
|
14
|
-
|
19
|
+
## Installation
|
15
20
|
|
16
21
|
In a valid Ruby environment :
|
17
22
|
|
23
|
+
```
|
18
24
|
$ sudo zsh
|
19
25
|
# gem ins methodic
|
20
|
-
|
21
|
-
|
26
|
+
```
|
27
|
+
## Implementation
|
22
28
|
|
23
29
|
* [Methodic]
|
24
30
|
* [Methodic::Options]
|
25
31
|
|
26
|
-
|
32
|
+
## Examples
|
27
33
|
|
28
|
-
|
34
|
+
### Without known options control
|
29
35
|
|
36
|
+
```ruby
|
30
37
|
require 'rubygems'
|
31
38
|
require 'methodic'
|
32
39
|
[...]
|
@@ -45,9 +52,11 @@ In a valid Ruby environment :
|
|
45
52
|
# processing method
|
46
53
|
end
|
47
54
|
[...]
|
55
|
+
```
|
48
56
|
|
49
|
-
|
57
|
+
### With known options control
|
50
58
|
|
59
|
+
```ruby
|
51
60
|
require 'rubygems'
|
52
61
|
require 'methodic'
|
53
62
|
[...]
|
@@ -67,8 +76,9 @@ In a valid Ruby environment :
|
|
67
76
|
# processing method
|
68
77
|
end
|
69
78
|
[...]
|
79
|
+
```
|
70
80
|
|
71
|
-
|
81
|
+
### Remarque about conditions
|
72
82
|
|
73
83
|
|
74
84
|
* Condition MUST :
|
@@ -80,7 +90,7 @@ In a valid Ruby environment :
|
|
80
90
|
|
81
91
|
* Make your condition like
|
82
92
|
|
83
|
-
|
93
|
+
```ruby
|
84
94
|
aCond = Proc::new do |option|
|
85
95
|
case options
|
86
96
|
when .... then ...
|
@@ -88,10 +98,10 @@ In a valid Ruby environment :
|
|
88
98
|
else ...
|
89
99
|
end
|
90
100
|
end
|
91
|
-
|
101
|
+
```
|
92
102
|
|
93
103
|
|
94
|
-
|
104
|
+
## Copyright
|
95
105
|
|
96
106
|
<pre>Methodic (c) 2012-2013 Romain GEORGES <romain@ultragreen.net> for Ultragreen Software </pre>
|
97
107
|
|
data/Rakefile
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "bundler/gem_tasks"
|
1
3
|
require 'rubygems'
|
2
|
-
|
3
4
|
require 'rspec'
|
4
5
|
require 'rake'
|
5
6
|
require "rake/clean"
|
6
7
|
require "rubygems/package_task"
|
7
8
|
require "rdoc/task"
|
8
|
-
require 'code_statistics'
|
9
9
|
require 'rspec/core/rake_task'
|
10
10
|
require 'yard'
|
11
11
|
require 'yard/rake/yardoc_task.rb'
|
12
12
|
require "rake/tasklib"
|
13
13
|
require "roodi"
|
14
14
|
require "roodi_task"
|
15
|
-
|
16
|
-
|
15
|
+
require 'code_statistics'
|
16
|
+
|
17
17
|
RoodiTask.new() do | t |
|
18
18
|
t.patterns = %w(lib/**/*.rb)
|
19
19
|
t.config = "ultragreen_roodi_coding_convention.yml"
|
@@ -32,12 +32,12 @@ RSpec::Core::RakeTask.new('spec')
|
|
32
32
|
|
33
33
|
|
34
34
|
YARD::Rake::YardocTask.new do |t|
|
35
|
-
t.files = [ 'lib/**/*.rb', '-', 'doc/**/*','spec/**/*_spec.rb']
|
35
|
+
t.files = [ 'lib/**/*.rb', '-', 'doc/**/*','spec/**/*_spec.rb']
|
36
36
|
t.options += ['--title', "Gem Documentation"]
|
37
37
|
t.options += ['-o', "yardoc"]
|
38
38
|
t.options += ['-r', "doc/manual.rdoc"]
|
39
39
|
end
|
40
|
-
YARD::Config.load_plugin('yard-rspec')
|
40
|
+
YARD::Config.load_plugin('yard-rspec')
|
41
41
|
|
42
42
|
namespace :yardoc do
|
43
43
|
task :clobber do
|
@@ -60,4 +60,10 @@ Rake::RDocTask.new('rdoc') do |d|
|
|
60
60
|
d.options << '--line-numbers' << '--diagram' << '-SHN'
|
61
61
|
end
|
62
62
|
|
63
|
-
task :default => [:gem]
|
63
|
+
task :default => [:gem]
|
64
|
+
|
65
|
+
task :stage do
|
66
|
+
Rake::Task["clean"].invoke
|
67
|
+
Rake::Task["clobber"].invoke
|
68
|
+
Rake::Task["install"].invoke
|
69
|
+
end
|
data/lib/methodic.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# Copyright Ultragreen (c) 2012-2�013
|
1
|
+
# coding: utf-8
|
2
|
+
# Copyright Ultragreen (c) 2012-2Â013
|
4
3
|
#---
|
5
|
-
# Author : Romain GEORGES
|
6
|
-
# type : gem component library
|
4
|
+
# Author : Romain GEORGES
|
5
|
+
# type : gem component library
|
7
6
|
# obj : Methodic Module
|
8
7
|
#---
|
9
8
|
|
10
|
-
|
9
|
+
require 'drb'
|
10
|
+
|
11
|
+
# inherited List class from Array to patch push for uniqness carateristique
|
11
12
|
class List < Array
|
12
13
|
|
13
14
|
# override of push for uniqness and flatten return
|
14
|
-
def push(*value)
|
15
|
+
def push(*value)
|
15
16
|
super(value)
|
16
17
|
self.flatten!
|
17
18
|
self.uniq!
|
@@ -30,12 +31,12 @@ class List < Array
|
|
30
31
|
end
|
31
32
|
|
32
33
|
|
33
|
-
# module Methodic
|
34
|
+
# module Methodic
|
34
35
|
# @author Romain GEORGES <romain@ultragreen.net>
|
35
36
|
# @see http://www.ultragreen.net/projects/methodic
|
36
37
|
# @version 0.2
|
37
38
|
# @note this module include a class, but please use the module method Methodic::get_options to Build a Methodic::Options instance
|
38
|
-
# @example Complete usage
|
39
|
+
# @example Complete usage
|
39
40
|
# require 'rubygems'
|
40
41
|
# require 'methodic'
|
41
42
|
# [...]
|
@@ -44,7 +45,7 @@ end
|
|
44
45
|
# myOptions = Methodic::get_options(_options,true) do |m|
|
45
46
|
# m.specify_known_options [:country,:name,:surname,:age]
|
46
47
|
# m.specify_default_value :country => 'France'
|
47
|
-
# aCond = Proc::new {|option| case options when 'Doe' then true else false end }
|
48
|
+
# aCond = Proc::new {|option| case options when 'Doe' then true else false end }
|
48
49
|
# m.specify_condition_for :name => aCond
|
49
50
|
# m.specify_classes_of :name => String, :surname => String, :age => Fixnum, :country => String
|
50
51
|
# m.specify_presence_of :name
|
@@ -60,7 +61,7 @@ module Methodic
|
|
60
61
|
# class Options
|
61
62
|
# an Options utility class
|
62
63
|
# @note please do not instantiate with Options::new use Methodic::get_options
|
63
|
-
# @example Complete usage
|
64
|
+
# @example Complete usage
|
64
65
|
# require 'rubygems'
|
65
66
|
# require 'methodic'
|
66
67
|
# [...]
|
@@ -69,7 +70,7 @@ module Methodic
|
|
69
70
|
# myOptions = Methodic::get_options(_options,true) do |m|
|
70
71
|
# m.specify_known_options [:country,:name,:surname,:age]
|
71
72
|
# m.specify_default_value :country => 'France'
|
72
|
-
# aCond = Proc::new {|option| case options when 'Doe' then true else false end }
|
73
|
+
# aCond = Proc::new {|option| case options when 'Doe' then true else false end }
|
73
74
|
# m.specify_condition_for :name => aCond
|
74
75
|
# m.specify_classes_of :name => String, :surname => String, :age => Fixnum, :country => String
|
75
76
|
# m.specify_presence_of :name
|
@@ -87,17 +88,17 @@ module Methodic
|
|
87
88
|
# myOptions.known = [:name]
|
88
89
|
# myOptions.known.push :surname
|
89
90
|
# @example reading
|
90
|
-
# p myOptions.known
|
91
|
+
# p myOptions.known
|
91
92
|
# => [ :name, :surname ]
|
92
|
-
# @attr [Hash] the list of all options assumed for a method
|
93
|
+
# @attr [Hash] the list of all options assumed for a method
|
93
94
|
attr_accessor :known
|
94
|
-
|
95
|
+
|
95
96
|
# @example writing
|
96
97
|
# myOptions = Methodic::get_options(options)
|
97
98
|
# myOptions.classes = {:name => String }
|
98
99
|
# myOptions.classes[:surname] = String
|
99
100
|
# @example reading
|
100
|
-
# p myOptions.classes
|
101
|
+
# p myOptions.classes
|
101
102
|
# => { :name => String, :surname => String }
|
102
103
|
# @attr [Hash] classes a hash table of some options associated with their corresponding types or classes
|
103
104
|
attr_accessor :classes
|
@@ -108,7 +109,7 @@ module Methodic
|
|
108
109
|
# myOptions.mandatories = [:name]
|
109
110
|
# myOptions.mandatories.push :surname
|
110
111
|
# @example reading
|
111
|
-
# p myOptions.mandatories
|
112
|
+
# p myOptions.mandatories
|
112
113
|
# => [ :name, :surname ]
|
113
114
|
attr_accessor :mandatories
|
114
115
|
|
@@ -118,7 +119,7 @@ module Methodic
|
|
118
119
|
# myOptions.defaults = {:name => 'John' }
|
119
120
|
# myOptions.defaults[:surname] = 'Doe'
|
120
121
|
# @example reading
|
121
|
-
# p myOptions.defaults
|
122
|
+
# p myOptions.defaults
|
122
123
|
# => { :name => 'John', :surname => 'Doe' }
|
123
124
|
attr_accessor :defaults
|
124
125
|
|
@@ -128,17 +129,18 @@ module Methodic
|
|
128
129
|
# myOptions.formats = {:name => /\w+/ }
|
129
130
|
# myOptions.formats[:surname] = /\w+/
|
130
131
|
# @example reading
|
131
|
-
# p myOptions.defaults
|
132
|
+
# p myOptions.defaults
|
132
133
|
# => { :name => /\w+/, :surname => /\w+/ }
|
133
134
|
attr_accessor :formats
|
134
|
-
|
135
|
+
attr_accessor :toto
|
136
|
+
|
135
137
|
# @attr [Hash] conditions a hash table of some conditions with their corresponding
|
136
138
|
# @example writing
|
137
139
|
# myOptions = Methodic::get_options(options)
|
138
140
|
# myOptions.conditions = {:name => aProcObject }
|
139
141
|
# myOptions.conditions[:surname] = aProcObject
|
140
142
|
# @example reading
|
141
|
-
# p myOptions.defaults
|
143
|
+
# p myOptions.defaults
|
142
144
|
# => { :name => /\w+/, :surname => /\w+/ }
|
143
145
|
attr_accessor :conditions
|
144
146
|
|
@@ -151,9 +153,18 @@ module Methodic
|
|
151
153
|
# @return [Options] self
|
152
154
|
def initialize(_options = {},_validate_known_options = false)
|
153
155
|
|
154
|
-
raise ArgumentError::new('Argument _options must be a Hash') unless _options.class == Hash or _options.class == Methodic::Options
|
155
|
-
|
156
|
-
|
156
|
+
raise ArgumentError::new('Argument _options must be a Hash') unless _options.class == Hash or _options.class == Methodic::Options or _options.class == DRb::DRbObject
|
157
|
+
# ;) reintrance and cascading
|
158
|
+
raise ArgumentError::new('keys must be Symbol') unless _options.keys.select{|i| i.class == Symbol }.size == _options.keys.size
|
159
|
+
if _options.class == DRb::DRbObject then
|
160
|
+
self.clear
|
161
|
+
_options.each do |key,value|
|
162
|
+
self[key] = value
|
163
|
+
end
|
164
|
+
else
|
165
|
+
self.replace _options
|
166
|
+
end
|
167
|
+
|
157
168
|
@conditions = Hash::new
|
158
169
|
@defaults = Hash::new
|
159
170
|
@formats = Hash::new
|
@@ -163,7 +174,7 @@ module Methodic
|
|
163
174
|
@validate_known_options = _validate_known_options
|
164
175
|
yield self if block_given?
|
165
176
|
end
|
166
|
-
|
177
|
+
|
167
178
|
|
168
179
|
# read only accessor on the [Hash] slef keys
|
169
180
|
# @return [Array] self.keys the keys of The Options object itself
|
@@ -171,13 +182,13 @@ module Methodic
|
|
171
182
|
# options = {:name => 'Doe', :surname => 'John'}
|
172
183
|
# p myOptions = Methodic::get_options(options)
|
173
184
|
# => { :name => String, :surname => String }
|
174
|
-
# p myOptions.options
|
185
|
+
# p myOptions.options
|
175
186
|
# => [:name, :surname]
|
176
|
-
def options
|
187
|
+
def options
|
177
188
|
return self.keys
|
178
189
|
end
|
179
190
|
|
180
|
-
|
191
|
+
|
181
192
|
# pretty accessor for specifying the default(s) value(s) for options
|
182
193
|
# @param [Hash] values a value definition, keys are symbols
|
183
194
|
# @return [hash] @defaults merged with values
|
@@ -190,7 +201,7 @@ module Methodic
|
|
190
201
|
return @defaults
|
191
202
|
end
|
192
203
|
alias :specify_defaults_values :specify_default_value
|
193
|
-
|
204
|
+
|
194
205
|
# pretty accessor for specifying classes of options
|
195
206
|
# @param [Hash] values a value definition, keys are symbols
|
196
207
|
# @return [hash] @classes merged with values
|
@@ -221,19 +232,19 @@ module Methodic
|
|
221
232
|
alias :specify_conditions_for :specify_condition_for
|
222
233
|
|
223
234
|
|
224
|
-
|
235
|
+
|
225
236
|
# pretty accessor for specifying mandatories options
|
226
237
|
# @param [Array] values a Array of symbols or a unique symbol
|
227
238
|
# @return [Array] @mandatories merged with values
|
228
239
|
# @example usage
|
229
240
|
# myOptions = Methodic::get_options(_options)
|
230
|
-
# myOptions.specify_presence_of :name
|
241
|
+
# myOptions.specify_presence_of :name
|
231
242
|
# myOptions.specify_presences_of [ :name, :surname ]
|
232
|
-
def specify_presence_of(*values)
|
243
|
+
def specify_presence_of(*values)
|
233
244
|
@mandatories << values
|
234
245
|
@mandatories.flatten!
|
235
246
|
@mandatories.uniq!
|
236
|
-
return @mandatories
|
247
|
+
return @mandatories
|
237
248
|
end
|
238
249
|
alias :specify_presences_of :specify_presence_of
|
239
250
|
|
@@ -243,16 +254,16 @@ module Methodic
|
|
243
254
|
# @return [Array] @known merged with values
|
244
255
|
# @example usage
|
245
256
|
# myOptions = Methodic::get_options(_options)
|
246
|
-
# myOptions.specify_known_option :name
|
257
|
+
# myOptions.specify_known_option :name
|
247
258
|
# myOptions.specify_known_options [ :name, :surname ]
|
248
|
-
def specify_known_option(*values)
|
259
|
+
def specify_known_option(*values)
|
249
260
|
@known << values
|
250
261
|
@known.flatten!
|
251
262
|
@known.uniq!
|
252
|
-
return @known
|
263
|
+
return @known
|
253
264
|
end
|
254
265
|
alias :specify_known_options :specify_known_option
|
255
|
-
|
266
|
+
|
256
267
|
# pretty accessor for specifying the format of options
|
257
268
|
# @param [Hash] values a value definition, keys are symbols
|
258
269
|
# @return [hash] @formats merged with values
|
@@ -266,19 +277,19 @@ module Methodic
|
|
266
277
|
return @formats
|
267
278
|
end
|
268
279
|
alias :specify_formats_of :specify_format_of
|
269
|
-
|
270
|
-
|
271
|
-
# default values merge method
|
272
|
-
# merge @defaults with self
|
280
|
+
|
281
|
+
|
282
|
+
# default values merge method
|
283
|
+
# merge @defaults with self
|
273
284
|
# @return [self|Options]
|
274
285
|
# @example usage
|
275
286
|
# myOptions = Methodic::get_options(:name = 'Walker')
|
276
287
|
# myOptions.specify_default_value_of :surname => 'John'
|
277
|
-
# p myOptions
|
288
|
+
# p myOptions
|
278
289
|
# # =>{:surname=>"John", :name=>"Doe"}
|
279
290
|
# myOptions.merge
|
280
291
|
# p myOptions
|
281
|
-
# # =>{:surname=>"John", :name=>"Walker"}
|
292
|
+
# # =>{:surname=>"John", :name=>"Walker"}
|
282
293
|
def merge_with_defaults
|
283
294
|
self.replace( @defaults.merge self)
|
284
295
|
return self
|
@@ -296,19 +307,20 @@ module Methodic
|
|
296
307
|
table.push validate_presences unless @mandatories.empty?
|
297
308
|
table.push validate_formats unless @formats.empty?
|
298
309
|
table.push validate_conditions unless @conditions.empty?
|
299
|
-
return true unless table.include?(false)
|
310
|
+
return true unless table.include?(false)
|
300
311
|
end
|
301
312
|
alias :validate! :validate
|
302
|
-
|
313
|
+
|
314
|
+
# here become private
|
303
315
|
private
|
304
316
|
# private method for the formats validation step
|
305
317
|
def validate_formats
|
306
318
|
self.each do |option,value|
|
307
319
|
if @formats.key? option then
|
308
|
-
raise ArgumentError::new("Option : #{option} don't match /#{@formats[option]}/") and return false unless value =~ @formats[option]
|
320
|
+
raise ArgumentError::new("Option : #{option} don't match /#{@formats[option]}/") and return false unless value =~ @formats[option]
|
309
321
|
end
|
310
322
|
end
|
311
|
-
|
323
|
+
|
312
324
|
return true
|
313
325
|
end
|
314
326
|
|
@@ -367,6 +379,6 @@ module Methodic
|
|
367
379
|
def Methodic::get_options(_options = {},_validate_known_options=false)
|
368
380
|
return Methodic::Options::new(_options,_validate_known_options)
|
369
381
|
end
|
370
|
-
|
382
|
+
|
371
383
|
|
372
384
|
end
|