Module: FlatMap::ModelMapper::Persistence::ClassMethods
- Defined in:
- lib/flat_map/model_mapper/persistence.rb
Overview
ModelMethods class macros
Instance Method Summary collapse
-
#default_target_class_name ⇒ String
Return target class name based on name of the ancestor mapper that is closest to FlatMap::Mapper, which may be
self
. -
#find(id, *traits, &block) ⇒ FlatMap::Mapper
Find a record of the
target_class
byid
and use it as a target for a new mapper, with a list of passedtraits
applied to it. -
#target_class ⇒ Class
Fetch a class for the target of the mapper.
Instance Method Details
#default_target_class_name ⇒ String
Return target class name based on name of the ancestor mapper that is closest to FlatMap::Mapper, which may be self
.
class VehicleMapper
# some definitions
end
class CarMapper < VehicleMapper
# some more definitions
end
CarMapper.target_class # => Vehicle
51 52 53 54 55 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 51 def default_target_class_name ancestor_classes = ancestors.select{ |ancestor| ancestor.is_a? Class } base_mapper_index = ancestor_classes.index(::FlatMap::ModelMapper) ancestor_classes[base_mapper_index - 1].name[/^([\w:]+)Mapper.*$/, 1] end |
#find(id, *traits, &block) ⇒ FlatMap::Mapper
Find a record of the target_class
by id
and use it as a target for a new mapper, with a list of passed traits
applied to it.
26 27 28 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 26 def find(id, *traits, &block) new(target_class.find(id), *traits, &block) end |
#target_class ⇒ Class
Fetch a class for the target of the mapper.
33 34 35 |
# File 'lib/flat_map/model_mapper/persistence.rb', line 33 def target_class (target_class_name || default_target_class_name).constantize end |