Class: BetterRecord::Encoder
Instance Method Summary collapse
- #find_encoding ⇒ Object
-
#initialize(str) ⇒ Encoder
constructor
A new instance of Encoder.
- #is_encoding?(encoding_check) ⇒ Boolean
- #is_iso8859? ⇒ Boolean
- #is_utf8? ⇒ Boolean
- #is_windows?(str) ⇒ Boolean
- #to_utf8 ⇒ Object
Constructor Details
#initialize(str) ⇒ Encoder
Returns a new instance of Encoder.
6 7 8 |
# File 'lib/better_record/encoder.rb', line 6 def initialize(str) @str = str end |
Instance Method Details
#find_encoding ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/better_record/encoder.rb', line 16 def find_encoding puts 'utf-8' if is_utf8? return 'utf-8' if is_utf8? puts 'iso-8859-1' if is_iso8859? return 'iso-8859-1' if is_iso8859? puts 'Windows-1252' if is_windows? return 'Windows-1252' if is_windows? raise ArgumentError.new "Invalid Encoding" end |
#is_encoding?(encoding_check) ⇒ Boolean
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/better_record/encoder.rb', line 38 def is_encoding?(encoding_check) case @str.encoding when encoding_check @str.valid_encoding? when Encoding::ASCII_8BIT, Encoding::US_ASCII @str.dup.force_encoding(encoding_check).valid_encoding? else false end end |
#is_iso8859? ⇒ Boolean
30 31 32 |
# File 'lib/better_record/encoder.rb', line 30 def is_iso8859? is_encoding?(Encoding::ISO_8859_1) end |
#is_utf8? ⇒ Boolean
26 27 28 |
# File 'lib/better_record/encoder.rb', line 26 def is_utf8? is_encoding?(Encoding::UTF_8) end |
#is_windows?(str) ⇒ Boolean
34 35 36 |
# File 'lib/better_record/encoder.rb', line 34 def is_windows?(str) is_encoding?(Encoding::Windows_1252) end |
#to_utf8 ⇒ Object
10 11 12 13 14 |
# File 'lib/better_record/encoder.rb', line 10 def to_utf8 return @str if is_utf8? encoding = find_encoding @str.force_encoding(encoding).encode('utf-8', invalid: :replace, undef: :replace) end |