Module: BlockChanges

Defined in:
lib/block_changes.rb

Overview

Copyright © 2013 Kannan Manickam <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Constant Summary collapse

MAJOR =

Version information

0
MINOR =
0
TINY =
1
PRE =
nil
VERSION =
[MAJOR, MINOR, TINY, PRE].compact.join('.')

Instance Method Summary collapse

Instance Method Details

#changes_detected?Boolean

Returns:

  • (Boolean)


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

def changes_detected?
  @changes_detected
end

#detect_changes(*vars, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/block_changes.rb', line 48

def detect_changes(*vars, &block)
  # Reset changes done in the previous block if any
  reset_changes

  # Collect all variables and put it in an array
  old_vars = []
  vars.each do |var|
    var_eval = eval(var, block.binding)
    if var_eval.is_a?(Hash) || var_eval.is_a?(Array)
      var_eval = var_eval.clone
    end
    old_vars << var_eval
  end

  # Execute the block
  block.call

  # Obtain the variables after the block execution
  new_vars = []
  vars.each do |var|
    var_eval = eval(var, block.binding)
    if var_eval.is_a?(Hash) || var_eval.is_a?(Array)
      var_eval = var_eval.clone
    end
    new_vars << var_eval
  end

  # Check if there were any changes detected during block execution
  vars.each_with_index do |_, index|
    @no_of_block_changes += 1 if old_vars[index] != new_vars[index]
  end
  @changes_detected = @no_of_block_changes > 0
end

#get_changesObject



44
45
46
# File 'lib/block_changes.rb', line 44

def get_changes
  @no_of_block_changes
end

#reset_changesObject



39
40
41
42
# File 'lib/block_changes.rb', line 39

def reset_changes
  @changes_detected = nil
  @no_of_block_changes = 0
end