| Total Complexity | 3 |
| Total Lines | 43 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | # frozen_string_literal: true |
||
| 12 | class RationalType < Type |
||
| 13 | def initialize |
||
| 14 | super(Rational) |
||
| 15 | |||
| 16 | normalizer_block do |entity, *| |
||
| 17 | entity.to_s |
||
| 18 | end |
||
| 19 | |||
| 20 | define_denormalizer |
||
| 21 | define_factory |
||
| 22 | |||
| 23 | enumerator_block do |*| |
||
| 24 | ::Enumerator.new { |*| } |
||
| 25 | end |
||
| 26 | |||
| 27 | injector_block { |*| } |
||
| 28 | end |
||
| 29 | |||
| 30 | private |
||
| 31 | |||
| 32 | def define_denormalizer |
||
| 33 | denormalizer_block do |input, _, ctx| |
||
| 34 | break input if input.is_a?(Rational) |
||
| 35 | input = input.to_s if input.is_a?(Symbol) |
||
| 36 | break Rational(input) if input.is_a?(String) |
||
| 37 | singleton_class.send(:include, Mixin::Errors) |
||
| 38 | message = "String input expected (like '2.3'), " \ |
||
| 39 | "#{input.class} received: #{input}" |
||
| 40 | mapping_error(message, context: ctx) |
||
| 41 | end |
||
| 42 | end |
||
| 43 | |||
| 44 | def define_factory |
||
| 45 | factory_block do |_, _, ctx| |
||
| 46 | singleton_class.send(:include, Mixin::Errors) |
||
| 47 | message = 'Rational type could not be instantiated directly, ' \ |
||
| 48 | 'it only supports normalization and denormalization' |
||
| 49 | compliance_error(message, context: ctx) |
||
| 50 | end |
||
| 51 | end |
||
| 52 | |||
| 53 | INSTANCE = new |
||
| 54 | end |
||
| 55 | end |
||
| 60 |