Class: Wurfl::Handset
- Inherits:
-
Object
- Object
- Wurfl::Handset
- 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
-
#fallback ⇒ Object
Returns the value of attribute fallback.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
-
#wurfl_id ⇒ Object
Returns the value of attribute wurfl_id.
Instance Method Summary collapse
-
#==(other) ⇒ Object
A method to do a simple equality check against two handsets.
-
#[](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.
-
#[]=(key, val) ⇒ Object
Setter, A method to set a key and value of the handset.
- #differences(other) ⇒ Object
-
#each ⇒ Object
A Method to iterate over all of the keys and values that the handset has.
-
#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.
-
#initialize(wurfl_id, useragent, fallback = nil) ⇒ Handset
constructor
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.
-
#keys ⇒ Object
A method to get all of the keys that the handset has.
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.
21 22 23 24 25 26 27 |
# File 'lib/wurfl/handset.rb', line 21 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
#fallback ⇒ Object
Returns the value of attribute fallback.
13 14 15 |
# File 'lib/wurfl/handset.rb', line 13 def fallback @fallback end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
12 13 14 |
# File 'lib/wurfl/handset.rb', line 12 def user_agent @user_agent end |
#wurfl_id ⇒ Object
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.
83 84 85 86 87 88 |
# File 'lib/wurfl/handset.rb', line 83 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.
38 39 40 |
# File 'lib/wurfl/handset.rb', line 38 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.
55 56 57 |
# File 'lib/wurfl/handset.rb', line 55 def []= (key,val) @capabilityhash[key] = val end |
#differences(other) ⇒ Object
90 91 92 93 |
# File 'lib/wurfl/handset.rb', line 90 def differences(other) keys = (self.keys | other.keys) keys.find_all {|k| self[k] != other[k]} end |
#each ⇒ Object
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.
62 63 64 65 66 67 68 69 |
# File 'lib/wurfl/handset.rb', line 62 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 |
#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
46 47 48 49 50 51 52 |
# File 'lib/wurfl/handset.rb', line 46 def get_value_and_owner(key) if @capabilityhash.key?(key) [ @capabilityhash[key], @wurfl_id ] else @fallback.get_value_and_owner(key) end end |
#keys ⇒ Object
A method to get all of the keys that the handset has.
72 73 74 |
# File 'lib/wurfl/handset.rb', line 72 def keys @capabilityhash.keys | @fallback.keys end |