Sha256: 9036ae72d0136844e3f99bf4c864175ecb3ea8620128c4fb3a00ee3b449e2dad
Contents?: true
Size: 1.44 KB
Versions: 3
Compression:
Stored size: 1.44 KB
Contents
=begin rdoc Store the SQL order_by attributes with ascending and descending values =end # TODO don't use this anymore class OrderAttribute attr_reader :direction, :attribute def initialize( entity_class, sql_order_fragment ) @entity_class = entity_class if sql_order_fragment =~ /^(.*?\.)?(.*?) *asc$/i @direction = :asc @attribute = $2 elsif sql_order_fragment =~ /^(.*?\.)?(.*?) *desc$/i @direction = :desc @attribute = $2 else @direction = :asc @attribute = sql_order_fragment end end # return ORDER BY field name def to_s @string ||= attribute end def to_sym @sym ||= attribute.to_sym end # return 'field ASC' or 'field DESC', depending def to_sql "#{@entity_class.table_name}.#{attribute} #{direction.to_s}" end def reverse( direction ) case direction when :asc; :desc when :desc; :asc else; raise "unknown direction #{direction}" end end # return the opposite ASC or DESC from to_sql def to_reverse_sql "#{@entity_class.table_name}.#{attribute} #{reverse(direction).to_s}" end def ==( other ) @entity_class == other.instance_eval( '@entity_class' ) and self.direction == other.direction and self.attribute == other.attribute end # return -1 for desc, 1 for asc def to_i case direction when :asc; 1 when :desc; -1 else; raise "unknown direction #{direction}" end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
clevic-0.13.0.b3 | lib/clevic/order_attribute.rb |
clevic-0.13.0.b2 | lib/clevic/order_attribute.rb |
clevic-0.13.0.b1 | lib/clevic/order_attribute.rb |