Class: Momomoto::Order
- Inherits:
-
Object
- Object
- Momomoto::Order
- Defined in:
- lib/momomoto/order.rb
Overview
This class is used to specify the order of returned Rows. Whenever selecting rows you can use plain Symbols for column names or the builtin classes for having the result automatically sorted asc
ending, descending or case-insensitive with lower
.
When executing SELECT on tables the method Base#compile_order is invoked, which compiles the order statement according to Table#default_order if defined. This can be overwritten with options set as parameter for the select method.
class Feeds < Momomoto::Table
default_order Momomoto.desc( :last_changed )
#equals default_order Momomoto::Order::Desc.new( :last_changed )
end
newest_five_feeds = Feeds.select( {}, { :limit => 5} )
=> Returns the five rows from Feeds with the newest column last_changed
Now, if you want to get all feeds sorted by column title instead of last_changed and also want to ignore case, do the following:
feeds_by_title = Feeds.select( {}, {:order => Momomoto::lower(:title)} )
This overwrites the default value for order.
You can also define multiple order statements at once, which are then joined together.
class Feeds < Momomoto::Table
default_order [Momomoto::desc(:last_changed), Momomoto::lower(:title)]
end
Defined Under Namespace
Instance Attribute Summary collapse
-
#fields ⇒ Object
Getter and setter methods for
fields
.
Instance Method Summary collapse
-
#initialize(*fields) ⇒ Order
constructor
Creates a new instance of Order and flattens the given parameter
fields
. -
#to_sql(columns) ⇒ Object
This method is used to build the SQL statement from the given
columns
and their class.
Constructor Details
Instance Attribute Details
#fields ⇒ Object
Getter and setter methods for fields
. fields
are Symbols representing the columns the Order class operates on.
39 40 41 |
# File 'lib/momomoto/order.rb', line 39 def fields @fields end |
Instance Method Details
#to_sql(columns) ⇒ Object
This method is used to build the SQL statement from the given columns
and their class. This method is only invoked from Base#compile_order.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/momomoto/order.rb', line 53 def to_sql( columns ) sql = [] fields.each do | field | if field.kind_of?( Momomoto::Order ) sql << function( field.to_sql( columns ) ) else raise Momomoto::Error, "Unknown field #{field} in order" if not columns.keys.member?( field.to_sym ) sql << function( field ) end end sql.join(',') end |