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.

[View source]

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:

[View source]

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:

[View source]

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:

[View source]

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:

[View source]

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:

[View source]

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

[View source]

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

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Look for methods that might be dynamically defined and define them for lookup.

Returns:

  • (Boolean)
[View source]

169
170
171
172
173
174
175
176
177
178
179
# File 'lib/flat_map/open_mapper/mounting.rb', line 169

def respond_to_missing?(name, include_private = false)
  # Added magically by Ruby 1.9.3
  if name == :to_ary || name == :empty?
    false
  else
    return true if mapping(name).present?

    mounting = all_mountings.find{ |mount| mount.respond_to?(name) }
    return false if mounting.nil?
  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

[View source]

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