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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dnsruby/code_mapper.rb', line 95

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?(Integer)
    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 of type #{arg.class}: #{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



71
72
73
74
75
76
77
# File 'lib/dnsruby/code_mapper.rb', line 71

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

.method_missing(methId) ⇒ Object

:nodoc: all



90
91
92
93
# File 'lib/dnsruby/code_mapper.rb', line 90

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.



174
175
176
177
# File 'lib/dnsruby/code_mapper.rb', line 174

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



148
149
150
151
152
153
154
# File 'lib/dnsruby/code_mapper.rb', line 148

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

.to_string(arg) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/dnsruby/code_mapper.rb', line 140

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dnsruby/code_mapper.rb', line 54

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



156
157
158
159
160
161
162
# File 'lib/dnsruby/code_mapper.rb', line 156

def <=>(other)
  if other.is_a?(Integer)
    self.code <=> other
  else
    self.code <=> other.code
  end
end

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



164
165
166
167
168
169
170
# File 'lib/dnsruby/code_mapper.rb', line 164

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



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

def hash
  @code
end

#inspectObject



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

def inspect
  return @string
end

#set_code(arg) ⇒ Object



127
128
129
130
# File 'lib/dnsruby/code_mapper.rb', line 127

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

#set_string(arg) ⇒ Object



121
122
123
124
125
# File 'lib/dnsruby/code_mapper.rb', line 121

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

#unknown_code(arg) ⇒ Object

:nodoc: all



83
84
85
86
87
88
# File 'lib/dnsruby/code_mapper.rb', line 83

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)


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

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