0
@@ -40,148 +40,3 @@ module StrokeDB
0
-##########################################################
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
- if doc.meta.name == expected_meta && doc.meta.nsurl == expected_nsurl
0
- if (reference_slotname_value = doc[reference_slotname]) &&
0
- (conditions.keys.select {|k| doc[k] == conditions[k]}.size == conditions.size)))
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
- module HasManyAssociation
0
- attr_reader :association_owner, :association_slotname
0
- association_meta.constantize.new(association_owner.store, slots.merge({association_reference_slotname => association_owner}))
0
- doc.update_slots! association_reference_slotname => association_owner
0
- def association_reference_slotname
0
- association_owner.meta["has_many_#{association_slotname}"][:reference_slotname]
0
- association_owner.meta["has_many_#{association_slotname}"][:expected_meta]
0
- def has_many(slotname, opts={}, &block)
0
- opts = opts.stringify_keys
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
- @meta_initialization_procs << Proc.new do
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
- extend_with = extend_with.name
0
- raise "has_many extension should be either Module or Proc"
0
- reference_slotname = reference_slotname || name.demodulize.tableize.singularize
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
- # meta = _t.join('::')
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
- @args.last.reverse_merge!({"has_many_#{slotname}" => view})
0
- define_method(slotname) do
0
- _has_many_association(slotname)
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
- if extend_with = slot_has_many[:extend_with]
0
- result.extend(extend_with.constantize)
0
- result.instance_variable_set(:@association_owner, self)
0
- result.instance_variable_set(:@association_slotname, slotname)
0
- result.extend(HasManyAssociation)