Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/criteria.rb

Constant Summary collapse

@@one_to_many_associations =
[]
@@many_to_one_associations =
[]
@@critera_columns =
{}

Class Method Summary collapse

Class Method Details

.[](column) ⇒ Object

Access the column object by using the class as a hash. This is useful if there are other static methods that have the same name as your field, or the field name is not, for some reason, allowed as a ruby method name

Usually, you can just call Class.column_name to access this column object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/criteria.rb', line 518

def self.[](column)
  
  # First check to see if there is a column on this class 
  if self.columns.collect {|c| c.name }.include? column.to_s
    col = self.columns.reject {|c| c.name!=column.to_s }.first
    @@critera_columns[column.to_s] = Criteria::Column.new(self, col, self.connection)
    
  # No?  well lets check if there is an association on this class
  # FIXME: this is very prone to error, we need a way of confirming this
  elsif self.one_to_many_associations.include? column.to_s.intern
    @@critera_columns[column.to_s] = Criteria::OneToManyAssociation.new(self, column, self.connection)
  elsif self.many_to_one_associations.include? column.to_s.intern
    @@critera_columns[column.to_s] = Criteria::ManyToOneAssociation.new(self, column, self.connection)
  end
  
  if @@critera_columns.has_key? column.to_s
    @@critera_columns[column.to_s]
  else
    nil
  end
end

.__criteria_find(*a) ⇒ Object Also known as: find

Provide the ability to search by criteria using the normal find() syntax. Basically we just turn the criteria object into a normal query hash (:conditions, :order, :limit, etc)

Since this can come as the first or second argument in the find() method, we just scan for any instances of Criteria and then call to_hash



558
559
560
561
562
# File 'lib/criteria.rb', line 558

def self.__criteria_find(*a)
  a = rewrite_criteria_in_args(a)
  # puts a.inspect
  __ar_find(*a)
end

.__criteria_method_missing(*a) ⇒ Object Also known as: method_missing



548
549
550
551
# File 'lib/criteria.rb', line 548

def self.__criteria_method_missing(*a)
  # puts a.inspect
  self[a[0]] || __ar_method_missing(*a)
end

.many_to_one_associationsObject



540
541
542
# File 'lib/criteria.rb', line 540

def self.many_to_one_associations
  @@many_to_one_associations
end

.one_to_many_associationsObject



544
545
546
# File 'lib/criteria.rb', line 544

def self.one_to_many_associations
  @@one_to_many_associations
end