Class: NRSER::Char::AlphaNumericSub

Inherits:
Object
  • Object
show all
Defined in:
lib/nrser/char/alpha_numeric_sub.rb

Overview

Lil’ structure with useful info and methods for special characters.

Instance Attribute Summary collapse

On-Demand Built-In Instances (Class Methods) collapse

Instance Method Summary collapse

Constructor Details

#initialize(sub_a: nil, sub_A: nil, sub_0: nil, exceptions: nil) ⇒ AlphaNumericSub

Instantiate a new ‘AlphaNumericSub`.

Parameters:

  • sub_a (String) (defaults to: nil)

    Character the lower-case ‘a` ASCII character gets replaced with.

  • sub_A (String) (defaults to: nil)

    Character the upper-case ‘A` ASCII character gets replaced with.

  • sub_0 (String) (defaults to: nil)

    Character the ‘0` gets subbed out for.

  • exceptions (Hash?) (defaults to: nil)

    I don’t know just read the source.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 158

def initialize  sub_a: nil,
                sub_A: nil,
                sub_0: nil,
                exceptions: nil
  binding.locals.tap do |args|
    t.list( t.utf8_char? ).check \
      args.slice( :sub_a, :sub_A, :sub_0 ).values
    
    t.hash_?( keys: t.utf8_char, values: t.utf8_char ).check exceptions
    
    if args.all?( &:nil? )
      raise ArgumentError.new,
        "All arguments can't be `nil` (sub couldn't do anything)"
    end
  end
  
  @sub_a = sub_a.freeze
  @sub_A = sub_A.freeze
  @sub_0 = sub_0.freeze
  @exceptions = if exceptions
    exceptions.each { |k, v| k.freeze; v.freeze }.freeze
  end
end

Instance Attribute Details

#exceptionsHash<String, String> (readonly)

Map of source character to dest character substitutions to handle cases where a few of the destination alpha-numerics don’t follow the “offest from ‘a`/`A`/`0`” rule.

Returns:



68
69
70
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 68

def exceptions
  @exceptions
end

#sub_0String? (readonly)

The target set’s ‘0` character (as a length 1 String), the `#ord` of which is used to determine the target `#ord` of numeric `0-9` characters for substitution.

Returns:

  • (String)

    When the sub supports numeric substitution.

  • (nil)

    When the sub does not support numeric substitution.



58
59
60
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 58

def sub_0
  @sub_0
end

#sub_AString? (readonly)

The ‘A` character (as a length 1 String), the `#ord` of which is used to determine the target `#ord` upper-case alpha (`A-Z`) characters for substitution.

Returns:

  • (String)

    When the sub supports upper-case alpha substitution.

  • (nil)

    When the sub does not support upper-case alpha substitution.



45
46
47
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 45

def sub_A
  @sub_A
end

#sub_aString? (readonly)

The ‘a` character (as a length 1 String), the `#ord` of which is used to determine the target `#ord` of lower-case alpha (`a-z`) characters for substitution.

Returns:

  • (String)

    When the sub supports lower-case alpha substitution.

  • (nil)

    When the sub does not support lower-case alpha substitution.



32
33
34
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 32

def sub_a
  @sub_a
end

Class Method Details

.unicode_math_boldNRSER::Char::AlphaNumericSub

Instance to substitute alphas with their “Unicode Math Bold” counterpart.



103
104
105
106
107
108
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 103

def self.unicode_math_bold
  @@unicode_math_italic ||= new \
   sub_a: '𝐚',
   sub_A: '𝐀',
   sub_0: '𝟬'
end

.unicode_math_bold_italicNRSER::Char::AlphaNumericSub

Instance to substitute alphas with their “Unicode Math Bold Italic” counterpart.



116
117
118
119
120
121
122
123
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 116

def self.unicode_math_bold_italic
  @@unicode_math_italic ||= new \
   sub_A: '𝑨',
   sub_a: '𝒂',
   # Just use the bold numbers since Unicode doesn't seem to have
   # italic numbers
   sub_0: '𝟬'
end

.unicode_math_italicNRSER::Char::AlphaNumericSub

Instance to substitute alphas with their “Unicode Math Italic” counterpart.



86
87
88
89
90
91
92
93
94
95
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 86

def self.unicode_math_italic
  @@unicode_math_italic ||= new \
   sub_a: '𝑎',
   sub_A: '𝐴',
   # Doesn't have italic numbers to just don't sub
   exceptions: {
     # The `h` is not in the run
     'h' => ''
   }
end

.unicode_math_monospaceNRSER::Char::AlphaNumericSub

Instance to substitute alphas with their “Unicode Math Bold Italic” counterpart.



131
132
133
134
135
136
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 131

def self.unicode_math_monospace
  @@unicode_math_italic ||= new \
   sub_A: '𝙰',
   sub_a: '𝚊',
   sub_0: '𝟶'
end

Instance Method Details

#demoObject



218
219
220
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 218

def demo
  sub ['a'..'z', 'A'..'Z', '0'..'9'].map { |_| _.to_a.join }.join
end

#sub(src) ⇒ Object

Instance Methods



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 186

def sub src
  dest = src.dup
  
  if @exceptions
    @exceptions.each do |src_char, dest_char|
      dest.gsub! src_char, dest_char
    end
  end
  
  [
    ['a', /[a-z]/],
    ['A', /[A-Z]/],
    ['0', /[0-9]/],
  ].each do |src_char, regexp|
    src_char_ord = src_char.ord
    start_dest_char = instance_variable_get "@sub_#{ src_char }"
    
    unless start_dest_char.nil?
      start_dest_char_ord = start_dest_char.ord
      
      dest.gsub!( regexp ) { |char|
        NRSER::Char.from_i(
          start_dest_char_ord + (char.ord - src_char_ord)
        )
      }
    end
  end
  
  dest
end