Module: FlatMap::Mapper::Targeting::ClassMethods

Defined in:
lib/flat_map/mapper/targeting.rb

Overview

ModelMethods class macros

Instance Method Summary collapse

Instance Method Details

#build(*traits, &block) ⇒ FlatMap::Mapper

Create a new mapper object wrapped around new instance of its target_class, with a list of passed traits applied to it.

Parameters:

  • traits (*Symbol)

Returns:



37
38
39
# File 'lib/flat_map/mapper/targeting.rb', line 37

def build(*traits, &block)
  new(target_class.new, *traits, &block)
end

#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)


73
74
75
76
77
# File 'lib/flat_map/mapper/targeting.rb', line 73

def default_target_class_name
  ancestor_classes = ancestors.select{ |ancestor| ancestor.is_a? Class }
  base_mapper_index = ancestor_classes.index(::FlatMap::Mapper)
  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:



48
49
50
# File 'lib/flat_map/mapper/targeting.rb', line 48

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



55
56
57
# File 'lib/flat_map/mapper/targeting.rb', line 55

def target_class
  (target_class_name || default_target_class_name).constantize
end