Class: CodePages::CodePage

Inherits:
Object
  • Object
show all
Defined in:
lib/code-pages/code_page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, resource_file) ⇒ CodePage

Returns a new instance of CodePage.



7
8
9
10
# File 'lib/code-pages/code_page.rb', line 7

def initialize(id, resource_file)
  @id = id
  @resource_file = resource_file
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/code-pages/code_page.rb', line 5

def id
  @id
end

#resource_fileObject (readonly)

Returns the value of attribute resource_file.



5
6
7
# File 'lib/code-pages/code_page.rb', line 5

def resource_file
  @resource_file
end

Instance Method Details

#code_page_mappingObject



30
31
32
# File 'lib/code-pages/code_page.rb', line 30

def code_page_mapping
  @code_page_mapping ||= unicode_mapping.invert
end

#from_utf8(str) ⇒ Object



19
20
21
22
23
24
# File 'lib/code-pages/code_page.rb', line 19

def from_utf8(str)
  str
    .unpack('U*')
    .map { |code_point| code_page_mapping[code_point] }
    .pack('C*')
end

#to_utf8(str) ⇒ Object



12
13
14
15
16
17
# File 'lib/code-pages/code_page.rb', line 12

def to_utf8(str)
  str
    .each_byte
    .map { |byte| unicode_mapping[byte] }
    .pack('U*')
end

#unicode_mappingObject



26
27
28
# File 'lib/code-pages/code_page.rb', line 26

def unicode_mapping
  @unicode_mapping ||= YAML.load_file(resource_file)
end