Class: Wurfl::Handset

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/wurfl/handset.rb

Overview

A class that represents a handset based on information taken from the WURFL.

Defined Under Namespace

Classes: NullHandset

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wurfl_id, useragent, fallback = nil) ⇒ Handset

Constructor Parameters: wurfl_id: is the WURFL ID of the handset useragent: is the user agent of the handset fallback: is the fallback handset that this handset

uses for missing details.


20
21
22
23
24
25
26
# File 'lib/wurfl/handset.rb', line 20

def initialize (wurfl_id, useragent, fallback = nil) 
  # A hash to hold keys and values specific to this handset
  @capabilityhash = Hash::new 
  @wurfl_id = wurfl_id
  @user_agent = useragent
  @fallback = fallback || NullHandset.instance
end

Instance Attribute Details

#user_agentObject

Returns the value of attribute user_agent.



12
13
14
# File 'lib/wurfl/handset.rb', line 12

def user_agent
  @user_agent
end

#wurfl_idObject

Returns the value of attribute wurfl_id.



12
13
14
# File 'lib/wurfl/handset.rb', line 12

def wurfl_id
  @wurfl_id
end

Instance Method Details

#==(other) ⇒ Object

A method to do a simple equality check against two handsets. Parameter: other: Is the another WurflHandset to check against. Returns: true if the two handsets are equal in all values. false if they are not exactly equal in values, id and user agent. Note: for a more detailed comparison, use the compare method.



82
83
84
85
86
87
# File 'lib/wurfl/handset.rb', line 82

def ==(other)
  other.instance_of?(Wurfl::Handset) && 
    self.wurfl_id == other.wurfl_id && 
    self.user_agent == other.user_agent &&
    other.keys.all? {|key| other[key] == self[key] }
end

#[](key) ⇒ Object

Hash accessor Parameters: key: the WURFL key whose value is desired Returns: The value of the key, nil if the handset does not have the key.



37
38
39
# File 'lib/wurfl/handset.rb', line 37

def [] (key)
  @capabilityhash.key?(key) ? @capabilityhash[key] : @fallback[key]
end

#[]=(key, val) ⇒ Object

Setter, A method to set a key and value of the handset.



54
55
56
# File 'lib/wurfl/handset.rb', line 54

def []= (key,val)
  @capabilityhash[key] = val
end

#differences(other) ⇒ Object



89
90
91
92
# File 'lib/wurfl/handset.rb', line 89

def differences(other)
  keys = (self.keys | other.keys)
  keys.find_all {|k| self[k] != other[k]}
end

#eachObject

A Method to iterate over all of the keys and values that the handset has. Note: this will abstract the hash iterator to handle all the lower level calls for the fallback values.



61
62
63
64
65
66
67
68
# File 'lib/wurfl/handset.rb', line 61

def each
  self.keys.each do |key|
    # here is the magic that gives us the key and value of the handset
    # all the way up to the fallbacks end.  
    # Call the pass block with the key and value passed
    yield key, self[key]
  end
end

#fallback=(v) ⇒ Object



28
29
30
# File 'lib/wurfl/handset.rb', line 28

def fallback=(v)
  @fallback = v || NullHandset.instance
end

#get_value_and_owner(key) ⇒ Object

like the above accessor, but also to know who the value comes from Returns: the value and the id of the handset from which the value was obtained



45
46
47
48
49
50
51
# File 'lib/wurfl/handset.rb', line 45

def get_value_and_owner(key)
  if @capabilityhash.key?(key)
    [ @capabilityhash[key], @wurfl_id ]
  else
    @fallback.get_value_and_owner(key)
  end
end

#keysObject

A method to get all of the keys that the handset has.



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

def keys
  @capabilityhash.keys | @fallback.keys
end