module FlatMap::OpenMapper::Mounting

This module hosts definitions required for mounting functionality of the mappers. This includes mounting definition methods, overloaded read and write methods to make them aware of mounted mappers and other methods.

Also, the method_missing method is defined here to delegate the missing method to the very first mounted mapper that responds to it.

Attributes

save_order[RW]

Public Instance Methods

after_save_mountings() click to toggle source

Return list of mappings to be saved after target of self was saved

@return [Array<FlatMap::OpenMapper>]

# File lib/flat_map/open_mapper/mounting.rb, line 79
def after_save_mountings
  nearest_mountings.reject{ |mount| mount.save_order == :before }
end
before_save_mountings() click to toggle source

Return list of mappings to be saved before saving target of self

@return [Array<FlatMap::OpenMapper>]

# File lib/flat_map/open_mapper/mounting.rb, line 72
def before_save_mountings
  nearest_mountings.select{ |mount| mount.save_order == :before }
end
method_missing(name, *args, &block) click to toggle source

Delegate missing method to any mounted mapping that respond to it, unless those methods are protected methods of FlatMap::OpenMapper.

NOTE: :to_ary method is called internally by Ruby 1.9.3 when we call something like [mapper].flatten. And we DO want default behavior for handling this missing method.

Calls superclass method
# File lib/flat_map/open_mapper/mounting.rb, line 157
def method_missing(name, *args, &block)
  return super if name == :to_ary ||
                          self.class.protected_instance_methods.include?(name)

  return self[name] if mapping(name).present?

  mounting = all_mountings.find{ |mount| mount.respond_to?(name) }
  return super if mounting.nil?
  mounting.send(name, *args, &block)
end
mounting(mounting_name, is_deep = true) click to toggle source

Return a mapping with the name that corresponds to passed mounting_name, if it exists.

@return [FlatMap::Mapping, nil]

# File lib/flat_map/open_mapper/mounting.rb, line 104
def mounting(mounting_name, is_deep = true)
  list = is_deep ? all_mountings : mountings
  list.find{ |mount| mount.name == mounting_name }
end
mountings() click to toggle source

Return a list of all mountings (mapper objects) associated with self.

Overridden in {Traits}. Left here for consistency.

@return [Array<FlatMap::OpenMapper>]

# File lib/flat_map/open_mapper/mounting.rb, line 96
def mountings
  @mountings ||= self.class.mountings.map{ |factory| factory.create(self) }
end
nearest_mountings() click to toggle source

Return all mountings that are mouted on self directly or through traits.

@return [Array<FlatMap::OpenMapper>]

# File lib/flat_map/open_mapper/mounting.rb, line 87
def nearest_mountings
  mountings.map{ |mount| mount.owned? ? mount.nearest_mountings : mount }.flatten
end
read() click to toggle source

Extend original {Mapping#read} method to take into account mountings of mounted mappers.

@return [Hash] read values

Calls superclass method
# File lib/flat_map/open_mapper/mounting.rb, line 46
def read
  mountings.inject(super) do |result, mapper|
    result.merge(mapper.read)
  end
end
write(params) click to toggle source

Extend original {Mapping#write} method to pass params to mounted mappers.

Overridden in {Persistence}. Left here for consistency.

@param [Hash] params @return [Hash] params

Calls superclass method
# File lib/flat_map/open_mapper/mounting.rb, line 59
def write(params)
  super

  mountings.each do |mapper|
    mapper.write(params)
  end

  params
end

Protected Instance Methods

all_mappings() click to toggle source

Return a list of all mappings, i.e. mappings associated to self, plus mappings of all deeply mounted mappers. If self is the owner, that means it is a part (a trait) of a host mapper. That means that all mappings of it actually correspond to all mappings of a host mapper. This allows to define things like validation in a trait where access to top-level mappings is required.

@return [Array<FlatMap::Mapping>]

# File lib/flat_map/open_mapper/mounting.rb, line 136
def all_mappings
  return all_nested_mappings unless owned?
  owner.all_mappings
end
all_mountings() click to toggle source

Return a list of all mounted mappers. If self is a trait, return a list of all mountings of the owner. This will allow separate traits to share methods via method_missing pattern.

@return [Array<FlatMap::OpenMapper>] mounted mappers (including traits)

# File lib/flat_map/open_mapper/mounting.rb, line 114
def all_mountings
  return all_nested_mountings.unshift(self) unless owned?
  owner.all_mountings
end
all_nested_mappings() click to toggle source

Return a list of all mappings, i.e. mappings that associated to self plus mappings of all deeply mounted mappers.

@return [Array<FlatMap::Mapping>]

# File lib/flat_map/open_mapper/mounting.rb, line 146
def all_nested_mappings
  (mappings + mountings.map{ |mount| mount.send(:all_nested_mappings) }).flatten
end
all_nested_mountings() click to toggle source

Return a list of mountings that are accessible by a named mapper.

@return [Array<FlatMap::OpenMapper>]

# File lib/flat_map/open_mapper/mounting.rb, line 123
def all_nested_mountings
  mountings.dup.concat(mountings.map{ |mount| mount.send(:all_nested_mountings) }).flatten
end