Module: FlatMap::OpenMapper::Traits
- Extended by:
- ActiveSupport::Concern
- Included in:
- FlatMap::OpenMapper
- Defined in:
- lib/flat_map/open_mapper/traits.rb
Overview
This small module allows mappers to define traits, which technically means mounting anonymous mappers, attached to host one.
Also, FlatMap::OpenMapper::Mounting#mountings completely overridden here to support special trait behavior.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#extension ⇒ FlatMap::OpenMapper
Return :extension trait, if present.
-
#extension? ⇒ Boolean
Return
trueifselfis extension of host mapper. -
#mountings ⇒ Array<FlatMap::OpenMapper>
Override the original Mounting#mountings method to filter out those traited mappers that are not required for trait setup of
self. -
#self_mountings ⇒ Array<FlatMap::OpenMapper>
Return a list of all mountings that represent full picture of
self, i.e. -
#trait(trait_name) ⇒ FlatMap::OpenMapper?
Try to find trait mapper with name that corresponds to
trait_nameUsed internally to manipulate such mappers (for example, skip some traits) in some scenarios.
Instance Method Details
#extension ⇒ FlatMap::OpenMapper
Return :extension trait, if present
64 65 66 |
# File 'lib/flat_map/open_mapper/traits.rb', line 64 def extension trait(:extension) end |
#extension? ⇒ Boolean
Return true if self is extension of host mapper.
91 92 93 |
# File 'lib/flat_map/open_mapper/traits.rb', line 91 def extension? owned? && self.class.name =~ /ExtensionTrait$/ end |
#mountings ⇒ Array<FlatMap::OpenMapper>
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.
33 34 35 36 37 38 39 40 41 |
# File 'lib/flat_map/open_mapper/traits.rb', line 33 def mountings @mountings ||= begin mountings = self.class.mountings.reject do |factory| factory.traited? && !factory.required_for_any_trait?(traits) end mountings.concat(singleton_class.mountings) mountings.map{ |factory| factory.create(self, *traits) } end end |
#self_mountings ⇒ Array<FlatMap::OpenMapper>
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
47 48 49 |
# File 'lib/flat_map/open_mapper/traits.rb', line 47 def self_mountings mountings.select(&:owned?).map{ |mount| mount.self_mountings }.flatten.concat [self] end |
#trait(trait_name) ⇒ FlatMap::OpenMapper?
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.
57 58 59 |
# File 'lib/flat_map/open_mapper/traits.rb', line 57 def trait(trait_name) self_mountings.find{ |mount| mount.class.name.underscore =~ /#{trait_name}_trait$/ } end |