Module: FlatMap::ModelMapper::Persistence::ClassMethods

Defined in:
lib/flat_map/model_mapper/persistence.rb

Overview

ModelMethods class macros

Instance Method Summary collapse

Instance Method Details

#default_target_class_nameString

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

Returns:

  • (String)


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.

Parameters:

  • id (#to_i)

    of the record

  • traits (*Symbol)

Returns:



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_classClass

Fetch a class for the target of the mapper.

Returns:

  • (Class)

    class



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