Module: Pawnee::Modified

Included in:
Base
Defined in:
lib/pawnee/pawnee/modified.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pawnee/pawnee/modified.rb', line 3

def self.included(base)
  # Extend EmptyDirectory to track modifiations
  Thor::Actions::EmptyDirectory.class_eval do
    alias :old_invoke_with_conflict_check :invoke_with_conflict_check
    
    # Change invoke with conflict check to track changes
    # when the block is invoked.
    def invoke_with_conflict_check(*args, &block)
      if block_given?
        old_invoke_with_conflict_check(*args) do
          results = yield
          base.track_modification!
          return results
        end
      else
        return old_invoke_with_conflict_check(*args)
      end
    end
  end
end

Instance Method Details

#modified?Boolean

Actions track if they modify something, or if it is already in the desired state. If they modify, they call the track_modification! method on the base class.

modified? returns true if there has been a tracked modification within the current modify_block block or within the current task.

Returns:

  • (Boolean)


32
33
34
# File 'lib/pawnee/pawnee/modified.rb', line 32

def modified?
  (@modifications && @modifications.first) || false
end

#modify_block(&block) ⇒ Object

Allows you to track modifications within a block. During the block any call to modified? will return the value for only during the block.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pawnee/pawnee/modified.rb', line 46

def modify_block(&block)
  @modifications ||= [false]
  
  # Add a modification value to the stack
  @modifications << false
  
  yield
  
  # Return the modification value (can also be retrieved
  # with modified?)
  return @modifications.pop
end

#track_modification!Object

Track a modification within the current modification block.



38
39
40
41
# File 'lib/pawnee/pawnee/modified.rb', line 38

def track_modification!
  @modifications ||= [false]
  @modifications[@modifications.size-1] = true
end