view · edit · sidebar · attach · print · history

ODBA - How would you do a search?

<< InitialSetup | Index | ConditionIndex >>

 #!/usr/bin/env ruby

 require 'odba'
 require 'odba/index_definition'

 class User
  attr_accessor :first_name, :last_name
  include ODBA::Persistable
  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
  end
  def to_s
    "#@first_name #@last_name"
  end
 end

 index_def = YAML.load <<-EOD
 --- !ruby/object:ODBA::IndexDefinition
 index_name: 'users_by_last_name'
 origin_klass: 'User'
 target_klass: 'User'
 resolve_search_term: 'last_name.downcase.split(/\s+/)'
 resolve_target: ''
 resolve_origin: ''
 EOD

 ODBA.storage.dbi = DBI.connect('DBI:pg:test', 'test', '')
 ODBA.cache.setup
 ODBA.cache.drop_index('users_by_last_name')
 ODBA.cache.create_index(index_def, Object)

 composer = User.new('Ludwig', 'Van Beethoven')
 composer.odba_store
 painter = User.new('Vincent', 'Van Gogh')
 painter.odba_store
 scientist = User.new('Albert', 'Einstein')
 scientist.odba_store

 puts ODBA.cache.retrieve_from_index('users_by_last_name', 'van')

 # -> Ludwig Van Beethoven
 #    Vincent Van Gogh


 puts ODBA.cache.retrieve_from_index('users_by_last_name', 'beethoven')

 # -> Ludwig Van Beethoven
view · edit · sidebar · attach · print · history
Page last modified on March 14, 2011, at 05:30 PM