Module: FlatMap::OpenMapper::Mounting

Extended by:
ActiveSupport::Concern
Included in:
FlatMap::OpenMapper
Defined in:
lib/flat_map/open_mapper/mounting.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

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.



157
158
159
160
161
162
163
164
165
166
# 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

Instance Method Details

#after_save_mountingsArray<FlatMap::OpenMapper>

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

Returns:



79
80
81
# 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_mountingsArray<FlatMap::OpenMapper>

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

Returns:



72
73
74
# File 'lib/flat_map/open_mapper/mounting.rb', line 72

def before_save_mountings
  nearest_mountings.select{ |mount| mount.save_order == :before }
end

#mounting(mounting_name, is_deep = true) ⇒ FlatMap::Mapping?

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

Returns:



104
105
106
107
# 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

#mountingsArray<FlatMap::OpenMapper>

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

Overridden in Traits. Left here for consistency.

Returns:



96
97
98
# File 'lib/flat_map/open_mapper/mounting.rb', line 96

def mountings
  @mountings ||= self.class.mountings.map{ |factory| factory.create(self) }
end

#nearest_mountingsArray<FlatMap::OpenMapper>

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

Returns:



87
88
89
# File 'lib/flat_map/open_mapper/mounting.rb', line 87

def nearest_mountings
  mountings.map{ |mount| mount.owned? ? mount.nearest_mountings : mount }.flatten
end

#readHash

Extend original FlatMap::OpenMapper::Mapping#read method to take into account mountings of mounted mappers.

Returns:

  • (Hash)

    read values



46
47
48
49
50
# 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) ⇒ Hash

Extend original FlatMap::OpenMapper::Mapping#write method to pass params to mounted mappers.

Overridden in Persistence. Left here for consistency.

Parameters:

  • params (Hash)

Returns:

  • (Hash)

    params



59
60
61
62
63
64
65
66
67
# File 'lib/flat_map/open_mapper/mounting.rb', line 59

def write(params)
  super

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

  params
end