ocassionally 1.0.0

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: 854c08962e0c26c0efdd2b5a29e2aff3c2ab9331
4
+ data.tar.gz: 916d3534505324151ea0948a7f824b9f493f4c9b
5
+ SHA512:
6
+ metadata.gz: 98f07df61b9f475f153feba83db595501730efbc115ef02210497e4c7a48f7129b036134ca0fa52ecf3fd6d58421db286e09bb7c156ba329ae8f1ab27277dae6
7
+ data.tar.gz: 9ac5287c44faa1cf9ad8f99ed259797c858e0d16c9d269118b02c294277583e124129de96304b6efb1656f8e9b9b1ec3989b492f9236c679174e76ab5764eb80
@@ -0,0 +1,19 @@
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
+
16
+ .irbrc
17
+ .rvmrc
18
+ .ruby-version
19
+ .envrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ocassionally.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ You want it; its yours.
@@ -0,0 +1,89 @@
1
+ # ocassionally
2
+
3
+ ocassionally it works; and, ocassionally it doesn't. Why should
4
+ your program work the same way every time? That is boring.
5
+ Add some spice to your code. Make debugging it harder.
6
+ Model real-world things with fuzzy conditionals.
7
+
8
+ Conditional blocks of the 'if' and 'unless' are so black-and-white.
9
+ Add some color to your code with ocassionally.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'ocassionally'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install ocassionally
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'ocassionally'
31
+ include Ocassionally
32
+
33
+ # Default: default is 25% of the time test the conditional
34
+ ocassionally(some_conditional) do
35
+ # interesting things
36
+ end
37
+
38
+ # Check the conditional 75% of the time
39
+ ocassionally(75, some_conditional) do
40
+ # surprising things
41
+ end
42
+
43
+ # Is some_conditional too expensive to execute all the time?
44
+ # Only execute it ocassionally.
45
+ ocassionally(25, "some_conditional") do
46
+ # unexpected things
47
+ end
48
+
49
+ ocassionally("sleep(5)") { puts "Sorry I was sleeping. What do you want?" }
50
+
51
+ ```
52
+
53
+ The order of the two parameters does not matter. The default for
54
+ the rate is 25% the default for the conditional is true. So that
55
+ means you can also do this:
56
+
57
+ ```ruby
58
+ ocassionally do
59
+ # weird things a quarter of the time
60
+ end
61
+
62
+ ocassionally(95) do
63
+ # weird things most of the time
64
+ end
65
+ ```
66
+
67
+ If you get tired of ocassionally doing things. Try
68
+ doing them #sometimes, #seldom, #often, #very_often, #every_time or #never.
69
+
70
+ If you don't like the simplistic #__probability_function(rate)
71
+ then over-ride it with your own. Its Ruby. Its your
72
+ play ground.
73
+
74
+ ## Take a look at this other gem
75
+
76
+ I originally called this library maybe; but, that got co-opted by
77
+ folks who can't handle nil. Then I called it sometimes; but, someone else
78
+ already had that gem name which does something similar. Take a look
79
+ at this one:
80
+
81
+ https://github.com/sudara/sometimes
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/[my-github-username]/ocassionally/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,61 @@
1
+ module Ocassionally
2
+
3
+ def __probability_function(rate)
4
+ rand(100) < rate
5
+ end
6
+
7
+ def __get_rate_and_condition(args, default_rate, default_condition)
8
+ rate = default_rate
9
+ condition = default_condition
10
+ args.each do |arg|
11
+ rate = arg if [Fixnum, Float].include? arg.class
12
+ condition = arg if [String, TrueClass, FalseClass, NilClass].include? arg.class
13
+ end
14
+ if Float == rate.class
15
+ rate = (rate < 1) ? (rate * 100).to_i : rate.to_i
16
+ end
17
+ return rate, condition
18
+ end
19
+
20
+ def __ocassionally(rate, condition, block=nil)
21
+ if __probability_function(rate) && ( String == condition.class ? eval(condition) : condition )
22
+ block.call unless block.nil?
23
+ end
24
+ end
25
+
26
+ def sometimes(*args, &block)
27
+ rate, condition = __get_rate_and_condition(args, 50, true)
28
+ __ocassionally(rate, condition, block)
29
+ end
30
+
31
+ def ocassionally(*args, &block)
32
+ rate, condition = __get_rate_and_condition(args, 25, true)
33
+ __ocassionally(rate, condition, block)
34
+ end
35
+
36
+ def seldom(*args, &block)
37
+ rate, condition = __get_rate_and_condition(args, 5, true)
38
+ __ocassionally(rate, condition, block)
39
+ end
40
+
41
+ def often(*args, &block)
42
+ rate, condition = __get_rate_and_condition(args, 75, true)
43
+ __ocassionally(rate, condition, block)
44
+ end
45
+
46
+ def very_often(*args, &block)
47
+ rate, condition = __get_rate_and_condition(args, 95, true)
48
+ __ocassionally(rate, condition, block)
49
+ end
50
+
51
+ def every_time(*args, &block)
52
+ # defered contionals are ignored
53
+ yield if block_given?
54
+ end
55
+
56
+ def never(*args, &block)
57
+ # noop; but, there may be a side-effect if your conditional changes state
58
+ end
59
+
60
+ end
61
+
@@ -0,0 +1,3 @@
1
+ module Ocassionally
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ocassionally/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ocassionally"
8
+ spec.version = Ocassionally::VERSION
9
+ spec.authors = ["Dewayne VanHoozer"]
10
+ spec.email = ["dvanhoozer@gmail.com"]
11
+ spec.summary = %q{Ocassionally it works; and, ocassionally it doesn't}
12
+ spec.description = %q{Probabilistic conditionals for fuzziness.}
13
+ spec.homepage = "http://github.com/MadBomber/ocassionally"
14
+ spec.license = "You want it, its yours"
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
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ocassionally
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dewayne VanHoozer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Probabilistic conditionals for fuzziness.
42
+ email:
43
+ - dvanhoozer@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/ocassionally.rb
54
+ - lib/ocassionally/version.rb
55
+ - ocassionally.gemspec
56
+ homepage: http://github.com/MadBomber/ocassionally
57
+ licenses:
58
+ - You want it, its yours
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.3.0
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Ocassionally it works; and, ocassionally it doesn't
80
+ test_files: []