Module: FlatMap::BaseMapper::Traits

Extended by:
ActiveSupport::Concern
Included in:
FlatMap::BaseMapper
Defined in:
lib/flat_map/base_mapper/traits.rb

Overview

This small module allows mappers to define traits, which technically means mounting anonymous mappers, attached to host one.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#extensionFlatMap::BaseMapper

Return :extension trait, if present



63
64
65
# File 'lib/flat_map/base_mapper/traits.rb', line 63

def extension
  trait(:extension)
end

#extension?Boolean

Return true if self is extension of host mapper.



90
91
92
# File 'lib/flat_map/base_mapper/traits.rb', line 90

def extension?
  owned? && self.class.name =~ /ExtensionTrait$/
end

#mountingsArray<FlatMap::BaseMapper>

Override the original Mounting#mountings method to filter out those traited mappers that are not required for trait setup of self. Also, handle any inline extension that may be defined on the mounting mapper, which is attached as a singleton trait.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/flat_map/base_mapper/traits.rb', line 30

def mountings
  @mountings ||= begin
    mountings = self.class.mountings.
                           reject{ |factory|
                             factory.traited? &&
                             !factory.required_for_any_trait?(traits)
                           }
    mountings.concat(singleton_class.mountings)
    mountings.map{ |factory| factory.create(self, *traits) }
  end
end

#self_mountingsArray<FlatMap::BaseMapper>

Return a list of all mountings that represent full picture of self, i.e. self and all traits, including deeply nested, that are mounted on self



46
47
48
# File 'lib/flat_map/base_mapper/traits.rb', line 46

def self_mountings
  mountings.select(&:owned?).map{ |mount| mount.self_mountings }.flatten.concat [self]
end

#trait(trait_name) ⇒ FlatMap::BaseMapper?

Try to find trait mapper with name that corresponds to trait_name Used internally to manipulate such mappers (for example, skip some traits) in some scenarios.



56
57
58
# File 'lib/flat_map/base_mapper/traits.rb', line 56

def trait(trait_name)
  self_mountings.find{ |mount| mount.class.name.underscore =~ /#{trait_name}_trait$/ }
end