Class: Clevic::ActiveRecordAdaptor

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

Instance Method Summary collapse

Constructor Details

#initialize(entity_class) ⇒ ActiveRecordAdaptor

Returns a new instance of ActiveRecordAdaptor.



38
39
40
# File 'lib/clevic/sequel_ar_adapter.rb', line 38

def initialize( entity_class )
  @entity_class = entity_class
end

Instance Method Details

#attribute_list(attribute, attribute_value, by_description, by_frequency, find_options, &block) ⇒ Object

values are passed as row objects to block



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/clevic/sequel_ar_adapter.rb', line 81

def attribute_list( attribute, attribute_value, by_description, by_frequency, find_options, &block )
  query =
  case
    when by_description
      entity_class.adaptor.query_order_description( attribute, attribute_value, find_options )
    when by_frequency
      entity_class.adaptor.query_order_frequency( attribute, attribute_value, find_options )
    else
      entity_class.adaptor.query_order_frequency( attribute, attribute_value, find_options )
  end

  entity_class.connection.execute( query ).each( &block )
end

#count(attribute = nil, options = {}) ⇒ Object

options is a hash



51
52
53
# File 'lib/clevic/sequel_ar_adapter.rb', line 51

def count( attribute = nil, options = {} )
  @entity_class.count( attribute, options )
end

#find(options) ⇒ Object



55
56
57
# File 'lib/clevic/sequel_ar_adapter.rb', line 55

def find( options )
  @entity_class.find( :all, options )
end

#query_order_description(attribute, attribute_value, find_options) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/clevic/sequel_ar_adapter.rb', line 59

def query_order_description( attribute, attribute_value, find_options )
  <<-EOF
    select distinct #{attribute.to_s}, lower(#{attribute.to_s})
    from #{@entity_class.table_name}
    where (#{find_options[:conditions] || '1=1'})
    or #{@entity_class.connection.quote_column_name( attribute.to_s )} = #{@entity_class.connection.quote( attribute_value )}
    order by lower(#{attribute.to_s})
  EOF
end

#query_order_frequency(attribute, attribute_value, find_options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/clevic/sequel_ar_adapter.rb', line 69

def query_order_frequency( attribute, attribute_value, find_options )
  <<-EOF
    select distinct #{attribute.to_s}, count(#{attribute.to_s})
    from #{entity_class.table_name}
    where (#{find_options[:conditions] || '1=1'})
    or #{@entity_class.connection.quote_column_name( attribute.to_s )} = #{@entity_class.connection.quote( attribute_value )}
    group by #{attribute.to_s}
    order by count(#{attribute.to_s}) desc
  EOF
end

#quoted_falseObject



42
43
44
# File 'lib/clevic/sequel_ar_adapter.rb', line 42

def quoted_false
  @entity_class.connection.quoted_false
end

#quoted_trueObject



46
47
48
# File 'lib/clevic/sequel_ar_adapter.rb', line 46

def quoted_true
  @entity_class.connection.quoted_true
end