Class: CodePages::CodePage
- Inherits:
-
Object
- Object
- CodePages::CodePage
- Defined in:
- lib/code-pages/code_page.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#resource_file ⇒ Object
readonly
Returns the value of attribute resource_file.
Instance Method Summary collapse
- #code_page_mapping ⇒ Object
- #from_utf8(str) ⇒ Object
-
#initialize(id, resource_file) ⇒ CodePage
constructor
A new instance of CodePage.
- #to_utf8(str) ⇒ Object
- #unicode_mapping ⇒ Object
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
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/code-pages/code_page.rb', line 5 def id @id end |
#resource_file ⇒ Object (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_mapping ⇒ Object
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_mapping ⇒ Object
26 27 28 |
# File 'lib/code-pages/code_page.rb', line 26 def unicode_mapping @unicode_mapping ||= YAML.load_file(resource_file) end |