block_changes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "bundler", ">= 1.0"
5
+ gem "jeweler", ">= 1.6.4"
6
+ gem "simplecov"
7
+ gem "rspec"
8
+ end
@@ -0,0 +1,29 @@
1
+ Block Changes
2
+ =============
3
+
4
+ Detecting changes in a Ruby block
5
+ ---------------------------------
6
+ The following example illustrates how to see if changes are detected in a Ruby block for the expected variables. Variables a, b, c are concerned in this example. If these variables are changed `changes_detected?` will return true. If these variables are not changes in the block `changes_detected?` will return false. This will be helpful to maintain idempotency. The following example is very simple. We'll know what variables we concern about when we write the code so passing those variables to the code will not be hard. Also we might not want to detect changes of local temporary variables defined inside the block.
7
+
8
+ ```ruby
9
+ require 'block_changes'
10
+
11
+ include BlockChanges
12
+
13
+ a = 1
14
+ b = 2
15
+ c = 3
16
+
17
+ # Note that the variable names are in quotes and it is required.
18
+ detect_changes('a', 'b', 'c') do
19
+ a = 2
20
+ b = 3
21
+ c = 4
22
+ end
23
+
24
+ # Check if changes are detected in the block
25
+ puts "Changes Detected" if changes_detected?
26
+
27
+ # Check the number variables changed in the block
28
+ puts "No of changes: #{get_changes}"
29
+ ```
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../lib/block_changes', __FILE__)
2
+ require 'rake'
3
+ require 'jeweler'
4
+
5
+ Jeweler::Tasks.new do |gemspec|
6
+ gemspec.name = 'block_changes'
7
+ gemspec.version = BlockChanges::VERSION
8
+ gemspec.platform = Gem::Platform::RUBY
9
+ gemspec.date = Time.now.utc.strftime("%Y-%m-%d")
10
+ gemspec.require_path = 'lib'
11
+ gemspec.files = `git ls-files`.split("\n")
12
+ gemspec.extra_rdoc_files = ['LICENSE', 'README.md']
13
+ gemspec.authors = [ 'Kannan Manickam' ]
14
+ gemspec.email = [ 'arangamani.kannan@gmail.com' ]
15
+ gemspec.homepage = 'https://github.com/arangamani/block_changes'
16
+ gemspec.summary = 'Ruby Module for Detecting Variable Changes in a Block'
17
+ gemspec.description = %{
18
+ This is a simple Ruby module for detecting variable changes inside a block for improving code
19
+ with increased idempotency.}
20
+ gemspec.test_files = `git ls-files -- {spec}/*`.split("\n")
21
+ gemspec.rubygems_version = '1.8.17'
22
+ end
@@ -0,0 +1,43 @@
1
+ #
2
+ # Copyright (c) 2012 Kannan Manickam <arangamani.kannan@gmail.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+
23
+ require File.expand_path('../../lib/block_changes', __FILE__)
24
+
25
+ include BlockChanges
26
+
27
+ puts "[BEFORE] Changes detected: #{changes_detected?}"
28
+
29
+ # Some variables are defined
30
+ some_var = 5
31
+ some_hash = { :var => 5 }
32
+ some_array = [4, 5, 'str']
33
+ detect_changes('some_var', 'some_hash', 'some_array') do
34
+ some_var = 6
35
+ some_hash[:var] = 6
36
+ some_array[0] += 1
37
+ end
38
+
39
+ puts "some_var = #{some_var}"
40
+ puts "some_hash = #{some_hash}"
41
+ puts "some_array = #{some_array}"
42
+ puts "[AFTER] Changes detected: #{changes_detected?}"
43
+ puts "[AFTER] No of changes: #{get_changes}"
@@ -0,0 +1,81 @@
1
+ #
2
+ # Copyright (c) 2013 Kannan Manickam <arangamani.kannan@gmail.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+
23
+ module BlockChanges
24
+
25
+ # Version information
26
+ MAJOR = 0
27
+ MINOR = 0
28
+ TINY = 1
29
+ PRE = nil
30
+ VERSION = [MAJOR, MINOR, TINY, PRE].compact.join('.')
31
+
32
+ @changes_detected = nil
33
+ @no_of_block_changes = 0
34
+
35
+ def changes_detected?
36
+ @changes_detected
37
+ end
38
+
39
+ def reset_changes
40
+ @changes_detected = nil
41
+ @no_of_block_changes = 0
42
+ end
43
+
44
+ def get_changes
45
+ @no_of_block_changes
46
+ end
47
+
48
+ def detect_changes(*vars, &block)
49
+ # Reset changes done in the previous block if any
50
+ reset_changes
51
+
52
+ # Collect all variables and put it in an array
53
+ old_vars = []
54
+ vars.each do |var|
55
+ var_eval = eval(var, block.binding)
56
+ if var_eval.is_a?(Hash) || var_eval.is_a?(Array)
57
+ var_eval = var_eval.clone
58
+ end
59
+ old_vars << var_eval
60
+ end
61
+
62
+ # Execute the block
63
+ block.call
64
+
65
+ # Obtain the variables after the block execution
66
+ new_vars = []
67
+ vars.each do |var|
68
+ var_eval = eval(var, block.binding)
69
+ if var_eval.is_a?(Hash) || var_eval.is_a?(Array)
70
+ var_eval = var_eval.clone
71
+ end
72
+ new_vars << var_eval
73
+ end
74
+
75
+ # Check if there were any changes detected during block execution
76
+ vars.each_with_index do |_, index|
77
+ @no_of_block_changes += 1 if old_vars[index] != new_vars[index]
78
+ end
79
+ @changes_detected = @no_of_block_changes > 0
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: block_changes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kannan Manickam
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: ! '
79
+
80
+ This is a simple Ruby module for detecting variable changes inside a block for improving
81
+ code
82
+
83
+ with increased idempotency.'
84
+ email:
85
+ - arangamani.kannan@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - README.md
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - examples/sample.rb
96
+ - lib/block_changes.rb
97
+ homepage: https://github.com/arangamani/block_changes
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.24
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Ruby Module for Detecting Variable Changes in a Block
121
+ test_files: []