Class: Pocolog::Upgrade::Ops::EnumCast
- Defined in:
- lib/pocolog/upgrade/ops/enum_cast.rb
Instance Attribute Summary collapse
-
#known_symbols ⇒ Object
readonly
Returns the value of attribute known_symbols.
Attributes inherited from Base
Instance Method Summary collapse
- #convert(value) ⇒ Object
-
#initialize(from_t, to_t) ⇒ EnumCast
constructor
A new instance of EnumCast.
Methods inherited from Base
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_symbols ⇒ Object (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 |