Class: Mobiles::DeviceLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/mobiles/device_locator.rb

Overview

Use this class to retrieve a mobile class for a particular user-agent or based on the device name. This class uses a build in cache mechanism to improve the performance

Constant Summary collapse

@@mobiles_map =
Hash.new
@@devices_cache =
Hash.new

Class Method Summary collapse

Class Method Details

.eval_file(filename) ⇒ Object

used for internal use. Unfortunately we can’t declare it as a private method



27
28
29
30
# File 'lib/mobiles/device_locator.rb', line 27

def self.eval_file filename
  abs_filename = File.dirname(__FILE__)+'/../../repo/'+filename
  eval(File.new(abs_filename).read)
end

.find_device_by_device_id(device_id) ⇒ Object

returns an instance of a device class for the provided device_id parameter



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mobiles/device_locator.rb', line 44

def self.find_device_by_device_id device_id
  device = @@devices_cache.fetch(device_id , nil)
   if device.nil?
         class_name = class_name_for_device_id device_id
      device = eval  <<-command
                      eval_file '#{device_id}.rbx'
                      Mobiles::Repository::#{class_name}.new
                    command
      @@devices_cache.update({device_id => device})
      end
      device
end

.find_device_by_user_agent(user_agent) ⇒ Object

returns an instance of a device class for the provided user-agent parameter



35
36
37
38
39
40
41
# File 'lib/mobiles/device_locator.rb', line 35

def self.find_device_by_user_agent user_agent
  device_id = @@mobiles_map.fetch(user_agent , nil)
    unless device_id.nil?
      find_device_by_device_id device_id
     
    end      
end