Class: Datamappify::Repository::QueryMethod::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/datamappify/repository/query_method/method.rb,
lib/datamappify/repository/query_method/method/source_attributes_walker.rb

Overview

Provides a default set of methods to the varies Datamappify::Repository::QueryMethod classes

Direct Known Subclasses

Count, Destroy, Exists, Find, FindMultiple, Save

Defined Under Namespace

Classes: SourceAttributesWalker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, entity = nil, *args) ⇒ Method

Returns a new instance of Method.

Parameters:

  • options (Hash)

    a hash containing required items like data_mapper and states

  • entity (Entity) (defaults to: nil)
  • args (any)


20
21
22
23
24
25
# File 'lib/datamappify/repository/query_method/method.rb', line 20

def initialize(options, entity = nil, *args)
  @data_mapper = options[:data_mapper]
  @states      = options[:states]
  @lazy_load   = options[:lazy_load?]
  @entity      = entity
end

Instance Attribute Details

#data_mapperData::Mapper (readonly)

Returns:



9
10
11
# File 'lib/datamappify/repository/query_method/method.rb', line 9

def data_mapper
  @data_mapper
end

#statesUnitOfWork::PersistentStates (readonly)



12
13
14
# File 'lib/datamappify/repository/query_method/method.rb', line 12

def states
  @states
end

Instance Method Details

#attributes_walker(entity) {|provider_name, source_class, attributes| ... } ⇒ void (private)

This method returns an undefined value.

Walks through the attributes and performs actions on them

Parameters:

Yields:

  • (provider_name, source_class, attributes)

    action to be performed on the attributes grouped by their source class

Yield Parameters:

Yield Returns:

  • (void)

See Also:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/datamappify/repository/query_method/method.rb', line 108

def attributes_walker(entity, &block)
  UnitOfWork::Transaction.new(data_mapper) do
    data_mapper.classified_attributes.each do |provider_name, attributes|
      source_attributes_walker.new({
        :entity           => entity,
        :provider_name    => provider_name,
        :attributes       => attributes,
        :dirty_aware?     => dirty_aware?,
        :dirty_attributes => states.find(entity).changed,
        :query_method     => self
      }).execute(&block)
    end
  end
end

#dirty_aware?Boolean

Note:

Override this method for individual query methods

Should the method be aware of the dirty state? i.e. Find probably doesn’t whereas Save does

Returns:

  • (Boolean)


33
34
35
# File 'lib/datamappify/repository/query_method/method.rb', line 33

def dirty_aware?
  false
end

#dispatch_criteria_to_default_source(criteria_name, *args) ⇒ Object (private)

Dispatches a Criteria according to the data mapper‘s default provider and default source class

Parameters:

  • criteria_name (Symbol)
  • args (any)


63
64
65
66
67
# File 'lib/datamappify/repository/query_method/method.rb', line 63

def dispatch_criteria_to_default_source(criteria_name, *args)
  data_mapper.default_provider.build_criteria(
    criteria_name, data_mapper.default_source_class, *args
  )
end

#dispatch_criteria_to_providers(criteria_name, entity) ⇒ void (private)

This method returns an undefined value.

Dispatches a Criteria via #attributes_walker

Parameters:

  • criteria_name (Symbol)
  • entity (Entity)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/datamappify/repository/query_method/method.rb', line 76

def dispatch_criteria_to_providers(criteria_name, entity)
  _primary_record = nil

  attributes_walker(entity) do |provider_name, source_class, attributes|
    attributes.classify { |attr| attr.options[:via] }.each do |via, attrs|
      record = data_mapper.provider(provider_name).build_criteria(
        criteria_name,
        source_class,
        entity,
        attrs,
        :via            => via,
        :primary_record => _primary_record
      )

      _primary_record ||= record
    end
  end
end

#reader?Boolean

Note:

Override this method for individual query methods

The method is for reading data?

Returns:

  • (Boolean)


42
43
44
# File 'lib/datamappify/repository/query_method/method.rb', line 42

def reader?
  false
end

#source_attributes_walkerObject (private)



123
124
125
126
127
128
129
# File 'lib/datamappify/repository/query_method/method.rb', line 123

def source_attributes_walker
  if @lazy_load
    Lazy::SourceAttributesWalker
  else
    SourceAttributesWalker
  end
end

#writer?Boolean

Note:

Override this method for individual query methods

The method is for writing data?

Returns:

  • (Boolean)


51
52
53
# File 'lib/datamappify/repository/query_method/method.rb', line 51

def writer?
  false
end