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, name, resource_file) ⇒ CodePage

Returns a new instance of CodePage.



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

def initialize(id, name, resource_file)
  @id = id
  @name = name
  @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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
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



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

def code_page_mapping
  @code_page_mapping ||= unicode_mapping.invert
end

#from_utf8(str) ⇒ Object



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

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

#to_utf8(str) ⇒ Object



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

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

#unicode_mappingObject



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

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