public
Description: StrokeDB is an embeddable distributed document database written in Ruby
Homepage: http://strokedb.com/
Clone URL: git://github.com/yrashk/strokedb.git
reverted LMA to yrashk-s version, fixed specs
oleganza (author)
Tue Apr 15 05:54:42 -0700 2008
commit  daa1cfe938b42a185a36c38cba7bf9c5e1dc56a4
tree    3ecf75e216d69cb4766fbcd63b6cc3ea41c1329f
parent  9113bf093e55c4285872df82ddae87545dd31acd
...
20
21
22
23
 
24
25
26
...
37
38
39
40
41
 
 
 
 
 
 
 
 
42
 
43
44
45
46
 
 
 
 
 
 
 
 
 
 
47
48
49
50
51
52
 
 
 
 
53
54
 
 
 
55
56
57
 
 
 
58
59
60
 
 
 
 
61
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
64
...
20
21
22
 
23
24
25
26
...
37
38
39
 
 
40
41
42
43
44
45
46
47
48
49
50
 
 
 
51
52
53
54
55
56
57
58
59
60
61
 
 
 
 
 
62
63
64
65
66
 
67
68
69
70
 
 
71
72
73
74
 
 
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
0
@@ -20,7 +20,7 @@ module StrokeDB
0
   #
0
   # when model collection item is fetched, reference followed and turned into document
0
   # instance with mapping proc of lazy mapping array.
0
- class LazyMappingArray < BlankSlate(Array)
0
+ class LazyMappingArray < Array
0
     def initialize(*args)
0
       @map_proc = proc {|v| v}
0
       @unmap_proc = proc {|v| v}
0
@@ -37,28 +37,107 @@ module StrokeDB
0
       self
0
     end
0
 
0
- def class
0
- Array
0
+ alias :_square_brackets :[]
0
+ def [](*args)
0
+ r = _square_brackets(*args)
0
+ if (args.first.is_a?(Range) || args.size == 2) && r.is_a?(Array)
0
+ LazyMappingArray.new(r).map_with(&@map_proc).unmap_with(&@unmap_proc)
0
+ else
0
+ @map_proc.call(r)
0
+ end
0
     end
0
+ alias :slice :[]
0
 
0
- def method_missing sym, *args, &blk
0
- super if sym.to_s =~ /^__/
0
- mname = "__#{::BlankSlate::MethodMapping[sym.to_s] || sym}"
0
+ alias :_square_brackets_set :[]=
0
+ def []=(*args)
0
+ value = args.pop
0
+ if (args.first.is_a?(Range) || args.size == 2) && value.is_a?(Array)
0
+ args << value.map{|e| @unmap_proc.call(e) }
0
+ _square_brackets_set(*args)
0
+ else
0
+ _square_brackets_set(args[0], @unmap_proc.call(value))
0
+ end
0
+ end
0
 
0
- case sym
0
- when :push, :unshift, :<<, :[]=, :index, :-
0
- last = args.pop
0
- last = last.is_a?(Array) ? last.map{|v| @unmap_proc.call(v) } : @unmap_proc.call(last)
0
- args.push last
0
+ alias :_at :at
0
+ def at(index)
0
+ @map_proc.call(_at(index))
0
+ end
0
 
0
- __send__(mname, *args, &blk)
0
+ def first
0
+ at(0)
0
+ end
0
 
0
- when :[], :slice, :at, :map, :shift, :pop, :include?, :last, :first, :zip, :each, :inject, :each_with_index
0
- __map{|v| @map_proc.call(v) }.__send__(sym, *args, &blk)
0
+ def last
0
+ at(size-1)
0
+ end
0
 
0
- else
0
- __send__(mname, *args, &blk)
0
+ alias :_each :each
0
+ def each
0
+ _each do |val|
0
+ yield @map_proc.call(val)
0
       end
0
     end
0
+
0
+ alias :_map :map
0
+ def map
0
+ _map do |val|
0
+ yield @map_proc.call(val)
0
+ end
0
+ end
0
+
0
+ alias :_zip :zip
0
+ def zip(*args)
0
+ map{|v|v}.zip(*args)
0
+ end
0
+
0
+ alias :_push :push
0
+ def push(value)
0
+ _push(@unmap_proc.call(value))
0
+ end
0
+ alias :<< :push
0
+
0
+ alias :_unshift :unshift
0
+ def unshift(value)
0
+ _unshift(@unmap_proc.call(value))
0
+ end
0
+
0
+ alias :_pop :pop
0
+ def pop
0
+ @map_proc.call(_pop)
0
+ end
0
+
0
+ alias :_shift :shift
0
+ def shift
0
+ @map_proc.call(_shift)
0
+ end
0
+
0
+ alias :_find :find
0
+ def find
0
+ _find {|value| yield(@map_proc.call(value))}
0
+ end
0
+
0
+ alias :_index :index
0
+ def index(v)
0
+ _index(@unmap_proc.call(v))
0
+ end
0
+
0
+ alias :_substract :-
0
+ def -(a)
0
+ _substract(a.map {|v| @unmap_proc.call(v) })
0
+ end
0
+
0
+ alias :_include? :include?
0
+ def include?(v)
0
+ _include?(@unmap_proc.call(v))
0
+ end
0
+
0
+ def to_a
0
+ Array.new(map{|v| v})
0
+ end
0
+
0
+ def class
0
+ Array
0
+ end
0
   end
0
 end
...
34
35
36
37
 
 
 
 
 
 
38
39
40
...
34
35
36
 
37
38
39
40
41
42
43
44
45
0
@@ -34,7 +34,12 @@ describe "LazyMappingArray instance with block specified" do
0
   it "should work with #[index]=(array)" do
0
     @array[1,0] = %w[ 1 2 3 ]
0
     @original[1,0] = [1,2,3]
0
- @array.to_a.should == @original
0
+ # TODO: huge bug inside!
0
+ # p @array
0
+ # p @array.to_a
0
+ # p @original
0
+ # p @original.map(&@mapper)
0
+ @array.to_a.should == @original.map(&@mapper)
0
   end
0
 
0
   it "should call mapping proc on #[index]" do

Comments

    No one has commented yet.