module FlatMap::OpenMapper::Traits

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.

Public Instance Methods

extension() click to toggle source

Return :extension trait, if present

@return [FlatMap::OpenMapper]

# File lib/flat_map/open_mapper/traits.rb, line 64
def extension
  trait(:extension)
end
extension?() click to toggle source

Return true if self is extension of host mapper.

@return [Boolean]

# File lib/flat_map/open_mapper/traits.rb, line 91
def extension?
  owned? && self.class.name =~ /ExtensionTrait$/
end
mountings() click to toggle source

Override the original {FlatMap::OpenMapper::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.

@return [Array<FlatMap::OpenMapper>]

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

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

@return [Array<FlatMap::OpenMapper>]

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

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.

@param [Symbol] trait_name @return [FlatMap::OpenMapper, nil]

# 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

Protected Instance Methods

mapper_mountings() click to toggle source

Return only mountings that correspond to external mappers.

@return [Array<FlatMap::OpenMapper>]

# File lib/flat_map/open_mapper/traits.rb, line 83
def mapper_mountings
  mountings.select{ |mount| !mount.owned? }
end
trait_mountings() click to toggle source

Return only mountings that are actually traits for host mapper.

@return [Array<FlatMap::OpenMapper>]

# File lib/flat_map/open_mapper/traits.rb, line 71
def trait_mountings
  result = mountings.select{ |mount| mount.owned? }
  # mapper extension has more priority then traits, and
  # has to be processed first.
  result.unshift(result.pop) if result.length > 1 && result[-1].extension?
  result
end