Module: TanningBed

Defined in:
lib/tanning_bed.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =

:stopdoc:

'0.0.11'
LIBPATH =
::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
PATH =
::File.dirname(LIBPATH) + ::File::SEPARATOR

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



44
45
46
47
48
# File 'lib/tanning_bed.rb', line 44

def self.included(base)
  @@conn = nil
  @@on_solr_exception = nil
  base.extend(ClassMethods)
end

.libpath(*args) ⇒ Object

Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.



174
175
176
# File 'lib/tanning_bed.rb', line 174

def self.libpath( *args )
  args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
end

.on_solr_exceptionObject



67
68
69
# File 'lib/tanning_bed.rb', line 67

def self.on_solr_exception
  @@on_solr_exception
end

.on_solr_exception=(value) ⇒ Object



71
72
73
# File 'lib/tanning_bed.rb', line 71

def self.on_solr_exception=(value)
  @@on_solr_exception = value
end

.path(*args) ⇒ Object

Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join.



182
183
184
# File 'lib/tanning_bed.rb', line 182

def self.path( *args )
  args.empty? ? PATH : ::File.join(PATH, *args)
end

.require_all_libs_relative_to(fname, dir = nil) ⇒ Object

Utility method used to rquire all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.



191
192
193
194
195
196
197
# File 'lib/tanning_bed.rb', line 191

def self.require_all_libs_relative_to( fname, dir = nil )
  dir ||= ::File.basename(fname, '.*')
  search_me = ::File.expand_path(
      ::File.join(::File.dirname(fname), dir, '**', '*.rb'))

  Dir.glob(search_me).sort.each {|rb| require rb}
end

.solr_connection(url = 'http://localhost:8983/solr', autocommit = :on, reset = false) ⇒ Object

connect to the solr instance



51
52
53
54
55
56
57
# File 'lib/tanning_bed.rb', line 51

def self.solr_connection(url='http://localhost:8983/solr', autocommit=:on, reset=false)
  if reset
    @@conn = Solr::Connection.new(url, :autocommit => autocommit) 
  else
    @@conn ||= Solr::Connection.new(url, :autocommit => autocommit)
  end
end

.solr_exception(e) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/tanning_bed.rb', line 59

def self.solr_exception(e)
  if TanningBed.on_solr_exception
    TanningBed.on_solr_exception.call(e)
  else
    $stderr.puts("SOLR - " + e.to_s)
  end
end

.solr_load(results) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/tanning_bed.rb', line 98

def self.solr_load(results)
  key_set = results.collect do |result|
    key = result["search_id"].first.split(" ")
    Kernel.const_get(key[0]).send(:get, key[1])
  end
  return key_set
end

.solr_search(query_string, options = {}) ⇒ Object



92
93
94
95
96
# File 'lib/tanning_bed.rb', line 92

def self.solr_search(query_string, options={})
  TanningBed.solr_connection.query(query_string, options)
rescue Errno::ECONNREFUSED => e
  TanningBed.solr_exception(e)
end

.versionObject

Returns the version string for the library.



166
167
168
# File 'lib/tanning_bed.rb', line 166

def self.version
  VERSION
end

Instance Method Details

#lookup_key_type(key, klass) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/tanning_bed.rb', line 131

def lookup_key_type(key, klass)
  # is the key already in the correct_format?
  key_postfix = key.split("_").last
  return nil if ["i", "facet", "t", "f", "d", "mv"].include?(key_postfix)
  
  # Add the helper to the key string
  case klass.to_s
  when "Fixnum"
    "_i"
  when "String"
    if key.size < 255
      "_facet"
    else
      "_t"
    end
  when "Float"
    "_f"
  when "Date", "Datetime", "Time"
    "_d"
  when "Array"
    "_s_mv"
  else
    "_t"
  end
end

#search_fieldsObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tanning_bed.rb', line 116

def search_fields
  @fields = {}
  self.solr_keys.each do |key|
    if self.respond_to?(key)
      value = self.send(key)
      key_type = lookup_key_type(key, value.class)        
      @fields["#{key}#{key_type}"] = value
    end
  end
  @fields[:search_id] = solr_id
  @fields[:type_t] = self.class.to_s
  @fields[:id_t] = self.id
  return @fields
end

#solr_addObject

add a document to the index



80
81
82
83
84
# File 'lib/tanning_bed.rb', line 80

def solr_add
  TanningBed.solr_connection.add(search_fields)
rescue Errno::ECONNREFUSED => e
  TanningBed.solr_exception(e)
end

#solr_deleteObject



106
107
108
109
110
# File 'lib/tanning_bed.rb', line 106

def solr_delete
  TanningBed.solr_connection.delete(solr_id)
rescue Errno::ECONNREFUSED => e
  TanningBed.solr_exception(e)
end

#solr_idObject



75
76
77
# File 'lib/tanning_bed.rb', line 75

def solr_id
  "#{self.class} #{self.id}"
end

#solr_keysObject



112
113
114
# File 'lib/tanning_bed.rb', line 112

def solr_keys
  raise "You must define the method solr_keys in the class you want to use for Solr.\n  This should return an array of method names to call on you're class for indexing\n eg: ['id', 'name', 'description']"
end

#solr_updateObject



86
87
88
89
90
# File 'lib/tanning_bed.rb', line 86

def solr_update
  TanningBed.solr_connection.update(search_fields)
rescue Errno::ECONNREFUSED => e
  TanningBed.solr_exception(e)
end