Class: Perpetuity::MapperRegistry
- Inherits:
-
Object
- Object
- Perpetuity::MapperRegistry
- Includes:
- Enumerable
- Defined in:
- lib/perpetuity/mapper_registry.rb
Instance Method Summary collapse
- #[](klass) ⇒ Object
- #[]=(klass, mapper) ⇒ Object
- #check_rails_app_for_mappers ⇒ Object
- #each(&block) ⇒ Object
- #has_mapper?(klass) ⇒ Boolean
-
#initialize ⇒ MapperRegistry
constructor
A new instance of MapperRegistry.
- #load_mappers ⇒ Object
- #mapper_class(klass) ⇒ Object
- #mapper_for(klass, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ MapperRegistry
Returns a new instance of MapperRegistry.
5 6 7 |
# File 'lib/perpetuity/mapper_registry.rb', line 5 def initialize @mappers = Hash.new end |
Instance Method Details
#[](klass) ⇒ Object
13 14 15 |
# File 'lib/perpetuity/mapper_registry.rb', line 13 def [] klass mapper_class(klass).new(self) end |
#[]=(klass, mapper) ⇒ Object
32 33 34 |
# File 'lib/perpetuity/mapper_registry.rb', line 32 def []= klass, mapper @mappers[klass] = mapper end |
#check_rails_app_for_mappers ⇒ Object
44 45 46 |
# File 'lib/perpetuity/mapper_registry.rb', line 44 def check_rails_app_for_mappers Dir['app/**/*_mapper.rb'] end |
#each(&block) ⇒ Object
36 37 38 |
# File 'lib/perpetuity/mapper_registry.rb', line 36 def each &block @mappers.each(&block) end |
#has_mapper?(klass) ⇒ Boolean
9 10 11 |
# File 'lib/perpetuity/mapper_registry.rb', line 9 def has_mapper? klass @mappers.has_key? klass end |
#load_mappers ⇒ Object
40 41 42 |
# File 'lib/perpetuity/mapper_registry.rb', line 40 def load_mappers check_rails_app_for_mappers.each(&method(:load)) end |
#mapper_class(klass) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/perpetuity/mapper_registry.rb', line 22 def mapper_class klass @mappers.fetch(klass) do load_mappers unless @mappers.has_key? klass raise KeyError, "No mapper for #{klass}" end @mappers[klass] end end |
#mapper_for(klass, options = {}) ⇒ Object
17 18 19 20 |
# File 'lib/perpetuity/mapper_registry.rb', line 17 def mapper_for klass, ={} identity_map = .fetch(:identity_map) { IdentityMap.new } mapper_class(klass).new(self, identity_map) end |