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

Defined Under Namespace

Classes: Arrays

Constant Summary collapse

@@arrays =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ CodeMapper

:nodoc: all



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/Dnsruby/code_mapper.rb', line 99

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

Instance Attribute Details

#codeObject Also known as: to_code, to_i

Returns the value of attribute code.



31
32
33
# File 'lib/Dnsruby/code_mapper.rb', line 31

def code
  @code
end

#stringObject Also known as: to_string, to_s

Returns the value of attribute string.



31
32
33
# File 'lib/Dnsruby/code_mapper.rb', line 31

def string
  @string
end

Class Method Details

.add_pair(string, code) ⇒ Object

Add new a code to the CodeMapper



75
76
77
78
79
80
81
# File 'lib/Dnsruby/code_mapper.rb', line 75

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

.maxcodeObject



53
54
55
# File 'lib/Dnsruby/code_mapper.rb', line 53

def CodeMapper.maxcode
  return @maxcode
end

.method_missing(methId) ⇒ Object

:nodoc: all



94
95
96
97
# File 'lib/Dnsruby/code_mapper.rb', line 94

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.



178
179
180
181
# File 'lib/Dnsruby/code_mapper.rb', line 178

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

.stringsObject



47
48
49
50
51
# File 'lib/Dnsruby/code_mapper.rb', line 47

def CodeMapper.strings
  strings = []
  @@arrays[self].strings.keys.each {|s| strings.push(s)}
  return strings
end

.to_code(arg) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/Dnsruby/code_mapper.rb', line 152

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

.to_string(arg) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/Dnsruby/code_mapper.rb', line 144

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

.updateObject

Creates the CodeMapper from the defined constants



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/Dnsruby/code_mapper.rb', line 58

def CodeMapper.update

  @@arrays[self] = Arrays.new
  
  constants = self.constants - CodeMapper.constants
  constants.each do |i|
    @@arrays[self].strings.store(i.to_s, const_get(i))
  end 
  @@arrays[self].maxcode = constants.length
  @@arrays[self].values = @@arrays[self].strings.invert
  @@arrays[self].stringsdown = Hash.new
  @@arrays[self].strings.keys.each do |s|
    @@arrays[self].stringsdown.store(s.downcase, @@arrays[self].strings[s])
  end
end

Instance Method Details

#<=>(other) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/Dnsruby/code_mapper.rb', line 160

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

#==(other) ⇒ Object Also known as: eql?



168
169
170
171
172
173
174
# File 'lib/Dnsruby/code_mapper.rb', line 168

def ==(other)
  return true if [@code, @string].include?other
  if (CodeMapper === other)
    return true if ((other.code == @code) || (other.string == @string))
  end
  return false
end

#hashObject



136
137
138
# File 'lib/Dnsruby/code_mapper.rb', line 136

def hash
  @code
end

#inspectObject



140
141
142
# File 'lib/Dnsruby/code_mapper.rb', line 140

def inspect
  return @string
end

#set_code(arg) ⇒ Object



131
132
133
134
# File 'lib/Dnsruby/code_mapper.rb', line 131

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

#set_string(arg) ⇒ Object



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

def set_string(arg)
  array = @@arrays[self.class]
  @code = array.stringsdown[arg.downcase]
  @string = array.values[@code]
end

#unknown_code(arg) ⇒ Object

:nodoc: all



87
88
89
90
91
92
# File 'lib/Dnsruby/code_mapper.rb', line 87

def unknown_code(arg) #:nodoc: all
  # Be liberal in what you accept...
  #      raise ArgumentError.new("Code #{arg} not a member of #{self.class}")
  Classes.add_pair(arg.to_s, arg)
  set_code(arg)
end

#unknown_string(arg) ⇒ Object

:nodoc: all

Raises:

  • (ArgumentError)


83
84
85
# File 'lib/Dnsruby/code_mapper.rb', line 83

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