Class: Algebrick::Serializers2::AbstractConverter

Inherits:
Object
  • Object
show all
Includes:
TypeCheck
Defined in:
lib/algebrick/serializers2/abstract.rb

Direct Known Subclasses

FromJson, ToJson

Constant Summary

CannotConvert =
Algebrick.atom

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?, error

Class Method Details

+ (Object) converts(from, to)



20
21
22
23
# File 'lib/algebrick/serializers2/abstract.rb', line 20

def self.converts(from, to)
  @from = from
  @to   = to
end

+ (Object) from



25
26
27
28
# File 'lib/algebrick/serializers2/abstract.rb', line 25

def self.from
  @from || (superclass.from if superclass < AbstractConverter) or
      raise('from not defined')
end

+ (Object) to



30
31
32
33
# File 'lib/algebrick/serializers2/abstract.rb', line 30

def self.to
  @to || (superclass.to if superclass < AbstractConverter) or
      raise('to not defined')
end

Instance Method Details

- (Object) call(data, from, options = {})

returns data, format



48
49
50
51
52
53
54
# File 'lib/algebrick/serializers2/abstract.rb', line 48

def call(data, from, options = {})
  if from == self.from
    converted_data = catch(CannotConvert) { convert data, options }
    return converted_data, to if CannotConvert != converted_data
  end
  return data, from
end

- (Object) cannot_convert (private)



60
61
62
# File 'lib/algebrick/serializers2/abstract.rb', line 60

def cannot_convert
  throw CannotConvert, CannotConvert
end

- (Object) convert(data, options = {})

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/algebrick/serializers2/abstract.rb', line 43

def convert(data, options = {})
  raise NotImplementedError
end

- (Object) from



35
36
37
# File 'lib/algebrick/serializers2/abstract.rb', line 35

def from
  self.class.from
end

- (Object) to



39
40
41
# File 'lib/algebrick/serializers2/abstract.rb', line 39

def to
  self.class.to
end