dmapper 0.1 → 0.2

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.
@@ -5,6 +5,14 @@ module DMap
5
5
  "Confirmation"
6
6
  end
7
7
 
8
+ def self.is_valid?(command=nil)
9
+ if command.is_a?(String) or command.is_a?(Symbol)
10
+ true
11
+ else
12
+ false
13
+ end
14
+ end
15
+
8
16
  def self.validate(command=nil)
9
17
  {:confirm => command.to_sym}
10
18
  end
@@ -1,49 +1,12 @@
1
1
  module DMap
2
- # Default validations if Property::Type doesn't have it
3
2
  module Validations
4
- attr_accessor :list
3
+ attr_accessor :list, :default
5
4
 
6
5
  class << self
7
6
  def list
8
7
  @list ||= Hash[]
9
8
  end
10
9
 
11
- def default(value=nil)
12
- true
13
- end
14
-
15
- def length(value=nil)
16
- true if value.match /^\d+$/ or value.match /^\d+\.\.\d+$/
17
- end
18
-
19
- def unique(value=nil)
20
- true
21
- end
22
-
23
- def key(value=nil)
24
- true
25
- end
26
-
27
- def required(value=nil)
28
- true
29
- end
30
-
31
- def lazy(value=nil)
32
- false # Not true since lazy is loaded by default
33
- end
34
-
35
- def accessor(value=nil)
36
- return true if value == "protected" or value == "private" or value == "public"
37
- end
38
-
39
- def writer(value=nil)
40
- return true if value == "protected" or value == "private" or value == "public"
41
- end
42
-
43
- def reader(value=nil)
44
- return true if value == "protected" or value == "private" or value == "public"
45
- end
46
-
47
10
  # validates_*_of
48
11
  def add(field, validation, bucket, value)
49
12
  list[field] = {} if list[field].nil?
@@ -56,18 +19,19 @@ module DMap
56
19
  module Core
57
20
  module When
58
21
  def self.run(validation)
59
- {:when => validation.split('-')}
22
+ { :when => validation.split('-').map { |x| ':' + x } }
60
23
  end
61
24
  end
62
25
 
63
26
  module Fields
64
27
  def self.run(validation)
65
- {:fields => validation.split('-')}
28
+ { :fields => validation.split('-') }
66
29
  end
67
30
  end
68
31
 
69
32
  module Numbers
70
- def self.run(validation)
33
+ def self.run(valid)
34
+ validation = valid
71
35
  # Let's get the minimum/maximum numbers e.g. 20min50max
72
36
  minimum = validation[/(\d+)min/, 1] # fetch min
73
37
  unless minimum.nil? # take it out
@@ -87,7 +51,7 @@ module DMap
87
51
  equals = validation[/^(\d+)$/, 1] unless minimum or maximum
88
52
  validation.sub!(/^\d+$/, '')
89
53
 
90
- {:cmd => validation, :min => minimum, :max => maximum, :within => within, :equals => equals}
54
+ Hash[:cmd => validation, :min => minimum, :max => maximum, :within => within, :equals => equals]
91
55
  end
92
56
  end
93
57
  end
@@ -5,6 +5,11 @@ module DMap
5
5
  "FormatOf"
6
6
  end
7
7
 
8
+ def self.is_valid?(command=nil)
9
+ # To many different values to really pin-point
10
+ true
11
+ end
12
+
8
13
  def self.validate(command=nil)
9
14
  command = command.to_sym if command.match(/^[A-Za-z]/)
10
15
  {:as => command}
@@ -1,12 +1,43 @@
1
1
  module DMap
2
2
  module Validations
3
+ # Only need the property tag when the validation attaches itself to the property
4
+ class Length
5
+ attr_accessor :property
6
+
7
+ def self.property; true; end
8
+
9
+ # Return true if it's valid
10
+ def self.is_valid?(command=nil)
11
+ command ||= ""
12
+ !(command.match(/^\d+$/) or command.match(/^\d+\.\.\d+$/)).nil?
13
+ end
14
+
15
+ def self.validate(command=nil)
16
+ command ||= ""
17
+ DMap::Validations::Core::Numbers.run command
18
+ end
19
+ end
20
+
3
21
  class LengthOf
22
+ attr_accessor :ret
23
+
24
+ # for aliases
4
25
  def self.parent_name
5
26
  "LengthOf"
6
27
  end
7
28
 
29
+ def self.ret
30
+ @ret
31
+ end
32
+
33
+ # Return true if it's valid
34
+ def self.is_valid?(command)
35
+ @ret = DMap::Validations::Core::Numbers.run command
36
+ !(ret[:min].nil? and ret[:max].nil? and ret[:within].nil? and ret[:equals].nil?)
37
+ end
38
+
8
39
  def self.validate(command=nil)
9
- ret = DMap::Validations::Core::Numbers.run command
40
+ @ret ||= DMap::Validations::Core::Numbers.run command
10
41
  end
11
42
  end
12
43
  end
@@ -0,0 +1,76 @@
1
+ module DMap
2
+ module Validations
3
+ class Key
4
+ attr_accessor :property
5
+
6
+ def self.property; true; end
7
+
8
+ def self.is_valid?(command=nil)
9
+ command ||= "true"
10
+ (command == "true" or command == "false")
11
+ end
12
+
13
+ def self.validate(command=nil)
14
+ true
15
+ end
16
+ end
17
+
18
+ class Lazy
19
+ attr_accessor :property
20
+
21
+ def self.property; true; end
22
+
23
+ def self.is_valid?(command=nil)
24
+ command ||= "true"
25
+ (command == "true" or command == "false" or !command.match('^[\:A-Za-z_\-]+$').nil?)
26
+ end
27
+
28
+ def self.validate(command=nil)
29
+ if command == "true" or command == "false"
30
+ command
31
+ else
32
+ DMap::Validations::Core::When.run(command)
33
+ end
34
+ end
35
+ end
36
+
37
+ class Default
38
+ attr_accessor :property
39
+
40
+ def self.property; true; end;
41
+
42
+ def self.parent_name
43
+ "Default"
44
+ end
45
+
46
+ def self.is_valid?(command=nil)
47
+ true
48
+ end
49
+
50
+ def self.validate(command=nil)
51
+ command ||= "true"
52
+ end
53
+ end
54
+ class Def < Default; end;
55
+
56
+ class Required
57
+ attr_accessor :property
58
+
59
+ def self.property; true; end;
60
+
61
+ def self.parent_name
62
+ "Required"
63
+ end
64
+
65
+ def self.is_valid?(command)
66
+ command ||= "true"
67
+ (command == "true" or command == "false")
68
+ end
69
+
70
+ def self.validate(command)
71
+ command ||= "true"
72
+ end
73
+ end
74
+ class Req < Required; end;
75
+ end
76
+ end
@@ -5,6 +5,10 @@ module DMap
5
5
  "PresenceOf"
6
6
  end
7
7
 
8
+ def self.is_valid?(command=nil)
9
+ true
10
+ end
11
+
8
12
  def self.validate(command=nil)
9
13
  ret = DMap::Validations::Core::Numbers.run command
10
14
  ret1 = DMap::Validations::Core::When.run ret[:cmd]
@@ -5,6 +5,10 @@ module DMap
5
5
  "PrimitiveTypeOf"
6
6
  end
7
7
 
8
+ def self.is_valid?(command=nil)
9
+ true # always
10
+ end
11
+
8
12
  def self.validate(command=nil)
9
13
  {:null => true}
10
14
  end
@@ -1,10 +1,29 @@
1
1
  module DMap
2
2
  module Validations
3
+ # Only need the property tag when the validation attaches itself to the property
4
+ class Unique
5
+ attr_accessor :property
6
+
7
+ def self.property; true; end;
8
+
9
+ def self.is_valid?(command=nil)
10
+ true # always
11
+ end
12
+
13
+ def self.validate(command=nil)
14
+ (command != "true" or command != "false")
15
+ end
16
+ end
17
+
3
18
  class UniquenessOf
4
19
  def self.parent_name
5
20
  "UniquenessOf"
6
21
  end
7
22
 
23
+ def self.is_valid?(command=nil)
24
+ true # always
25
+ end
26
+
8
27
  def self.validate(command=nil)
9
28
  {:field => :self}
10
29
  end
@@ -0,0 +1,434 @@
1
+ module DMap
2
+ # TITLE:
3
+ #
4
+ # OrderedHash (originally Dictionary)
5
+ #
6
+ # AUTHORS:
7
+ #
8
+ # - Jan Molic
9
+ # - Thomas Sawyer
10
+ #
11
+ # CREDIT:
12
+ #
13
+ # - Andrew Johnson (merge, to_a, inspect, shift and Hash[])
14
+ # - Jeff Sharpe (reverse and reverse!)
15
+ # - Thomas Leitner (has_key? and key?)
16
+ #
17
+ # LICENSE:
18
+ #
19
+ # Copyright (c) 2005 Jan Molic, Thomas Sawyer
20
+ #
21
+ # Ruby License
22
+ #
23
+ # This module is free software. You may use, modify, and/or redistribute this
24
+ # software under the same terms as Ruby.
25
+ #
26
+ # This program is distributed in the hope that it will be useful, but WITHOUT
27
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
28
+ # FOR A PARTICULAR PURPOSE.
29
+ #
30
+ # Originally ported from OrderHash 2.0, Copyright (c) 2005 jan molic
31
+ #
32
+ # LOG:
33
+ #
34
+ # - 2007.10.31 trans
35
+ # Fixed initialize so the constructor blocks correctly effected dictionary
36
+ # rather then just the internal hash.
37
+
38
+ # = Dictionary
39
+ #
40
+ # The Dictionary class is a Hash that preserves order.
41
+ # So it has some array-like extensions also. By defualt
42
+ # a Dictionary object preserves insertion order, but any
43
+ # order can be specified including alphabetical key order.
44
+ #
45
+ # == Usage
46
+ #
47
+ # Just require this file and use Dictionary instead of Hash.
48
+ #
49
+ # # You can do simply
50
+ # hsh = Dictionary.new
51
+ # hsh['z'] = 1
52
+ # hsh['a'] = 2
53
+ # hsh['c'] = 3
54
+ # p hsh.keys #=> ['z','a','c']
55
+ #
56
+ # # or using Dictionary[] method
57
+ # hsh = Dictionary['z', 1, 'a', 2, 'c', 3]
58
+ # p hsh.keys #=> ['z','a','c']
59
+ #
60
+ # # but this doesn't preserve order
61
+ # hsh = Dictionary['z'=>1, 'a'=>2, 'c'=>3]
62
+ # p hsh.keys #=> ['a','c','z']
63
+ #
64
+ # # Dictionary has useful extensions: push, pop and unshift
65
+ # p hsh.push('to_end', 15) #=> true, key added
66
+ # p hsh.push('to_end', 30) #=> false, already - nothing happen
67
+ # p hsh.unshift('to_begin', 50) #=> true, key added
68
+ # p hsh.unshift('to_begin', 60) #=> false, already - nothing happen
69
+ # p hsh.keys #=> ["to_begin", "a", "c", "z", "to_end"]
70
+ # p hsh.pop #=> ["to_end", 15], if nothing remains, return nil
71
+ # p hsh.keys #=> ["to_begin", "a", "c", "z"]
72
+ # p hsh.shift #=> ["to_begin", 30], if nothing remains, return nil
73
+ #
74
+ # == Usage Notes
75
+ #
76
+ # * You can use #order_by to set internal sort order.
77
+ # * #<< takes a two element [k,v] array and inserts.
78
+ # * Use ::auto which creates Dictionay sub-entries as needed.
79
+ # * And ::alpha which creates a new Dictionary sorted by key.
80
+ class OrderedHash
81
+
82
+ include Enumerable
83
+
84
+ class << self
85
+ #--
86
+ # TODO is this needed? Doesn't the super class do this?
87
+ #++
88
+ def [](*args)
89
+ hsh = new
90
+ if Hash === args[0]
91
+ hsh.replace(args[0])
92
+ elsif (args.size % 2) != 0
93
+ raise ArgumentError, "odd number of elements for Hash"
94
+ else
95
+ while !args.empty?
96
+ hsh[args.shift] = args.shift
97
+ end
98
+ end
99
+ hsh
100
+ end
101
+
102
+ # Like #new but the block sets the order.
103
+ #
104
+ def new_by(*args, &blk)
105
+ new(*args).order_by(&blk)
106
+ end
107
+
108
+ # Alternate to #new which creates a dictionary sorted by key.
109
+ #
110
+ # d = Dictionary.alpha
111
+ # d["z"] = 1
112
+ # d["y"] = 2
113
+ # d["x"] = 3
114
+ # d #=> {"x"=>3,"y"=>2,"z"=>2}
115
+ #
116
+ # This is equivalent to:
117
+ #
118
+ # Dictionary.new.order_by { |key,value| key }
119
+ def alpha(*args, &block)
120
+ new(*args, &block).order_by_key
121
+ end
122
+
123
+ # Alternate to #new which auto-creates sub-dictionaries as needed.
124
+ #
125
+ # d = Dictionary.auto
126
+ # d["a"]["b"]["c"] = "abc" #=> { "a"=>{"b"=>{"c"=>"abc"}}}
127
+ #
128
+ def auto(*args)
129
+ #AutoDictionary.new(*args)
130
+ leet = lambda { |hsh, key| hsh[key] = new(&leet) }
131
+ new(*args, &leet)
132
+ end
133
+ end
134
+
135
+ # New Dictiionary.
136
+ def initialize(*args, &blk)
137
+ @order = []
138
+ @order_by = nil
139
+ if blk
140
+ dict = self # This ensure autmatic key entry effect the
141
+ oblk = lambda{ |hsh, key| blk[dict,key] } # dictionary rather then just the interal hash.
142
+ @hash = Hash.new(*args, &oblk)
143
+ else
144
+ @hash = Hash.new(*args)
145
+ end
146
+ end
147
+
148
+ def order
149
+ reorder if @order_by
150
+ @order
151
+ end
152
+
153
+ # Keep dictionary sorted by a specific sort order.
154
+ def order_by( &block )
155
+ @order_by = block
156
+ order
157
+ self
158
+ end
159
+
160
+ # Keep dictionary sorted by key.
161
+ #
162
+ # d = Dictionary.new.order_by_key
163
+ # d["z"] = 1
164
+ # d["y"] = 2
165
+ # d["x"] = 3
166
+ # d #=> {"x"=>3,"y"=>2,"z"=>2}
167
+ #
168
+ # This is equivalent to:
169
+ #
170
+ # Dictionary.new.order_by { |key,value| key }
171
+ #
172
+ # The initializer Dictionary#alpha also provides this.
173
+ def order_by_key
174
+ @order_by = lambda { |k,v| k }
175
+ order
176
+ self
177
+ end
178
+
179
+ # Keep dictionary sorted by value.
180
+ #
181
+ # d = Dictionary.new.order_by_value
182
+ # d["z"] = 1
183
+ # d["y"] = 2
184
+ # d["x"] = 3
185
+ # d #=> {"x"=>3,"y"=>2,"z"=>2}
186
+ #
187
+ # This is equivalent to:
188
+ #
189
+ # Dictionary.new.order_by { |key,value| value }
190
+ def order_by_value
191
+ @order_by = lambda { |k,v| v }
192
+ order
193
+ self
194
+ end
195
+
196
+ #
197
+ def reorder
198
+ if @order_by
199
+ assoc = @order.collect{ |k| [k,@hash[k]] }.sort_by(&@order_by)
200
+ @order = assoc.collect{ |k,v| k }
201
+ end
202
+ @order
203
+ end
204
+
205
+ def ==(hsh2)
206
+ if hsh2.is_a?( Dictionary )
207
+ @order == hsh2.order &&
208
+ @hash == hsh2.instance_variable_get("@hash")
209
+ else
210
+ false
211
+ end
212
+ end
213
+
214
+ def [] k
215
+ @hash[ k ]
216
+ end
217
+
218
+ def fetch(k, *a, &b)
219
+ @hash.fetch(k, *a, &b)
220
+ end
221
+
222
+ # Store operator.
223
+ #
224
+ # h[key] = value
225
+ #
226
+ # Or with additional index.
227
+ #
228
+ # h[key,index] = value
229
+ def []=(k, i=nil, v=nil)
230
+ if v
231
+ insert(i,k,v)
232
+ else
233
+ store(k,i)
234
+ end
235
+ end
236
+
237
+ def insert( i,k,v )
238
+ @order.insert( i,k )
239
+ @hash.store( k,v )
240
+ end
241
+
242
+ def store( a,b )
243
+ @order.push( a ) unless @hash.has_key?( a )
244
+ @hash.store( a,b )
245
+ end
246
+
247
+ def clear
248
+ @order = []
249
+ @hash.clear
250
+ end
251
+
252
+ def delete( key )
253
+ @order.delete( key )
254
+ @hash.delete( key )
255
+ end
256
+
257
+ def each_key
258
+ order.each { |k| yield( k ) }
259
+ self
260
+ end
261
+
262
+ def each_value
263
+ order.each { |k| yield( @hash[k] ) }
264
+ self
265
+ end
266
+
267
+ def each
268
+ order.each { |k| yield( k,@hash[k] ) }
269
+ self
270
+ end
271
+ alias each_pair each
272
+
273
+ def delete_if
274
+ order.clone.each { |k| delete k if yield(k,@hash[k]) }
275
+ self
276
+ end
277
+
278
+ def values
279
+ ary = []
280
+ order.each { |k| ary.push @hash[k] }
281
+ ary
282
+ end
283
+
284
+ def keys
285
+ order
286
+ end
287
+
288
+ def invert
289
+ hsh2 = self.class.new
290
+ order.each { |k| hsh2[@hash[k]] = k }
291
+ hsh2
292
+ end
293
+
294
+ def reject( &block )
295
+ self.dup.delete_if(&block)
296
+ end
297
+
298
+ def reject!( &block )
299
+ hsh2 = reject(&block)
300
+ self == hsh2 ? nil : hsh2
301
+ end
302
+
303
+ def replace( hsh2 )
304
+ @order = hsh2.order
305
+ @hash = hsh2.hash
306
+ end
307
+
308
+ def shift
309
+ key = order.first
310
+ key ? [key,delete(key)] : super
311
+ end
312
+
313
+ def unshift( k,v )
314
+ unless @hash.include?( k )
315
+ @order.unshift( k )
316
+ @hash.store( k,v )
317
+ true
318
+ else
319
+ false
320
+ end
321
+ end
322
+
323
+ def <<(kv)
324
+ push( *kv )
325
+ end
326
+
327
+ def push( k,v )
328
+ unless @hash.include?( k )
329
+ @order.push( k )
330
+ @hash.store( k,v )
331
+ true
332
+ else
333
+ false
334
+ end
335
+ end
336
+
337
+ def pop
338
+ key = order.last
339
+ key ? [key,delete(key)] : nil
340
+ end
341
+
342
+ def inspect
343
+ ary = []
344
+ each {|k,v| ary << k.inspect + "=>" + v.inspect}
345
+ '{' + ary.join(", ") + '}'
346
+ end
347
+
348
+ def dup
349
+ a = []
350
+ each{ |k,v| a << k; a << v }
351
+ self.class[*a]
352
+ end
353
+
354
+ def update( hsh2 )
355
+ hsh2.each { |k,v| self[k] = v }
356
+ reorder
357
+ self
358
+ end
359
+ alias :merge! update
360
+
361
+ def merge( hsh2 )
362
+ self.dup.update(hsh2)
363
+ end
364
+
365
+ def select
366
+ ary = []
367
+ each { |k,v| ary << [k,v] if yield k,v }
368
+ ary
369
+ end
370
+
371
+ def reverse!
372
+ @order.reverse!
373
+ self
374
+ end
375
+
376
+ def reverse
377
+ dup.reverse!
378
+ end
379
+
380
+ def first
381
+ @hash[order.first]
382
+ end
383
+
384
+ def last
385
+ @hash[order.last]
386
+ end
387
+
388
+ def length
389
+ @order.length
390
+ end
391
+ alias :size :length
392
+
393
+ def empty?
394
+ @hash.empty?
395
+ end
396
+
397
+ def has_key?(key)
398
+ @hash.has_key?(key)
399
+ end
400
+
401
+ def key?(key)
402
+ @hash.key?(key)
403
+ end
404
+
405
+ def to_a
406
+ ary = []
407
+ each { |k,v| ary << [k,v] }
408
+ ary
409
+ end
410
+
411
+ def to_json
412
+ buf = "["
413
+ map do |k,v|
414
+ buf << k.to_json
415
+ buf << ", "
416
+ buf << v.to_json
417
+ end.join(", ")
418
+ buf << "]"
419
+ buf
420
+ end
421
+
422
+ def to_s
423
+ self.to_a.to_s
424
+ end
425
+
426
+ def to_hash
427
+ @hash.dup
428
+ end
429
+
430
+ def to_h
431
+ @hash.dup
432
+ end
433
+ end
434
+ end