Module: Kasket::ConfigurationMixin

Defined in:
lib/kasket/configuration_mixin.rb

Instance Method Summary collapse

Instance Method Details

#has_kasket(options = {}) ⇒ Object



65
66
67
# File 'lib/kasket/configuration_mixin.rb', line 65

def has_kasket(options = {})
  has_kasket_on :id
end

#has_kasket_index_on?(sorted_attributes) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/kasket/configuration_mixin.rb', line 61

def has_kasket_index_on?(sorted_attributes)
  kasket_indices.include?(sorted_attributes)
end

#has_kasket_on(*args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kasket/configuration_mixin.rb', line 69

def has_kasket_on(*args)
  attributes = args.sort! { |x, y| x.to_s <=> y.to_s }
  if attributes != [:id] && !kasket_indices.include?([:id])
    has_kasket_on(:id)
  end

  @kasket_indices ||= []
  @kasket_indices << attributes unless @kasket_indices.include?(attributes)

  include WriteMixin unless include?(WriteMixin)
  extend DirtyMixin unless respond_to?(:kasket_dirty_methods)
  extend ReadMixin unless methods.map(&:to_sym).include?(:find_by_sql_with_kasket)
end

#kasket_activerecord_versionObject



29
30
31
# File 'lib/kasket/configuration_mixin.rb', line 29

def kasket_activerecord_version
  "R#{ActiveRecord::VERSION::MAJOR}#{ActiveRecord::VERSION::MINOR}"
end

#kasket_indicesObject



55
56
57
58
59
# File 'lib/kasket/configuration_mixin.rb', line 55

def kasket_indices
  result = @kasket_indices || []
  result += superclass.kasket_indices unless self == ActiveRecord::Base
  result.uniq
end

#kasket_key_for(attribute_value_pairs) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kasket/configuration_mixin.rb', line 33

def kasket_key_for(attribute_value_pairs)
  if attribute_value_pairs.size == 1 && attribute_value_pairs[0][0] == :id && attribute_value_pairs[0][1].is_a?(Array)
    attribute_value_pairs[0][1].map {|id| kasket_key_for_id(id)}
  else
    key = attribute_value_pairs.map do |attribute, value|
      column = columns_hash[attribute.to_s]
      value = nil if value.blank?
      attribute.to_s + '=' + connection.quote(column.type_cast(value), column).downcase
    end.join('/')

    if key.size > (250 - kasket_key_prefix.size) || key =~ /\s/
      key = Digest::MD5.hexdigest(key)
    end

    kasket_key_prefix + key
  end
end

#kasket_key_for_id(id) ⇒ Object



51
52
53
# File 'lib/kasket/configuration_mixin.rb', line 51

def kasket_key_for_id(id)
  kasket_key_for([['id', id]])
end

#kasket_key_prefixObject



25
26
27
# File 'lib/kasket/configuration_mixin.rb', line 25

def kasket_key_prefix
  @kasket_key_prefix ||= "kasket-#{Kasket::Version::PROTOCOL}/#{kasket_activerecord_version}/#{table_name}/version=#{column_names.join.sum}/"
end

#kasket_parserObject



21
22
23
# File 'lib/kasket/configuration_mixin.rb', line 21

def kasket_parser
  @kasket_parser ||= QueryParser.new(self)
end

#use_kasket?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/kasket/configuration_mixin.rb', line 17

def use_kasket?
  !Thread.current['kasket_disabled']
end

#without_kasket(&block) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/kasket/configuration_mixin.rb', line 9

def without_kasket(&block)
  old_value = Thread.current['kasket_disabled'] || false
  Thread.current['kasket_disabled'] = true
  yield
ensure
  Thread.current['kasket_disabled'] = old_value
end