pass_attempt 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 358fbce38a83de3e6bf29fd03f1f25944e0b6606
4
+ data.tar.gz: 21be768972aef21fbd3321f62de057817d9d54e6
5
+ SHA512:
6
+ metadata.gz: a878192ce5637bfa3c47778785041c05500f81568bdee4b9768658912084ac089226eda20074fb22f8af03236faa834f6717ad9377fea4f8eb7ccc1a3200bf5f
7
+ data.tar.gz: babd0529e5b228a4a2bc7487544eff2a10e98f0e2aee609fc442a7a34ef4e47958796c8fa5f0d92e56ddb8ab6d91404606db95a545af4b86bb1cefe1a5ee3ec5
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .ruby-*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pass_attempt.gemspec
4
+ gemspec
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2014 Alejandro Babio
2
+
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # PassAttempt
2
+
3
+ PassAttempt run a block depending on a condition.
4
+
5
+ Super simple Singleton with counter by name, that process a block. when condition is true.
6
+
7
+ I wrote this gem in order to avoid fill my exception_notification service.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'pass_attempt'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install pass_attempt
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ name = 'my_filtred_acction'
29
+ condition = ->(attempts) { attempts.odd? }
30
+
31
+ (1..4).each do |i|
32
+ PassAttempt.try(name, condition) do
33
+ puts "my_filtred_acction #{i}"
34
+ end
35
+ end
36
+
37
+ my_filtred_acction 1
38
+ my_filtred_acction 3
39
+ ```
40
+
41
+ Condition can be a proc that receive counter for name and return boolean.
42
+ Also condition can be a symbol (:log2 or :log10), to be used as test of counter of name.
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( https://github.com/alejandrobabio/pass_attempt/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create a new Pull Request
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'lib' << 'test'
9
+ t.pattern = 'test/*_test.rb'
10
+ t.verbose = true
11
+ end
@@ -0,0 +1,25 @@
1
+ require "pass_attempt/version"
2
+
3
+ module PassAttempt
4
+ module_function
5
+
6
+ def try(name, condition, &block)
7
+ yield if check(condition, incr(name))
8
+ end
9
+
10
+ def incr(name)
11
+ @names ||= {}
12
+ @names[name] = @names.fetch(name, 0) + 1
13
+ end
14
+
15
+ def check(condition, attemp)
16
+ case condition
17
+ when :log2
18
+ Math.log2(attemp) == Math.log2(attemp).truncate
19
+ when :log10
20
+ Math.log10(attemp) == Math.log10(attemp).truncate
21
+ else
22
+ condition.call(attemp)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module PassAttempt
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pass_attempt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pass_attempt"
8
+ spec.version = PassAttempt::VERSION
9
+ spec.authors = ["Alejandro Babio\n"]
10
+ spec.email = ["alejandro.e.babio@hotmail.com"]
11
+ spec.summary = %q{PassAttempt run a block depending on a custom proc.}
12
+ spec.description = %q{PassAttempt run a block depending on a custom proc.}
13
+ spec.homepage = "https://github.com/alejandrobabio/pass_attempt"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+ require 'test_helper'
3
+
4
+ describe PassAttempt do
5
+
6
+ it 'should work with 10 parms and a block' do
7
+ cond = ->(a) { true }
8
+ PassAttempt.try('n', cond) { 'hello world' }.must_equal 'hello world'
9
+ end
10
+
11
+ it 'should not process block if condition is false' do
12
+ condition = ->(attempt) { false }
13
+ PassAttempt.try('z', condition) { 'hello' }.must_equal nil
14
+ end
15
+
16
+ it 'should count by name and evaluate condition' do
17
+ condition = ->(attempt) { attempt.odd? }
18
+ PassAttempt.try('a', condition) { 'hello' }.must_equal 'hello'
19
+ PassAttempt.try('b', condition) { 'hello' }.must_equal 'hello'
20
+ PassAttempt.try('b', condition) { 'hello' }.must_equal nil
21
+ PassAttempt.try('b', condition) { 'hello' }.must_equal 'hello'
22
+ PassAttempt.try('b', condition) { 'hello' }.must_equal nil
23
+ PassAttempt.try('a', condition) { 'hello' }.must_equal nil
24
+ PassAttempt.try('a', condition) { 'hello' }.must_equal 'hello'
25
+ end
26
+
27
+ it 'apply log2 func on condition' do
28
+ 2.times do
29
+ PassAttempt.try('c', :log2) { 'hello' }.must_equal 'hello'
30
+ end
31
+ PassAttempt.try('c', :log2) { 'hello' }.must_equal nil
32
+ PassAttempt.try('c', :log2) { 'hello' }.must_equal 'hello'
33
+ 3.times do
34
+ PassAttempt.try('c', :log2) { 'hello' }.must_equal nil
35
+ end
36
+ PassAttempt.try('c', :log2) { 'hello' }.must_equal 'hello'
37
+ end
38
+
39
+ it 'apply log10 func on condition' do
40
+ PassAttempt.try('d', :log10) { 'hello' }.must_equal 'hello'
41
+ 8.times do
42
+ PassAttempt.try('d', :log10) { 'hello' }.must_equal nil
43
+ end
44
+ PassAttempt.try('d', :log10) { 'hello' }.must_equal 'hello'
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift( File.join( File.dirname(__FILE__), "..", "lib") )
2
+
3
+ #require "rubygems"
4
+ require 'minitest/spec'
5
+ #require 'minitest/mock'
6
+ require 'minitest/autorun'
7
+ require 'pass_attempt'
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pass_attempt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - |
8
+ Alejandro Babio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '5.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '5.0'
56
+ description: PassAttempt run a block depending on a custom proc.
57
+ email:
58
+ - alejandro.e.babio@hotmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/pass_attempt.rb
69
+ - lib/pass_attempt/version.rb
70
+ - pass_attempt.gemspec
71
+ - test/pass_attempt_test.rb
72
+ - test/test_helper.rb
73
+ homepage: https://github.com/alejandrobabio/pass_attempt
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: PassAttempt run a block depending on a custom proc.
97
+ test_files:
98
+ - test/pass_attempt_test.rb
99
+ - test/test_helper.rb