Class: FlatMap::OpenMapper

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Includes:
ActiveModel::Validations, AttributeMethods, Mapping, Mounting, Persistence, Skipping, Traits
Defined in:
lib/flat_map/open_mapper.rb

Overview

Base Mapper that can be used for mounting other mappers, handling business logic, etc. For the intentional usage of mappers, pleas see ModelMapper

Direct Known Subclasses

ModelMapper

Defined Under Namespace

Modules: AttributeMethods, Mapping, Mounting, Persistence, Skipping, Traits Classes: Factory, NoTargetError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Skipping

#save, #shallow_save, #skip!, #skipped?, #use!, #valid?, #write

Methods included from Persistence

#apply, #errors, #persisted?, #save, #save_target, #shallow_save, #valid?, #write

Methods included from AttributeMethods

#method_missing

Methods included from Traits

#extension, #extension?, #mountings, #self_mountings, #trait

Methods included from Mounting

#after_save_mountings, #before_save_mountings, #method_missing, #mounting, #mountings, #nearest_mountings, #read, #write

Methods included from Mapping

#[], #[]=, #mapping, #read, #write

Constructor Details

#initialize(target, *traits) ⇒ OpenMapper

Initializes mapper with target and traits, which are used to fetch proper list of mounted mappers. Raises error if target is not specified.

Parameters:

  • target (Object)

    Target of mapping

  • traits (*Symbol)

    List of traits

Raises:

  • (FlatMap::Mapper::NoTargetError)


56
57
58
59
60
61
62
63
64
# File 'lib/flat_map/open_mapper.rb', line 56

def initialize(target, *traits)
  raise NoTargetError.new(self.class) unless target.present?

  @target, @traits = target, traits

  if block_given?
    singleton_class.trait :extension, &Proc.new
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FlatMap::OpenMapper::AttributeMethods

Instance Attribute Details

#hostFlatMap::Mapper

If mapper was mounted by another mapper, host is the one who mounted self.

Returns:



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

def host
  owned? ? owner.host : @host
end

#nameObject

Returns the value of attribute name.



35
36
37
# File 'lib/flat_map/open_mapper.rb', line 35

def name
  @name
end

#ownerObject

Returns the value of attribute owner.



35
36
37
# File 'lib/flat_map/open_mapper.rb', line 35

def owner
  @owner
end

#suffixString?

suffix reader. Delegated to owner for owned mappers.

Returns:

  • (String, nil)


102
103
104
# File 'lib/flat_map/open_mapper.rb', line 102

def suffix
  owned? ? owner.suffix : @suffix
end

#targetObject (readonly)

Returns the value of attribute target.



34
35
36
# File 'lib/flat_map/open_mapper.rb', line 34

def target
  @target
end

#traitsObject (readonly)

Returns the value of attribute traits.



34
35
36
# File 'lib/flat_map/open_mapper.rb', line 34

def traits
  @traits
end

Class Method Details

.inherited(subclass) ⇒ Object

Callback to dup mappings and mountings on inheritance. The values are cloned from actual mappers (i.e. something like CustomerAccountMapper, since it is useless to clone empty values of FlatMap::Mapper).

Note: those class attributes are defined in Mapping and Mounting modules.



44
45
46
47
# File 'lib/flat_map/open_mapper.rb', line 44

def self.inherited(subclass)
  subclass.mappings  = mappings.dup
  subclass.mountings = mountings.dup
end

Instance Method Details

#hosted?Boolean

Return true if mapper is hosted, i.e. it is mounted by another mapper.

Returns:

  • (Boolean)


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

def hosted?
  host.present?
end

#inspectString

Return a simple string representation of mapper. Done so to avoid really long inspection of internal objects (target - usually AR model, mountings and mappings)

Returns:

  • (String)


70
71
72
# File 'lib/flat_map/open_mapper.rb', line 70

def inspect
  to_s
end

#owned?Boolean

Return true if mapper is owned. This means that current mapper is actually a trait. Thus, it is a part of an owner mapper.

Returns:

  • (Boolean)


79
80
81
# File 'lib/flat_map/open_mapper.rb', line 79

def owned?
  owner.present?
end

#suffixed?Boolean

Return true if suffix is present.

Returns:

  • (Boolean)


109
110
111
# File 'lib/flat_map/open_mapper.rb', line 109

def suffixed?
  suffix.present?
end