| @@ 10-63 (lines=54) @@ | ||
| 7 | ||
| 8 | module AMA |
|
| 9 | module Entity |
|
| 10 | class Mapper |
|
| 11 | class Type |
|
| 12 | module BuiltIn |
|
| 13 | # DateTime type description |
|
| 14 | class DateTimeType < Type |
|
| 15 | def initialize |
|
| 16 | super(DateTime) |
|
| 17 | ||
| 18 | normalizer_block do |entity, *| |
|
| 19 | entity.iso8601(3) |
|
| 20 | end |
|
| 21 | ||
| 22 | define_denormalizer |
|
| 23 | define_factory |
|
| 24 | ||
| 25 | enumerator_block do |*| |
|
| 26 | ::Enumerator.new { |*| } |
|
| 27 | end |
|
| 28 | ||
| 29 | injector_block { |*| } |
|
| 30 | end |
|
| 31 | ||
| 32 | private |
|
| 33 | ||
| 34 | def define_denormalizer |
|
| 35 | denormalizer_block do |input, _, ctx| |
|
| 36 | break input if input.is_a?(DateTime) |
|
| 37 | input = input.to_s if input.is_a?(Symbol) |
|
| 38 | break DateTime.iso8601(input, 3) if input.is_a?(String) |
|
| 39 | if input.is_a?(Integer) |
|
| 40 | break DateTime.strptime(input.to_s, '%s') |
|
| 41 | end |
|
| 42 | singleton_class.send(:include, Mixin::Errors) |
|
| 43 | message = 'String input expected (like ' \ |
|
| 44 | "'2001-02-03T04:05:06.123+04:00'), " \ |
|
| 45 | "#{input.class} received: #{input}" |
|
| 46 | mapping_error(message, context: ctx) |
|
| 47 | end |
|
| 48 | end |
|
| 49 | ||
| 50 | def define_factory |
|
| 51 | factory_block do |_, _, ctx| |
|
| 52 | singleton_class.send(:include, Mixin::Errors) |
|
| 53 | message = 'DateTime type could not be instantiated directly, ' \ |
|
| 54 | 'it only supports normalization and denormalization' |
|
| 55 | compliance_error(message, context: ctx) |
|
| 56 | end |
|
| 57 | end |
|
| 58 | ||
| 59 | INSTANCE = new |
|
| 60 | end |
|
| 61 | end |
|
| 62 | end |
|
| 63 | end |
|
| 64 | end |
|
| 65 | end |
|
| 66 | ||
| @@ 8-57 (lines=50) @@ | ||
| 5 | ||
| 6 | module AMA |
|
| 7 | module Entity |
|
| 8 | class Mapper |
|
| 9 | class Type |
|
| 10 | module BuiltIn |
|
| 11 | # Rational type description |
|
| 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 |
|
| 56 | end |
|
| 57 | end |
|
| 58 | end |
|
| 59 | end |
|
| 60 | ||