Class: Perpetuity::MapperRegistry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/perpetuity/mapper_registry.rb

Instance Method Summary collapse

Constructor Details

#initializeMapperRegistry

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_mappersObject



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

Returns:

  • (Boolean)


9
10
11
# File 'lib/perpetuity/mapper_registry.rb', line 9

def has_mapper? klass
  @mappers.has_key? klass
end

#load_mappersObject



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, options={}
  identity_map = options.fetch(:identity_map) { IdentityMap.new }
  mapper_class(klass).new(self, identity_map)
end