gumball 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 46cbe13b04129dc45db4d922b943eba73b653745
4
+ data.tar.gz: 24dc7b3f61ff28295d104ffa4b4e53965d6c2af4
5
+ SHA512:
6
+ metadata.gz: 520b55c18544eadda7c062ed83a1fc1d574119400994020cf23a36dd183b1404a46064da6022d50965805b4da109f87ced5fa06646f441e81d147365636bf642
7
+ data.tar.gz: 9b98c5891ff0b3c69ef9b19203e14695ad32081680d7587d850530e168a01c5e1d0759d09d2a976dbc54da905ccc071d1215836c594adcf2c538f6754adf6b17
@@ -0,0 +1,23 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: kevinstuffandthings
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior.
15
+
16
+ **Expected behavior**
17
+ A clear and concise description of what you expected to happen.
18
+
19
+ **Logs**
20
+ If applicable, add code/outputs to help explain your problem.
21
+
22
+ **Additional context**
23
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: kevinstuffandthings
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,33 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ # uses: ruby/setup-ruby@v1
27
+ uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
28
+ with:
29
+ ruby-version: 2.6
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run tests
33
+ run: bundle exec rspec
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
4
+ .yardoc
5
+ doc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4
4
+ - 2.5
5
+ - 2.6
6
+ script:
7
+ - bundle exec rspec
8
+ deploy:
9
+ provider: rubygems
10
+ api_key:
11
+ secure: jUWa2Jx0HRVv7Dl0zYPTskHQ+RJGy2TUcBWOjyFbBUbeDKiRh8J/NnbMuCOvmL94htSaFk5lcs6k74Itu6jW12+KyT98OAnOardFPFuI0cHHhIMEhF6ms8EhyOCZ0vvR9zav11gfNzyF716qO09DQSDuoKWWNdhrYj8a5paLdbHuRqQiw3YUmnjVQ/v9SPYaZ7ziXC4X4tBxQhK2ujNsNfztSVrpYb6pnsXnt+hFQOnskzi0+er4854iymykeyNVgB71KgivrE3T55ZTXPPglshsZQE7M7FlYA5L3bwzR7SlA202yFYHB3hUN3obwO3BjO4OiAdKQnSosBHaXI13ZvCOz02AfEJhwoxkBXw1/2CvSQ12QQPVgqkpxWY03h+URk+gGHCV+JaqZPCV9excmVWg/IOQou0IJ6h84VFViyVc2aFVN2AIdoaG3NsppbV3j4VuLG6J2VVpNmUzN3pGgi5YSjVq9Hp+huXZFfWpnUgAxc680hKgcFajE3JNANG+NbNEeoDmITvZyV5kvJklgUKPsl2wNk+wY/GGrSBgNOVlyXqTWLZb/8Z/cSrN5DEc6D5BIS/SG95wlCRT8jUqbyEPjT+H77TH2ySfwWZzblLqbIxaLiZr3hX16oMdUx7Cya5LKLFgQBpU6cFUxvZX7SqHwjk1vnuG592Kxd9GkX4=
12
+ gem: gumball
13
+ on:
14
+ tags: true
15
+ repo: kevinstuffandthings/gumball
16
+ skip_cleanup: 'true'
@@ -0,0 +1 @@
1
+ --protected lib/**/*.rb
@@ -0,0 +1,76 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kevinstuffandthings@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kevin McDonald
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # Gumball [![Build Status](https://travis-ci.com/kevinstuffandthings/gumball.svg?branch=master)](https://travis-ci.com/kevinstuffandthings/gumball) [![Gem Version](https://badge.fury.io/rb/gumball.svg)](https://badge.fury.io/rb/gumball)
2
+
3
+ A gem providing a mechanism to dispense instances of a class, somewhere between a true singleton and new instance each time.
4
+
5
+ Originally developed for [Simulmedia](https://simulmedia.com).
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ # update with the version of your choice
12
+ gem 'gumball'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ $ bundle install
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```bash
24
+ $ gem install gumball
25
+ ```
26
+
27
+ ## Usage
28
+ Let's say we have some expensive operation we need to utilize the value of. We need to refresh it occasionally, but if we sometimes get a
29
+ slightly-stale copy, that's ok.
30
+
31
+ ```ruby
32
+ class ExpensiveThing
33
+ def self.value
34
+ sleep 5
35
+ rand(1..100)
36
+ end
37
+ end
38
+ ```
39
+
40
+ We set up a new dispenser for that operation. These dispensers are best saved as class variables, so the dispenser itself is a singleton.
41
+
42
+ ```ruby
43
+ require 'gumball'
44
+
45
+ dispenser = Gumball::Dispenser.new(300) { ExpensiveThing.value }
46
+ # => #<Gumball::Dispenser:0x007f87eff446c8 @ttl=300, @last_refreshed=nil, @refresh_block=#<Proc:0x007f87eff44678@(irb):9>, @on_change_block=nil>
47
+
48
+ dispenser.item # this will take a while
49
+ # => 90
50
+
51
+ dispenser.item # if run in quick succession, returned result is immediate, and value is the same
52
+ # => 90
53
+
54
+ sleep 305
55
+
56
+ dispenser.item # ttl has expired... prepare to wait
57
+ # => 41
58
+
59
+ dispenser.item # we've saved our refreshed version, and our response is immediate!
60
+ # => 41
61
+ ```
62
+
63
+ If we want to be able to set up our dispenser so that we can perform some other operation in the event the value actually changes:
64
+
65
+ ```ruby
66
+ dispenser = Gumball::Dispenser.new(300) { ExpensiveThing.value }
67
+ dispenser.on_change { |o, n| puts "OUR VALUE CHANGED: #{o} -> #{n}" }
68
+ ```
69
+
70
+ The `on_change` method counts on equality being properly implemented between the values, and ONLY fires when inequality between
71
+ old and new items is detected. If the refresh pulls in the same value, the `on_change` block is not triggered.
72
+
73
+ # Problems?
74
+ Please submit an [issue](https://github.com/kevinstuffandthings/gumball/issues).
75
+ We'll figure out how to get you up and running with Gumball as smoothly as possible.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/setup"
3
+ require "bundler/gem_tasks"
4
+
5
+ Dir.glob('lib/tasks/*.rake').each { |r| load r }
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ # coding: utf-8
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'gumball/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "gumball"
9
+ spec.version = Gumball::VERSION
10
+ spec.authors = ["Kevin McDonald"]
11
+ spec.email = ["kevinstuffandthings@gmail.com"]
12
+ spec.summary = %q{Dispense expensive objects with ease}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/kevinstuffandthings/gumball"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "pry-byebug"
25
+ spec.add_development_dependency "rack-test"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "yard"
29
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require "gumball/version"
3
+ require "gumball/dispenser"
4
+
5
+ # Gumball
6
+ module Gumball
7
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+ module Gumball
3
+ class Dispenser
4
+ attr_reader :ttl
5
+
6
+ # Set up a new dispenser.
7
+ # @param ttl [???]
8
+ # @param block
9
+ # @return [Dispenser]
10
+ def initialize(ttl, &block)
11
+ @ttl = ttl
12
+ @last_refreshed = nil
13
+ @refresh_block = block
14
+ @on_change_block = nil
15
+ end
16
+
17
+ # Add a block to execute upon detection of a change in the item.
18
+ # The block should accept the old/new versions of the item.
19
+ # @param block
20
+ # @return [self]
21
+ def on_change(&block)
22
+ @on_change_block = block
23
+ self
24
+ end
25
+
26
+ # Retrieve an item from the dispenser. On the first retrieval, a
27
+ # new instance of the item will be created, and returned until deemed
28
+ # expired by the specified `ttl`.
29
+ # @return item
30
+ def item
31
+ now = Time.now
32
+ old_item = @_item
33
+ @_item = nil if expired?
34
+ unless @_item
35
+ @_item = @refresh_block.call
36
+ @on_change_block.call(old_item, @_item) if @last_refreshed && @on_change_block && (old_item != @_item)
37
+ @last_refreshed = now
38
+ end
39
+ @_item
40
+ end
41
+
42
+ private
43
+
44
+ def expired?
45
+ return false unless @last_refreshed
46
+ @last_refreshed + ttl < Time.now
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Gumball
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ module Gumball
3
+ describe Dispenser do
4
+ def now
5
+ Time.now.to_i
6
+ end
7
+
8
+ it "lives for a bit, and then refreshes" do
9
+ ttl = 5
10
+ data = described_class.new(ttl) { now }.on_change { |_, nv| @about = {new_value: nv, now: now} }
11
+ data.item # this "primes" it
12
+
13
+ n = now
14
+ 5.times { expect(data.item).to be <= n }
15
+ expect(@about).to be nil
16
+
17
+ sleep ttl - 1
18
+ 5.times { expect(data.item).to be <= n }
19
+ expect(@about).to be nil
20
+
21
+ sleep 2
22
+ 5.times { expect(data.item).to be > n }
23
+ expect(@about[:new_value]).to eq data.item
24
+ expect(@about[:now]).to be > n
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'gumball'
2
+
3
+ RSpec.configure do |config|
4
+ config.expect_with :rspec do |expectations|
5
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
6
+ end
7
+
8
+ config.mock_with :rspec do |mocks|
9
+ mocks.verify_partial_doubles = true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gumball
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin McDonald
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-12 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Dispense expensive objects with ease
112
+ email:
113
+ - kevinstuffandthings@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
119
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
120
+ - ".github/workflows/ruby.yml"
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".travis.yml"
124
+ - ".yardopts"
125
+ - CODE_OF_CONDUCT.md
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - gumball.gemspec
131
+ - lib/gumball.rb
132
+ - lib/gumball/dispenser.rb
133
+ - lib/gumball/version.rb
134
+ - spec/dispenser_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: https://github.com/kevinstuffandthings/gumball
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.6.14
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Dispense expensive objects with ease
160
+ test_files:
161
+ - spec/dispenser_spec.rb
162
+ - spec/spec_helper.rb