Class: Dnsruby::CodeMapper
- Inherits:
-
Object
- Object
- Dnsruby::CodeMapper
- Includes:
- Comparable
- Defined in:
- lib/Dnsruby/code_mapper.rb
Overview
Direct Known Subclasses
Algorithms, Classes, ExtendedRCode, Message::SecurityLevel, MetaTypes, Modes, Nsec3HashAlgorithms, OpCode, QTypes, RCode, RR::CERT::CertificateTypes, RR::DS::DigestTypes, RR::SSHFP::Algorithms, RR::SSHFP::FpTypes, Types
Defined Under Namespace
Classes: Arrays
Constant Summary collapse
- @@arrays =
{}
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.
- .strings ⇒ Object
- .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?)
- #hash ⇒ Object
-
#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
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
#code ⇒ Object 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 |
#string ⇒ Object 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 |
.maxcode ⇒ Object
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 |
.regexp ⇒ Object
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 |
.strings ⇒ Object
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 |
.update ⇒ Object
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 |
#hash ⇒ Object
136 137 138 |
# File 'lib/Dnsruby/code_mapper.rb', line 136 def hash @code end |
#inspect ⇒ Object
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
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 |