Module: ActiveRecord::VirtualBase::VirtualClassMethods

Defined in:
lib/galaxy/virtual_base.rb

Instance Method Summary collapse

Instance Method Details

#datasetObject

Return dataset proper for add/lookup/kill operations



62
63
64
# File 'lib/galaxy/virtual_base.rb', line 62

def dataset
  @@dataset
end

#establish_dataset(dataset) ⇒ Object

Establish dataset to run add/lookup/kill (redefined save/find/delete?) operations against



56
57
58
59
# File 'lib/galaxy/virtual_base.rb', line 56

def establish_dataset( dataset )
  #print 'Dataset established: ', dataset.name if dataset.respond_to? :name and dataset.name
  @@dataset = dataset
end

#has_many_linked(association_id, options = {}, &extension) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/galaxy/virtual_base.rb', line 24

def has_many_linked( association_id, options = {}, &extension )
  #puts "#{self} has_many_linked #{association_id} with options: #{options}"
  #Standard has_many_linked, add standard before and after callback methods
  
  owner_reference = if options[:foreign_key] then options[:foreign_key].split('_')[0] else self.name.downcase end
  
  add_method = "add_linked_#{association_id}".to_sym      
  define_method(add_method) do |association|
    #p association_id, association, self, owner_reference
    owner = association.send(owner_reference.to_sym)
    if owner != self
      # If association already has another current_owner, delete it it from current_owner's collection
      owner.send(association_id.to_sym).delete association if owner
      association.send( (owner_reference+'=').to_sym, self)
    end
  end 
  
  remove_method = "remove_linked_#{association_id}".to_sym      
  define_method(remove_method) do |association|
    owner = association.send(owner_reference.to_sym)
    raise ActiveRecordError if owner and owner != self # Something wrong, association is not pointing back to us 
    association.send( (owner_reference+'=').to_sym, nil)
  end
  
  before = if options[:before_add] then [options[:before_add], add_method] else add_method end
  after = if options[:after_remove] then [options[:after_remove], remove_method] else remove_method end
  has_many association_id, options.merge(:before_add => before, :after_remove => after), &extension
end

#kill(*args) ⇒ Object

Delete objects from the dataset - analog of delete()



79
80
81
82
83
84
85
86
# File 'lib/galaxy/virtual_base.rb', line 79

def kill(*args)
  obj = self.lookup(*args)
  case obj 
    when nil then false
    when Array then obj.each {|o| o.kill}
  else obj.kill  
  end 
end

#lookup(*args) ⇒ Object

Lookup objects of this Class in the dataset - analog of find()



67
68
69
70
71
72
73
74
75
76
# File 'lib/galaxy/virtual_base.rb', line 67

def lookup(*args)
  return nil unless dataset # Dataset is not established, unable to lookup
  # options = args.extract_options!
  case args.first
    when :first then dataset.send(table_name.to_sym).find{|m| m}
    when :last  then dataset.send(table_name.to_sym).compact[-1]
    when :all   then dataset.send(table_name.to_sym).compact
  else dataset.send(table_name.to_sym)[*args]
  end
end

#new_or_update(*args) ⇒ Object

Pass through method that calls new (called on each found Section record) Model should redefine this method for meaningful result (like Planets.new_or_update)



90
91
92
# File 'lib/galaxy/virtual_base.rb', line 90

def new_or_update *args
  new *args
end

#virtualObject

Model class that invokes this macro becomes “virtual” - uses dataset instead of DB connection



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/galaxy/virtual_base.rb', line 12

def virtual
  include ActiveRecord::VirtualBase::VirtualInstanceMethods
  include ActiveRecord::VirtualBase::YourMethods
  include Comparable
  
  # Should be part of a separate macro, but let's leave them here for now
  attr_accessor :idx # To keep track of Model position in container HashArray       
  @@dataset ||= nil
  
  #self.extend(VirtualMetaMethods)
end