Module: TokyoCafe::Persistable::ClassMethods

Includes:
TokyoCabinet
Defined in:
lib/tokyocafe/persistable.rb

Instance Method Summary collapse

Instance Method Details

#get_by_id(id, db_uri = self.location) ⇒ Object Also known as: get



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tokyocafe/persistable.rb', line 169

def get_by_id(id, db_uri = self.location)
  @@tdb = TDB::new
  unless @@tdb.open(db_uri, TDB::OREADER)
    ecode = @@tdb.ecode
    STDERR.printf("open error: %s\n", @@tdb.errmsg(ecode))
  end
  if id && @@tdb.get(id)
    h = @@tdb.get(id)
    res = self.new(h)
  else
    res = nil
  end
  @@tdb.close
  return res
end

#search(options = {}, db_uri = self.location) ⇒ Object

:conditions => => ‘tokyo’, :limit => 3, :order => { :name => :ASC }



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/tokyocafe/persistable.rb', line 187

def search(options = {}, db_uri = self.location)
  @@tdb = TDB::new
  unless @@tdb.open(db_uri, TDB::OREADER)
    ecode = @@tdb.ecode
    STDERR.printf("open error: %s\n", @@tdb.errmsg(ecode))
  end
  qry = TDBQRY::new(@@tdb)
  if options.include?(:conditions)
    options[:conditions].each do |k,v|
      qry.addcond(k.to_s, TDBQRY::QCSTREQ, v.to_s)
    end
  end
  if options.include?(:limit) && options[:limit].to_i > 0
    qry.setlimit(options[:limit])
  end
  if options.include?(:order)
    options[:order].each do |k,v|
      qry.setorder(k.to_s, TDBQRY.const_get("QOSTR#{v.to_s.upcase}"))
    end
  end
  res = qry.search
  @@tdb.close
  return res
end

#set_location=(db_uri) ⇒ Object



212
213
214
# File 'lib/tokyocafe/persistable.rb', line 212

def set_location=(db_uri)
  @location = db_uri == "" ? nil : db_uri
end