Class: FuzzyMatcher::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/fuzzy_matcher/adapter.rb

Constant Summary collapse

AVAILABLE_DBS =
['pg', 'mysql']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_type, db_name, db_user, db_password, table_name = 'library') ⇒ Adapter

Returns a new instance of Adapter.



10
11
12
13
14
# File 'lib/fuzzy_matcher/adapter.rb', line 10

def initialize(db_type, db_name, db_user, db_password, table_name = 'library')
  @type = db_type
  @table_name = table_name
  @connection = make_connection(db_name, db_user, db_password)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/fuzzy_matcher/adapter.rb', line 6

def connection
  @connection
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



6
7
8
# File 'lib/fuzzy_matcher/adapter.rb', line 6

def table_name
  @table_name
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/fuzzy_matcher/adapter.rb', line 6

def type
  @type
end

Instance Method Details

#build_fqa(level_values, values, distance_function) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/fuzzy_matcher/adapter.rb', line 48

def build_fqa(level_values, values, distance_function)
  level_values.each_with_index do |lv, id|
    values.each do |v|
      dist = calculate_distance(distance_function, lv, v)

      column = "u#{id}"
      update(v, column, dist)
    end
  end
end

#calculate_distance(distance_function, level_value, value) ⇒ Object



59
60
61
62
63
# File 'lib/fuzzy_matcher/adapter.rb', line 59

def calculate_distance(distance_function, level_value, value)
  query_string = "select #{distance_function}('#{level_value}','#{value}')"
  result = parse(send_query(query_string), false, distance_function)
  result.is_a?(Array) ? result[0] : result
end

#create_index_table(height) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/fuzzy_matcher/adapter.rb', line 25

def create_index_table(height)
  case @type
  when 'pg'
    create_table_pg(height)
  when 'mysql'
    create_table_mysql(height)
  end
  fill_index_table
end

#parse(values, known_key = true, value = 'value') ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/fuzzy_matcher/adapter.rb', line 39

def parse(values, known_key = true, value = 'value')
  case @type
  when 'pg'
    pg_parse_values(values, value)
  when 'mysql'
    mysql_parse_values(values, known_key)
  end
end

#select_all(columns) ⇒ Object



35
36
37
# File 'lib/fuzzy_matcher/adapter.rb', line 35

def select_all(columns)
  send_query "select #{columns.to_s} from #{@table_name}_indexed"
end

#send_find_query(conditions) ⇒ Object



20
21
22
23
# File 'lib/fuzzy_matcher/adapter.rb', line 20

def send_find_query(conditions)
  query_string = "select * from #{@table_name}_indexed where #{conditions}"
  parse(send_query "#{query_string}")
end

#send_query(query) ⇒ Object



16
17
18
# File 'lib/fuzzy_matcher/adapter.rb', line 16

def send_query(query)
  connection.send(query_method, query)
end