crufty 0.1.0

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: 8ddf1c6b724208c924b911979499a2d4349d75bd
4
+ data.tar.gz: 061e47b640b247068c2ca27f94dfd8df0ee7af6b
5
+ SHA512:
6
+ metadata.gz: eabb4d1015e356f4f725f0483e7638c70f6a86f23b1b60896310ef3bec09bb80ed56c6c741d0246f2fa609a9981fd0ada35b705aea3f5b5745b935f37b274448
7
+ data.tar.gz: 74ee577c84eef5706b44878cbcc084cc417a2db625cece835984ecaaf646c8ae9ba093a3d81c12d25e7de0d903bf295480b95a7622b7aa701b7c58f572d9e029
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crufty.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Michael Righi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # Crufty
2
+
3
+ Don't let quick fixes turn into permanently crufty code. Give it an expiration date.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'crufty'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install crufty
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ WARN_AFTER = DateTime.parse("2015-12-15 10:00AM -800")
25
+ ERROR_AFTER = DateTime.parse("2015-12-20 10:00AM -800")
26
+
27
+ # TODO: Remove this code before it starts to spit out warnings,
28
+ # and DEFINITELY remove it before it starts raising errors.
29
+ crufty(WARN_AFTER, ERROR_AFTER) do
30
+ # Hacky code goes here
31
+ end
32
+
33
+ # If you just want the warnings and never want it to error:
34
+
35
+ crufty(WARN_AFTER) do
36
+ # Hacky code goes here
37
+ end
38
+
39
+
40
+ # Explicit Usage:
41
+
42
+ crufty(best_by: WARN_AFTER, expires: ERROR_AFTER) do
43
+ # Hacky code goes here
44
+ end
45
+
46
+
47
+ ```
48
+
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/righi/crufty. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
53
+
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
58
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "crufty"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crufty/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crufty"
8
+ spec.version = Crufty::VERSION
9
+ spec.authors = ["Michael Righi"]
10
+ spec.email = ["michael@righi.me"]
11
+
12
+ spec.summary = %q{Self-destruct your crufty code to ensure it doesn't become permanent.}
13
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/righi/crufty"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "timecop"
34
+ spec.add_development_dependency "activesupport"
35
+ spec.add_development_dependency "pry", '0.10.1'
36
+ spec.add_development_dependency "pry-byebug", '2.0.0'
37
+ end
@@ -0,0 +1,64 @@
1
+ require "crufty/version"
2
+ require "crufty/context"
3
+ require "crufty/exceptions"
4
+ require "logger"
5
+
6
+ module Crufty
7
+ class << self
8
+ attr_accessor :logger, :stale_handler, :expired_handler
9
+ attr_writer :env
10
+
11
+ def env
12
+ @env ||= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
13
+ end
14
+
15
+ def on_stale(&block)
16
+ self.stale_handler = block
17
+ end
18
+
19
+ def on_expired(&block)
20
+ self.expired_handler = block
21
+ end
22
+ end
23
+
24
+ DEFAULT_ON_STALE_HANDLER = proc do |ctx|
25
+ stale_detail = (ctx.best_by.nil?) ? "Crufty code" : "Code went crufty at #{ctx.best_by} and"
26
+ expires_detail = (ctx.expires.nil?) ? "should be removed" : "will stop working at #{ctx.expires}"
27
+ Crufty.logger.warn("#{stale_detail} #{expires_detail}: #{ctx.backtrace[0]}")
28
+ end
29
+
30
+ DEFAULT_ON_EXPIRED_HANDLER = proc do |ctx|
31
+ raise ::Crufty::CodeExpired.new(ctx), "Crufty code expired at #{ctx.expires}", ctx.backtrace
32
+ end
33
+
34
+ if defined?(Rails) && Rails.logger
35
+ self.logger = Rails.logger
36
+ else
37
+ self.logger = Logger.new($stdout)
38
+ end
39
+
40
+ self.stale_handler = DEFAULT_ON_STALE_HANDLER
41
+ self.expired_handler = DEFAULT_ON_EXPIRED_HANDLER
42
+
43
+ module Methods
44
+ def crufty(warn_after = nil, error_after = nil, best_by: nil, expires: nil)
45
+ raise ArgumentError if (warn_after && best_by) || (error_after && expires)
46
+
47
+ best_by = warn_after || best_by
48
+ expires = error_after || expires
49
+
50
+ ctx = ::Crufty::Context.new(best_by, expires, caller)
51
+
52
+ if ctx.state == :expired
53
+ Crufty.expired_handler.call(ctx)
54
+ elsif ctx.state == :stale
55
+ Crufty.stale_handler.call(ctx)
56
+ yield
57
+ else
58
+ yield
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ Object.send :include, Crufty::Methods
@@ -0,0 +1,57 @@
1
+ require 'date'
2
+
3
+ module Crufty
4
+ class Context
5
+ attr_reader :best_by, :expires, :backtrace, :invoked_at, :invoke
6
+
7
+ def initialize(best_by, expires, backtrace=nil)
8
+ @invoke = true
9
+
10
+ if datetimeish?(best_by) && datetimeish?(expires)
11
+ @invoked_at = DateTime.now
12
+ elsif timeish?(best_by) && timeish?(expires)
13
+ @invoked_at = Time.now
14
+ else
15
+ raise ArgumentError, "Unexpected time params: #{best_by.class} and #{expires.class}"
16
+ end
17
+
18
+ @best_by = best_by
19
+ @expires = expires
20
+ @backtrace = backtrace
21
+ end
22
+
23
+ def state
24
+ @state ||= (expired? ? :expired : (stale? ? :stale : :fresh))
25
+ end
26
+
27
+ def fresh?
28
+ !stale? && !expired?
29
+ end
30
+
31
+ def stale?
32
+ if @best_by.nil?
33
+ true
34
+ else
35
+ @invoked_at >= @best_by
36
+ end
37
+ end
38
+
39
+ def expired?
40
+ if @expires.nil?
41
+ false
42
+ else
43
+ @invoked_at >= @expires
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def datetimeish?(time)
50
+ time.nil? || time.is_a?(DateTime)
51
+ end
52
+
53
+ def timeish?(time)
54
+ time.nil? || time.is_a?(Time)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,12 @@
1
+ module Crufty
2
+ class CodeExpired < StandardError
3
+ attr_reader :best_by, :expires
4
+
5
+ def initialize(context)
6
+ @best_by = context.best_by
7
+ @expires = context.expires
8
+
9
+ set_backtrace(context.backtrace)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Crufty
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crufty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Righi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-23 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: timecop
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: activesupport
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: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 2.0.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 2.0.0
111
+ description:
112
+ email:
113
+ - michael@righi.me
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - crufty.gemspec
129
+ - lib/crufty.rb
130
+ - lib/crufty/context.rb
131
+ - lib/crufty/exceptions.rb
132
+ - lib/crufty/version.rb
133
+ homepage: https://github.com/righi/crufty
134
+ licenses:
135
+ - MIT
136
+ metadata:
137
+ allowed_push_host: https://rubygems.org
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.4.8
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Self-destruct your crufty code to ensure it doesn't become permanent.
158
+ test_files: []