Class: Net::DND::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dnd/profile.rb

Overview

Container class for a single DND Profile. Takes the current fields set in the Session, along with the returned items from a lookup command and builds a ‘profile’ Hash. The class then provides dynamic accessor methods for each of the fields, as well as a [] accessor method for accessing fields whose name is a Ruby reserved word, i.e. class.

Instance Method Summary collapse

Constructor Details

#initialize(fields, items) ⇒ Profile

The profile constructor method. Takes 2 array arguments: fields and items. From these it creates a Hash object that is the internal represenation of the profile.



18
19
20
21
22
23
24
# File 'lib/net/dnd/profile.rb', line 18

def initialize(fields, items)
  @profile = Hash[*fields.zip(items).flatten]

  if @profile.has_key?(:expires)
    self.extend Net::DND::Expires
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id) ⇒ Object (private)

Handles all dynamic accessor methods for the Profile instance. This is based on the field accessor methods from Rails ActiveRecord. Fields can be directly accessed on the Profile object, either for purposes of returning their value or, if the field name is requested with a ‘?’ on the end, a true/false is returned based on the existence of the named field in Profile instance.



56
57
58
59
60
# File 'lib/net/dnd/profile.rb', line 56

def method_missing(method_id)
  attrib_name = method_id.to_s
  return @profile.has_key?(attrib_name.chop.to_sym) if attrib_name[-1, 1] == '?'
  return_field(method_id)
end

Instance Method Details

#[](field) ⇒ Object

Generic Hash-style accessor method. Provides access to fields in the profile hash when the name of the field is a reserved word in Ruby. Allows for field names supplied as either Strings or Symbols.



44
45
46
# File 'lib/net/dnd/profile.rb', line 44

def [](field)
  return_field(field)
end

#inspectObject

Inspection string for instances of the class



28
29
30
31
# File 'lib/net/dnd/profile.rb', line 28

def inspect
  attrib_inspect = @profile.inject("") { |s, pair| k,v = pair; s << "#{k}=\"#{v}\", " }
  "<#{self.class} length=#{length}, #{attrib_inspect.rstrip.chomp(',')}>"
end

#lengthObject

Length of the class instance, basically the number of fields/items. Only really used by the inspect method.



36
37
38
# File 'lib/net/dnd/profile.rb', line 36

def length
  @profile.length
end