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.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#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, name, resource_file) ⇒ CodePage
constructor
A new instance of CodePage.
- #to_utf8(str) ⇒ Object
- #unicode_mapping ⇒ Object
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
#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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/code-pages/code_page.rb', line 5 def name @name 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
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_mapping ⇒ Object
27 28 29 |
# File 'lib/code-pages/code_page.rb', line 27 def unicode_mapping @unicode_mapping ||= YAML.load_file(resource_file) end |