module FlatMap::ModelMapper::Persistence::ClassMethods

ModelMethods class macros

Public Instance Methods

default_target_class_name() click to toggle source

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

@return [String]

# 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) click to toggle source

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.

@param [#to_i] id of the record @param [*Symbol] traits @return [FlatMap::Mapper] mapper

# 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() click to toggle source

Fetch a class for the target of the mapper.

@return [Class] class

# File lib/flat_map/model_mapper/persistence.rb, line 33
def target_class
  (target_class_name || default_target_class_name).constantize
end