| Total Complexity | 7 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # frozen_String_literal: true |
||
| 10 | class Registry |
||
| 11 | attr_accessor :types |
||
| 12 | |||
| 13 | def initialize |
||
| 14 | @types = {} |
||
| 15 | end |
||
| 16 | |||
| 17 | # @param [AMA::Entity::Mapper::Type::Concrete] type |
||
| 18 | def register(type) |
||
| 19 | @types[type.type] = type |
||
| 20 | end |
||
| 21 | |||
| 22 | # @param [Class] klass |
||
| 23 | def registered?(klass) |
||
| 24 | @types.key?(klass) |
||
| 25 | end |
||
| 26 | |||
| 27 | def for(klass) |
||
| 28 | find_class_types(klass) | find_module_types(klass) |
||
| 29 | end |
||
| 30 | |||
| 31 | private |
||
| 32 | |||
| 33 | def inheritance_chain(klass) |
||
| 34 | cursor = klass |
||
| 35 | chain = [] |
||
| 36 | loop do |
||
| 37 | chain.push(cursor) |
||
| 38 | cursor = cursor.superclass |
||
| 39 | break if cursor.nil? |
||
| 40 | end |
||
| 41 | chain |
||
| 42 | end |
||
| 43 | |||
| 44 | def find_class_types(klass) |
||
| 45 | inheritance_chain(klass).each_with_object([]) do |entry, carrier| |
||
| 46 | carrier.push(types[entry]) if types[entry] |
||
| 47 | end |
||
| 48 | end |
||
| 49 | |||
| 50 | def find_module_types(klass) |
||
| 51 | chain = inheritance_chain(klass).reverse |
||
| 52 | result = chain.reduce([]) do |carrier, entry| |
||
| 53 | ancestor_types = entry.ancestors.map do |candidate| |
||
| 54 | types[candidate] |
||
| 55 | end |
||
| 56 | carrier | ancestor_types.reject(&:nil?) |
||
| 57 | end |
||
| 58 | result.reverse |
||
| 59 | end |
||
| 60 | end |
||
| 61 | end |
||
| 65 |