junk_drawer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +0 -0
- data/LICENSE +20 -0
- data/README.md +3 -0
- data/ROADMAP.md +0 -0
- data/lib/core_extensions/array.rb +9 -0
- data/lib/core_extensions/kernel.rb +8 -0
- data/lib/core_extensions/object.rb +8 -0
- data/lib/core_extensions/object_id.rb +12 -0
- data/lib/core_extensions/string.rb +41 -0
- data/lib/core_extensions/time.rb +17 -0
- data/lib/junk_drawer.rb +1 -0
- data/lib/junk_drawer/version.rb +3 -0
- metadata +96 -0
data/CHANGELOG.md
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Chris Hanks
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
data/ROADMAP.md
ADDED
File without changes
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class Object
|
2
|
+
# ActiveSupport's Object#try has difficulties if you
|
3
|
+
# try to use it on an instance of FalseClass (which
|
4
|
+
# I often seem to end up doing). Therefore...
|
5
|
+
def try(method, *args, &block)
|
6
|
+
send(method, *args, &block) if respond_to? method
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'all_your_base/are/belong_to_us'
|
2
|
+
|
3
|
+
module BSON
|
4
|
+
class ObjectID
|
5
|
+
# Translate ObjectID instances to a more human-friendly format.
|
6
|
+
# The reverse operation is String#to_oid.
|
7
|
+
# Requires the all-your-base gem.
|
8
|
+
def to_display
|
9
|
+
to_s.reverse.to_i(16).to_base_62
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'all_your_base/are/belong_to_us'
|
2
|
+
|
3
|
+
class String
|
4
|
+
class << self
|
5
|
+
# Build a url/human-friendly random string. Defaults to 25 characters long.
|
6
|
+
def friendly(length = 25)
|
7
|
+
@friendly_characters ||= ['0'..'9', 'a'..'z', 'A'..'Z'].map(&:to_a).flatten!
|
8
|
+
(1..length).map{@friendly_characters.sample}.join
|
9
|
+
end
|
10
|
+
|
11
|
+
# Build a completely unfriendly random string. Defaults to 25 characters long.
|
12
|
+
def crazy(length = 25)
|
13
|
+
@crazy_characters ||= ['0'..'9', 'a'..'z', 'A'..'Z'].map(&:to_a).flatten! + ["\\"] +
|
14
|
+
['(', ')', ' '] + %w(~ ` ! @ # $ % ^ & * _ - + = [ ] { } | ; : ' " , < . > / ?)
|
15
|
+
(1..length).map{@crazy_characters.sample}.join
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Translate a human-readable representation of an ObjectID instance back to an ObjectID.
|
20
|
+
# Requires the all-your-base gem.
|
21
|
+
def to_oid
|
22
|
+
string = from_base_62.to_s(16)
|
23
|
+
|
24
|
+
# replace leading zeroes that were lost during the hex conversion
|
25
|
+
until string.length == 24
|
26
|
+
string = '0' + string
|
27
|
+
end
|
28
|
+
|
29
|
+
BSON::ObjectID.from_string(string.reverse)
|
30
|
+
end
|
31
|
+
|
32
|
+
# For when you want to check for a string in a case-insensitive fashion. Mostly used in Mongo queries.
|
33
|
+
def to_regex
|
34
|
+
/^#{Regexp.escape(self)}$/i
|
35
|
+
end
|
36
|
+
|
37
|
+
# Convenience method, for whenever you need to pepper something.
|
38
|
+
def peppered
|
39
|
+
self + ENV['PEPPER']
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Time
|
2
|
+
# Simplify time range checking
|
3
|
+
# time = 10.minutes.from_now
|
4
|
+
# time.within? 5.minutes # "of right now" is implied
|
5
|
+
# => false
|
6
|
+
# time.within? 20.minutes
|
7
|
+
# => true
|
8
|
+
# time.within? 1.minute, :of => 12.minutes.from_now
|
9
|
+
# => false
|
10
|
+
# time.within? 5.minutes, :of => 12.minutes.from_now
|
11
|
+
# => true
|
12
|
+
def within?(period, options = {})
|
13
|
+
comparison_point = options[:of] || Time.now
|
14
|
+
time_difference = (comparison_point - self).abs
|
15
|
+
time_difference < period
|
16
|
+
end
|
17
|
+
end
|
data/lib/junk_drawer.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
%w(array kernel object object_id string time).each {|file| require "core_extensions/#{file}"}
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: junk_drawer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Chris Hanks
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-21 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: all-your-base
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 19
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: 0.3.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: A collection of Ruby core extensions and other goodies.
|
38
|
+
email:
|
39
|
+
- christopher.m.hanks@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- lib/core_extensions/array.rb
|
48
|
+
- lib/core_extensions/object_id.rb
|
49
|
+
- lib/core_extensions/time.rb
|
50
|
+
- lib/core_extensions/kernel.rb
|
51
|
+
- lib/core_extensions/object.rb
|
52
|
+
- lib/core_extensions/string.rb
|
53
|
+
- lib/junk_drawer.rb
|
54
|
+
- lib/junk_drawer/version.rb
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- ROADMAP.md
|
58
|
+
- CHANGELOG.md
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/chanks/junk_drawer
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 23
|
83
|
+
segments:
|
84
|
+
- 1
|
85
|
+
- 3
|
86
|
+
- 6
|
87
|
+
version: 1.3.6
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project: junk_drawer
|
91
|
+
rubygems_version: 1.3.7
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: A small collection of Ruby core extensions.
|
95
|
+
test_files: []
|
96
|
+
|