Class: Clevic::AttributeList

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

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

Instance Method Summary collapse

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, find_options )
  @entity_class = entity_class
  @attribute, @attribute_value, @find_options = attribute, attribute_value, find_options
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



11
12
13
# File 'lib/clevic/attribute_list.rb', line 11

def attribute
  @attribute
end

#attribute_valueObject (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_classObject (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_optionsObject (readonly)

Returns the value of attribute find_options.



11
12
13
# File 'lib/clevic/attribute_list.rb', line 11

def find_options
  @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 find_options.has_key?( :conditions )
    find_options[: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_descriptionObject

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_frequencyObject

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