Class: Clevic::AttributeList
Overview
Provide a list of entries for a distinct field, ordered by either value order, or frequency order. TODO move this into the common DistinctDelegate class.
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#attribute_value ⇒ Object
readonly
Returns the value of attribute attribute_value.
-
#entity_class ⇒ Object
readonly
Returns the value of attribute entity_class.
-
#find_options ⇒ Object
readonly
Returns the value of attribute find_options.
Instance Method Summary collapse
-
#conditions(dataset) ⇒ Object
because Sequel::Dataset won’t .filter with {}.
-
#dataset(by_description, by_frequency) ⇒ Object
by_frequency is the default.
-
#dataset_by_description ⇒ Object
sorts by attribute.
-
#dataset_by_frequency ⇒ Object
sorts by first letter then most frequent, instead of pure alphabetical.
-
#initialize(entity_class, attribute, attribute_value, find_options) ⇒ AttributeList
constructor
A new instance of AttributeList.
Constructor Details
#initialize(entity_class, attribute, attribute_value, find_options) ⇒ AttributeList
Returns a new instance of AttributeList.
7 8 9 10 |
# File 'lib/clevic/attribute_list.rb', line 7 def initialize( entity_class, attribute, attribute_value, ) @entity_class = entity_class @attribute, @attribute_value, @find_options = attribute, attribute_value, end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
11 12 13 |
# File 'lib/clevic/attribute_list.rb', line 11 def attribute @attribute end |
#attribute_value ⇒ Object (readonly)
Returns the value of attribute attribute_value.
11 12 13 |
# File 'lib/clevic/attribute_list.rb', line 11 def attribute_value @attribute_value end |
#entity_class ⇒ Object (readonly)
Returns the value of attribute entity_class.
11 12 13 |
# File 'lib/clevic/attribute_list.rb', line 11 def entity_class @entity_class end |
#find_options ⇒ Object (readonly)
Returns the value of attribute find_options.
11 12 13 |
# File 'lib/clevic/attribute_list.rb', line 11 def @find_options end |
Instance Method Details
#conditions(dataset) ⇒ Object
because Sequel::Dataset won’t .filter with {}
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/clevic/attribute_list.rb', line 14 def conditions( dataset ) # make sure the current attribute value is included if there's a filter rv = if .has_key?( :conditions ) [:conditions].lit | { attribute => attribute_value } end # filter if necessary unless rv.nil? dataset.filter( rv ) else dataset end end |
#dataset(by_description, by_frequency) ⇒ Object
by_frequency is the default
51 52 53 54 55 56 57 58 |
# File 'lib/clevic/attribute_list.rb', line 51 def dataset( by_description, by_frequency ) case when by_description dataset_by_description else dataset_by_frequency end end |
#dataset_by_description ⇒ Object
sorts by attribute
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/clevic/attribute_list.rb', line 30 def dataset_by_description # must have attribute equality test first, otherwise if find_options # doesn't have :conditions, then we end up with ( nil | { attribute => attribute_value } ) # which confuses Sequel ds = entity_class.naked \ .order( attribute ) \ .select( attribute ) \ .distinct conditions( ds ) end |
#dataset_by_frequency ⇒ Object
sorts by first letter then most frequent, instead of pure alphabetical
42 43 44 45 46 47 48 |
# File 'lib/clevic/attribute_list.rb', line 42 def dataset_by_frequency ds = entity_class.naked \ .select( attribute, :count.sql_function( attribute ) ) \ .group( attribute ) \ .order( :substr.sql_function( attribute,1,1 ), :count.sql_function( attribute ).desc ) conditions( ds ) end |