Class: Dnsruby::CodeMapper
- Inherits:
-
Object
- Object
- Dnsruby::CodeMapper
- 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
Direct Known Subclasses
Algorithms, Classes, ExtendedRCode, Message::SecurityLevel, MetaTypes, Modes, OpCode, QTypes, RCode, RR::CERT::CertificateTypes, RR::DS::DigestTypes, Types
Constant Summary collapse
- @@strings =
{}
- @@stringsdown =
{}
- @@values =
{}
- @@maxcode =
{}
Instance Attribute Summary collapse
-
#code ⇒ Object
(also: #to_code, #to_i)
Returns the value of attribute code.
-
#string ⇒ Object
(also: #to_string, #to_s)
Returns the value of attribute string.
Class Method Summary collapse
-
.add_pair(string, code) ⇒ Object
Add new a code to the CodeMapper.
- .maxcode ⇒ Object
-
.method_missing(methId) ⇒ Object
:nodoc: all.
-
.regexp ⇒ Object
Return a regular expression which matches any codes or strings from the CodeMapper.
- .to_code(arg) ⇒ Object
- .to_string(arg) ⇒ Object
-
.update ⇒ Object
Creates the CodeMapper from the defined constants.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
-
#initialize(arg) ⇒ CodeMapper
constructor
:nodoc: all.
- #inspect ⇒ Object
- #set_code(arg) ⇒ Object
- #set_string(arg) ⇒ Object
-
#unknown_code(arg) ⇒ Object
:nodoc: all.
-
#unknown_string(arg) ⇒ Object
:nodoc: all.
Constructor Details
#initialize(arg) ⇒ CodeMapper
:nodoc: all
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/Dnsruby/code_mapper.rb', line 88 def initialize(arg) #:nodoc: all if (arg.kind_of?String) arg.gsub!("_", "-") 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
#code ⇒ Object Also known as: to_code, to_i
Returns the value of attribute code.
34 35 36 |
# File 'lib/Dnsruby/code_mapper.rb', line 34 def code @code end |
#string ⇒ Object 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
65 66 67 68 69 70 |
# File 'lib/Dnsruby/code_mapper.rb', line 65 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 |
.maxcode ⇒ Object
40 41 42 |
# File 'lib/Dnsruby/code_mapper.rb', line 40 def CodeMapper.maxcode return @maxcode end |
.method_missing(methId) ⇒ Object
:nodoc: all
83 84 85 86 |
# File 'lib/Dnsruby/code_mapper.rb', line 83 def self.method_missing(methId) #:nodoc: all str = methId.id2name return self.new(str) end |
.regexp ⇒ Object
Return a regular expression which matches any codes or strings from the CodeMapper.
166 167 168 169 |
# File 'lib/Dnsruby/code_mapper.rb', line 166 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
131 132 133 134 135 136 137 |
# File 'lib/Dnsruby/code_mapper.rb', line 131 def CodeMapper.to_code(arg) if (arg.kind_of?Fixnum) return arg else return @@stringsdown[self][arg.downcase] end end |
.to_string(arg) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/Dnsruby/code_mapper.rb', line 123 def CodeMapper.to_string(arg) if (arg.kind_of?String) return arg else return @@values[self][arg] end end |
.update ⇒ Object
Creates the CodeMapper from the defined constants
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/Dnsruby/code_mapper.rb', line 45 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.to_s, 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
139 140 141 142 143 144 145 |
# File 'lib/Dnsruby/code_mapper.rb', line 139 def <=>(other) if (other.class == Fixnum) self.code <=> other else self.code <=> other.code end end |
#==(other) ⇒ Object Also known as: eql?
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/Dnsruby/code_mapper.rb', line 147 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 |
#inspect ⇒ Object
119 120 121 |
# File 'lib/Dnsruby/code_mapper.rb', line 119 def inspect return @string end |
#set_code(arg) ⇒ Object
109 110 111 112 |
# File 'lib/Dnsruby/code_mapper.rb', line 109 def set_code(arg) @code = arg @string = @@values[self.class][@code] end |
#set_string(arg) ⇒ Object
114 115 116 117 |
# File 'lib/Dnsruby/code_mapper.rb', line 114 def set_string(arg) @code = @@stringsdown[self.class][arg.downcase] @string = @@strings[self.class].invert[@code] end |
#unknown_code(arg) ⇒ Object
:nodoc: all
76 77 78 79 80 81 |
# File 'lib/Dnsruby/code_mapper.rb', line 76 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
72 73 74 |
# File 'lib/Dnsruby/code_mapper.rb', line 72 def unknown_string(arg) #:nodoc: all raise ArgumentError.new("String #{arg} not a member of #{self.class}") end |