public
Description: StrokeDB is an embeddable distributed document database written in Ruby
Homepage: http://strokedb.com/
Clone URL: git://github.com/yrashk/strokedb.git
Some garbage has been removed from index_slots.rb
yrashk (author)
Thu May 22 16:53:33 -0700 2008
commit  c4427188a18e99d3af939c4c3d5eca687d3cf787
tree    b8e3d36cc78a831155b289b08c59530312e6367a
parent  aa68457b31df9ffda318b2f6d0512ba775f52559
...
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
...
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -40,148 +40,3 @@ module StrokeDB
0
 end
0
 
0
 
0
-
0
-
0
-
0
-
0
-
0
-
0
-
0
-##########################################################
0
-----------------------------------------------------------
0
-
0
-
0
-module StrokeDB
0
-
0
-  module Associations
0
-    
0
-    AssociationViewImplementation = Proc.new do |view|
0
-      def view.map(uuid, doc)
0
-        reference_slotname = self[:reference_slotname]
0
-        through = self[:through]
0
-        expected_meta = self[:expected_meta]
0
-        expected_nsurl = self[:expected_nsurl]
0
-        conditions = self[:conditions]
0
-        sort_by = self[:sort_by]
0
-
0
-        if doc.meta.name == expected_meta && doc.meta.nsurl == expected_nsurl
0
-          if (reference_slotname_value = doc[reference_slotname]) &&
0
-             (conditions.nil? ||
0
-              (conditions &&
0
-               (conditions.keys.select {|k| doc[k] == conditions[k]}.size == conditions.size)))
0
-            begin
0
-              key = [reference_slotname_value, doc]
0
-              key = [key[0],doc.send(sort_by),key[1]] if sort_by
0
-              through.each {|t| doc = doc.send(t) }
0
-            rescue SlotNotFoundError
0
-              return nil unless doc
0
-            else
0
-              [ 
0
-                [
0
-                  key,
0
-                  doc
0
-                ]
0
-              ]
0
-            end
0
-        end
0
-      end
0
-      end
0
-    end
0
-
0
-    module HasManyAssociation
0
-      attr_reader :association_owner, :association_slotname
0
-      def new(slots={})
0
-        association_meta.constantize.new(association_owner.store, slots.merge({association_reference_slotname => association_owner}))
0
-      end
0
-      alias :build :new
0
-
0
-      def create!(slots={})
0
-        new(slots).save!
0
-      end
0
-      
0
-      def <<(doc)
0
-        doc.update_slots! association_reference_slotname => association_owner
0
-        self
0
-      end
0
-      
0
-      private 
0
-
0
-      def association_reference_slotname
0
-        association_owner.meta["has_many_#{association_slotname}"][:reference_slotname]
0
-      end
0
-
0
-      def association_meta
0
-        association_owner.meta["has_many_#{association_slotname}"][:expected_meta]
0
-      end
0
-
0
-    end
0
-
0
-    def has_many(slotname, opts={}, &block)
0
-      opts = opts.stringify_keys
0
-
0
-      reference_slotname = opts['foreign_reference']
0
-      through = opts['through'] || []
0
-      through = [through] unless through.is_a?(Array)
0
-      meta = (through.shift || slotname).to_s.singularize.camelize
0
-      nsurl = opts['nsurl'] || (name.modulize.empty? ? Module.nsurl : meta.modulize.constantize.nsurl)
0
-      extend_with = opts['extend'] || block
0
-      conditions = opts['conditions']
0
-      sort_by = opts['sort_by']
0
-      reverse = opts['reverse'] || false
0
-      
0
-      @meta_initialization_procs << Proc.new do
0
-        case extend_with
0
-        when Proc
0
-          extend_with_proc = extend_with
0
-          extend_with = "HasMany#{slotname.to_s.camelize}"
0
-          const_set(extend_with, Module.new(&extend_with_proc))
0
-          extend_with = "#{self.name}::HasMany#{slotname.to_s.camelize}"
0
-        when Module
0
-          extend_with = extend_with.name
0
-        when NilClass
0
-        else
0
-          raise "has_many extension should be either Module or Proc"
0
-        end
0
-        reference_slotname = reference_slotname || name.demodulize.tableize.singularize
0
-        
0
-        # TODO: remove the below commented out code, it seems that we do not need it anymore
0
-        # (but I am not sure, so that's why I've left it here)
0
-        # if name.index('::') # we're in namespaced meta
0
-        #   _t = name.split('::')
0
-        #   _t.pop
0
-        #   _t << meta
0
-        #   meta = _t.join('::') 
0
-        # end
0
-        
0
-        view = View.define!("#{name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl}##{name.demodulize.tableize.singularize}_has_many_#{slotname}",
0
-                            { :reference_slotname => reference_slotname, :through => through, :expected_meta => meta, :expected_nsurl => nsurl, :extend_with => extend_with, 
0
-                              :conditions => conditions, :sort_by => sort_by, :reverse => reverse }, &AssociationViewImplementation)
0
-        
0
-        @args.last.reverse_merge!({"has_many_#{slotname}" => view})
0
-        define_method(slotname) do 
0
-          _has_many_association(slotname)
0
-        end
0
-      end      
0
-    end 
0
-
0
-  private 
0
-
0
-    def initialize_associations
0
-      define_method(:_has_many_association) do |slotname|
0
-        slot_has_many = meta["has_many_#{slotname}"]
0
-        result = LazyArray.new.load_with do |lazy_array|
0
-          slot_has_many.find(:key => self, :reverse => slot_has_many[:reverse])
0
-        end
0
-        if extend_with = slot_has_many[:extend_with] 
0
-          result.extend(extend_with.constantize) 
0
-        end
0
-        result.instance_variable_set(:@association_owner, self)
0
-        result.instance_variable_set(:@association_slotname, slotname)
0
-        result.extend(HasManyAssociation)
0
-        result
0
-      end
0
-    end
0
-  end
0
-end  
0
-
0
-

Comments