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

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

Overview

Walks through the attributes of the source classes under a provider (e.g. ActiveRecord), the walker is aware of the dirty state so that certain operations (i.e. #save) can be bypassed

Direct Known Subclasses

Lazy::SourceAttributesWalker

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SourceAttributesWalker

Returns a new instance of SourceAttributesWalker.



8
9
10
11
12
13
14
15
# File 'lib/datamappify/repository/query_method/method/source_attributes_walker.rb', line 8

def initialize(options = {})
  @entity           = options[:entity]
  @provider_name    = options[:provider_name]
  @attributes       = options[:attributes]
  @dirty_aware      = options[:dirty_aware?]
  @dirty_attributes = options[:dirty_attributes]
  @query_method     = options[:query_method]
end

Instance Method Details

#check_dirty?(attributes) ⇒ Boolean (private)

Only walk when it’s not dirty aware, or it has dirty attributes

Parameters:

Returns:

  • (Boolean)


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

def check_dirty?(attributes)
  !@dirty_aware || dirty?(attributes)
end

#dirty?(attributes) ⇒ Boolean (private)

Whether the persistent state object is dirty

Parameters:

Returns:

  • (Boolean)


74
75
76
# File 'lib/datamappify/repository/query_method/method/source_attributes_walker.rb', line 74

def dirty?(attributes)
  (attributes.map(&:key) & @dirty_attributes).any?
end

#do_walk?(source_class, attributes) ⇒ Boolean (private)

Whether it is necessary to do the walk

Parameters:

Returns:

  • (Boolean)


47
48
49
# File 'lib/datamappify/repository/query_method/method/source_attributes_walker.rb', line 47

def do_walk?(source_class, attributes)
  check_dirty?(attributes)
end

#execute {|provider_name, source_class, attributes| ... } ⇒ void

This method returns an undefined value.

Yields:

  • (provider_name, source_class, attributes)

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

Yield Parameters:

Yield Returns:

  • (void)


29
30
31
32
33
34
35
36
# File 'lib/datamappify/repository/query_method/method/source_attributes_walker.rb', line 29

def execute(&block)
  @attributes.classify(&:source_class).each do |source_class, attributes|
    if do_walk?(source_class, attributes)
      block.call(@provider_name, source_class, attributes)
      walk_performed(attributes)
    end
  end
end

#walk_performed(attributes) ⇒ void (private)

This method returns an undefined value.

A hook method for when a walk is performed

Parameters:



56
57
58
# File 'lib/datamappify/repository/query_method/method/source_attributes_walker.rb', line 56

def walk_performed(attributes)
  Logger.performed(@query_method && @query_method.class)
end