Class: Encoding

Inherits:
Object show all
Defined in:
opal/opal/corelib/string/encoding.rb

Defined Under Namespace

Classes: CompatibilityError, EncodingError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, names, ascii, dummy) ⇒ Encoding

Returns a new instance of Encoding.



31
32
33
34
35
36
# File 'opal/opal/corelib/string/encoding.rb', line 31

def initialize(name, names, ascii, dummy)
  @name  = name
  @names = names
  @ascii = ascii
  @dummy = dummy
end

Instance Attribute Details

#default_externalObject

Returns the value of attribute default_external.



27
28
29
# File 'opal/opal/corelib/string/encoding.rb', line 27

def default_external
  @default_external
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'opal/opal/corelib/string/encoding.rb', line 29

def name
  @name
end

#namesObject (readonly)

Returns the value of attribute names.



29
30
31
# File 'opal/opal/corelib/string/encoding.rb', line 29

def names
  @names
end

Class Method Details

.find(name) ⇒ Object

Raises:



19
20
21
22
23
24
25
# File 'opal/opal/corelib/string/encoding.rb', line 19

def self.find(name)
  return default_external if name == :default_external
  register = `Opal.encodings`
  encoding = register.JS[name] || register.JS[name.upcase]
  raise ArgumentError, "unknown encoding name - #{name}" unless encoding
  encoding
end

.register(name, options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'opal/opal/corelib/string/encoding.rb', line 4

def self.register(name, options = {}, &block)
  names = [name] + (options[:aliases] || [])
  ascii = options[:ascii] || false
  dummy = options[:dummy] || false

  encoding = new(name, names, ascii, dummy)
  encoding.instance_eval(&block)

  register = `Opal.encodings`
  names.each do |encoding_name|
    const_set encoding_name.sub('-', '_'), encoding
    register.JS[encoding_name] = encoding
  end
end

Instance Method Details

#ascii_compatible?Boolean

Returns:



38
39
40
# File 'opal/opal/corelib/string/encoding.rb', line 38

def ascii_compatible?
  @ascii
end

#bytesizeObject



59
60
61
# File 'opal/opal/corelib/string/encoding.rb', line 59

def bytesize(*)
  raise NotImplementedError
end

#dummy?Boolean

Returns:



42
43
44
# File 'opal/opal/corelib/string/encoding.rb', line 42

def dummy?
  @dummy
end

#each_byteObject

methods to implement per encoding



55
56
57
# File 'opal/opal/corelib/string/encoding.rb', line 55

def each_byte(*)
  raise NotImplementedError
end

#inspectObject



50
51
52
# File 'opal/opal/corelib/string/encoding.rb', line 50

def inspect
  "#<Encoding:#{@name}#{' (dummy)' if @dummy}>"
end

#to_sObject



46
47
48
# File 'opal/opal/corelib/string/encoding.rb', line 46

def to_s
  @name
end