Class: Dnsruby::CodeMapper

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/Dnsruby/code_mapper.rb

Overview

CodeMapper superclass looks after String to code mappings (e.g. OpCode, RCode, etc.)

Subclasses simply define a mapping of codes to variable names, and CodeMapper provides utility methods.

All strings will come out as upper case

Example :

Types::AAAA or Types.AAAA
rcode.string or rcode.code

Constant Summary collapse

@@strings =
{}
@@stringsdown =
{}
@@values =
{}
@@maxcode =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ CodeMapper

:nodoc: all



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/Dnsruby/code_mapper.rb', line 84

def initialize(arg) #:nodoc: all
  if (arg.kind_of?String)
    if (@@stringsdown[self.class][arg.downcase] != nil)
      set_string(arg)
    else unknown_string(arg)
    end
  elsif (arg.kind_of?Fixnum)
    if (@@values[self.class][arg] != nil)
      set_code(arg)
    else unknown_code(arg)
    end
  elsif (arg.kind_of?self.class)
    set_code(arg.code)
  else
    raise ArgumentError.new("Unknown argument #{arg} for #{self.class}")
  end
end

Instance Attribute Details

#codeObject Also known as: to_code

Returns the value of attribute code.



34
35
36
# File 'lib/Dnsruby/code_mapper.rb', line 34

def code
  @code
end

#stringObject Also known as: to_string, to_s

Returns the value of attribute string.



34
35
36
# File 'lib/Dnsruby/code_mapper.rb', line 34

def string
  @string
end

Class Method Details

.add_pair(string, code) ⇒ Object

Add new a code to the CodeMapper



64
65
66
67
68
69
# File 'lib/Dnsruby/code_mapper.rb', line 64

def CodeMapper.add_pair(string, code)
  @@strings[self].store(string, code)
  @@values[self]=@@strings[self].invert
  @@stringsdown[self].store(string.downcase, code)
  @@maxcode[self]+=1
end

.maxcodeObject



39
40
41
# File 'lib/Dnsruby/code_mapper.rb', line 39

def CodeMapper.maxcode
  return @maxcode
end

.method_missing(methId) ⇒ Object

:nodoc: all



79
80
81
82
# File 'lib/Dnsruby/code_mapper.rb', line 79

def self.method_missing(methId) #:nodoc: all
  str = methId.id2name
  return self.new(str)
end

.regexpObject

Return a regular expression which matches any codes or strings from the CodeMapper.



158
159
160
161
# File 'lib/Dnsruby/code_mapper.rb', line 158

def self.regexp
  # Longest ones go first, so the regex engine will match AAAA before A, etc.
  return @@strings[self].keys.sort { |a, b| b.length <=> a.length }.join('|')
end

.to_code(arg) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/Dnsruby/code_mapper.rb', line 124

def CodeMapper.to_code(arg)
  if (arg.kind_of?Fixnum)
    return arg
  else
    return @@stringsdown[self][arg.downcase]
  end
end

.to_string(arg) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/Dnsruby/code_mapper.rb', line 116

def CodeMapper.to_string(arg)
  if (arg.kind_of?String) 
    return arg
  else
    return @@values[self][arg]
  end
end

.updateObject

Creates the CodeMapper from the defined constants



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/Dnsruby/code_mapper.rb', line 44

def CodeMapper.update
  
  @@strings[self] = {}
  @@stringsdown[self] = {}
  @@values[self] = {}
  @@maxcode[self] = 0
  
  constants = self.constants - CodeMapper.constants
  constants.each do |i|
    @@strings[self].store(i, const_get(i))
  end 
  @@maxcode[self] = constants.length
  @@values[self] = @@strings[self].invert
  @@stringsdown[self] = Hash.new
  @@strings[self].keys.each do |s|
    @@stringsdown[self].store(s.downcase, @@strings[self][s])    
  end
end

Instance Method Details

#<=>(other) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/Dnsruby/code_mapper.rb', line 132

def <=>(other)
  if (other.class == Fixnum)
    self.code <=> other
  else
    self.code <=> other.code
  end
end

#==(other) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/Dnsruby/code_mapper.rb', line 140

def ==(other)
  if other.kind_of?CodeMapper
    if other.string == @string && other.code == @code
      return true 
    end
  elsif other.kind_of?String
    if other == @string
      return true
    end
  elsif other.kind_of?Fixnum
    if other == @code
      return true
    end
  end
  return false
end

#inspectObject



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

def inspect
  return @string
end

#set_code(arg) ⇒ Object



102
103
104
105
# File 'lib/Dnsruby/code_mapper.rb', line 102

def set_code(arg)
  @code = arg
  @string = @@values[self.class][@code]
end

#set_string(arg) ⇒ Object



107
108
109
110
# File 'lib/Dnsruby/code_mapper.rb', line 107

def set_string(arg)
  @code = @@stringsdown[self.class][arg.downcase]
  @string = @@strings[self.class].invert[@code]
end

#unknown_code(arg) ⇒ Object

:nodoc: all

Raises:

  • (ArgumentError)


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

def unknown_code(arg) #:nodoc: all
  raise ArgumentError.new("Code #{arg} not a member of #{self.class}")
end

#unknown_string(arg) ⇒ Object

:nodoc: all

Raises:

  • (ArgumentError)


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

def unknown_string(arg) #:nodoc: all
  raise ArgumentError.new("String #{arg} not a member of #{self.class}")
end