Class: Pocolog::Upgrade::Ops::EnumCast

Inherits:
Base
  • Object
show all
Defined in:
lib/pocolog/upgrade/ops/enum_cast.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#to_type

Instance Method Summary collapse

Methods inherited from Base

#call, #identity?

Constructor Details

#initialize(from_t, to_t) ⇒ EnumCast

Returns a new instance of EnumCast



7
8
9
10
11
12
13
# File 'lib/pocolog/upgrade/ops/enum_cast.rb', line 7

def initialize(from_t, to_t)
    super(to_t)
    @known_symbols = to_t.keys.keys.map(&:to_sym).to_set
    if from_t.keys.none? { |sym, _| known_symbols.include?(sym.to_sym) }
        raise InvalidCast, 'no common symbols between the two types'
    end
end

Instance Attribute Details

#known_symbolsObject (readonly)

Returns the value of attribute known_symbols



5
6
7
# File 'lib/pocolog/upgrade/ops/enum_cast.rb', line 5

def known_symbols
  @known_symbols
end

Instance Method Details

#convert(value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/pocolog/upgrade/ops/enum_cast.rb', line 15

def convert(value)
    symbol = Typelib.to_ruby(value)
    unless known_symbols.include?(symbol)
        raise InvalidCast, "#{symbol} is not present in the target enum"
    end
    Typelib.from_ruby(symbol, to_type)
end