koalemos 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Rakefile +0 -1
- data/VERSION +1 -1
- data/koalemos.gemspec +1 -4
- data/lib/koalemos.rb +1 -1
- data/lib/koalemos/beginning_of_day.rb +2 -1
- data/lib/koalemos/blank.rb +15 -10
- data/lib/koalemos/commaify.rb +3 -2
- data/lib/koalemos/friendly_time.rb +4 -3
- data/lib/koalemos/full.rb +2 -2
- data/lib/koalemos/hash_union.rb +6 -2
- data/lib/koalemos/num_to_time.rb +23 -17
- data/lib/koalemos/odd.rb +3 -2
- data/lib/koalemos/rand.rb +2 -1
- data/lib/koalemos/round.rb +5 -3
- data/lib/koalemos/symbol_to_proc.rb +3 -2
- data/lib/koalemos/try.rb +5 -4
- data/lib/koalemos/uniq_by.rb +9 -3
- metadata +1 -11
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -11,7 +11,6 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/Talby/koalemos"
|
12
12
|
gem.authors = ["Tal Atlas"]
|
13
13
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
-
gem.add_development_dependency "yard", ">= 0"
|
15
14
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
15
|
end
|
17
16
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/koalemos.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{koalemos}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Tal Atlas"]
|
@@ -58,14 +58,11 @@ Gem::Specification.new do |s|
|
|
58
58
|
|
59
59
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
60
60
|
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
61
|
-
s.add_development_dependency(%q<yard>, [">= 0"])
|
62
61
|
else
|
63
62
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
64
|
-
s.add_dependency(%q<yard>, [">= 0"])
|
65
63
|
end
|
66
64
|
else
|
67
65
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
68
|
-
s.add_dependency(%q<yard>, [">= 0"])
|
69
66
|
end
|
70
67
|
end
|
71
68
|
|
data/lib/koalemos.rb
CHANGED
data/lib/koalemos/blank.rb
CHANGED
@@ -1,34 +1,39 @@
|
|
1
|
-
module Koalemos
|
2
|
-
module Blank
|
1
|
+
module Koalemos # :nodoc:# :nodoc:
|
2
|
+
module Blank# :nodoc:
|
3
|
+
# Allows for detection of whether an object contains something regardless of
|
4
|
+
# what class the object is
|
5
|
+
# @author Florian Frank
|
3
6
|
module Object
|
7
|
+
# Returns the !empty? or !self for any object
|
4
8
|
def blank?
|
5
9
|
respond_to?(:empty?) ? empty? : !self
|
6
10
|
end
|
7
|
-
|
11
|
+
|
12
|
+
# Returns !blank?
|
8
13
|
def present?
|
9
14
|
!blank?
|
10
15
|
end
|
11
16
|
end
|
12
17
|
|
13
|
-
module NilClass
|
18
|
+
module NilClass# :nodoc:
|
14
19
|
def blank?
|
15
20
|
true
|
16
21
|
end
|
17
22
|
end
|
18
23
|
|
19
|
-
module FalseClass
|
24
|
+
module FalseClass# :nodoc:
|
20
25
|
def blank?
|
21
26
|
true
|
22
27
|
end
|
23
28
|
end
|
24
29
|
|
25
|
-
module TrueClass
|
30
|
+
module TrueClass# :nodoc:
|
26
31
|
def blank?
|
27
32
|
false
|
28
33
|
end
|
29
34
|
end
|
30
35
|
|
31
|
-
module Array
|
36
|
+
module Array # :nodoc:
|
32
37
|
def self.included(modul)
|
33
38
|
modul.module_eval do
|
34
39
|
alias_method :blank?, :empty?
|
@@ -36,7 +41,7 @@ module Koalemos
|
|
36
41
|
end
|
37
42
|
end
|
38
43
|
|
39
|
-
module Hash
|
44
|
+
module Hash # :nodoc:
|
40
45
|
def self.included(modul)
|
41
46
|
modul.module_eval do
|
42
47
|
alias_method :blank?, :empty?
|
@@ -44,13 +49,13 @@ module Koalemos
|
|
44
49
|
end
|
45
50
|
end
|
46
51
|
|
47
|
-
module String
|
52
|
+
module String # :nodoc:
|
48
53
|
def blank?
|
49
54
|
self !~ /\S/
|
50
55
|
end
|
51
56
|
end
|
52
57
|
|
53
|
-
module Numeric
|
58
|
+
module Numeric # :nodoc:
|
54
59
|
def blank?
|
55
60
|
false
|
56
61
|
end
|
data/lib/koalemos/commaify.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
2
|
class ::Numeric
|
3
3
|
unless method_defined?(:commafy)
|
4
|
+
# Returns a string for a number with commas ever 3 digits
|
4
5
|
def commaify
|
5
6
|
to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
|
6
7
|
end
|
@@ -9,7 +10,7 @@ module Koalemos
|
|
9
10
|
|
10
11
|
class ::Float
|
11
12
|
unless method_defined?(:commafy)
|
12
|
-
def commaofy
|
13
|
+
def commaofy# :nodoc:
|
13
14
|
to_s.gsub(/\d+\./) do |s|
|
14
15
|
s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
|
15
16
|
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__),'num_to_time.rb')
|
2
2
|
|
3
|
-
module Koalemos
|
4
|
-
module FriendlyTime
|
3
|
+
module Koalemos # :nodoc:
|
4
|
+
module FriendlyTime# :nodoc:
|
5
|
+
# Friendly time
|
5
6
|
def friendly(since = Time.now)
|
6
7
|
ago = since - self
|
7
8
|
case ago
|
@@ -31,7 +32,7 @@ module Koalemos
|
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
|
-
class ::Time
|
35
|
+
class ::Time # :nodoc:
|
35
36
|
if !method_defined?(:friendly) && ::Numeric.method_defined?(:day)
|
36
37
|
include FriendlyTime
|
37
38
|
else
|
data/lib/koalemos/full.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
2
|
module Full
|
3
3
|
# Returns the object if it isn't blank (as in Object#blank?), otherwise it
|
4
4
|
# returns nil. If a block was given as an argument and the object isn't
|
@@ -25,7 +25,7 @@ module Koalemos
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
class ::Object
|
28
|
+
class ::Object # :nodoc:
|
29
29
|
include Full
|
30
30
|
end
|
31
31
|
end
|
data/lib/koalemos/hash_union.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
2
|
module HashUnion
|
3
|
+
# Reverse merge
|
4
|
+
# h1 = {:foo => 'foo',:bar => 'bar'}
|
5
|
+
# h1 |= {:foo => 'none',:baz => 'baz'}
|
6
|
+
# h1.should == {:foo => 'foo',:bar => 'bar',:baz => 'baz'} #=> true
|
3
7
|
def |(other)
|
4
8
|
other = other.to_hash if other.respond_to?(:to_hash)
|
5
9
|
other.merge(self)
|
6
10
|
end
|
7
11
|
end
|
8
12
|
|
9
|
-
class ::Hash
|
13
|
+
class ::Hash # :nodoc:
|
10
14
|
if method_defined?(:|)
|
11
15
|
warn "#{self}#| already defined, didn't include at #{__FILE__}:#{__LINE__}"
|
12
16
|
else
|
data/lib/koalemos/num_to_time.rb
CHANGED
@@ -1,49 +1,55 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
|
+
# Blah blah blah
|
2
3
|
module NumToTime
|
3
|
-
def minute
|
4
|
+
def minute# :nodoc:
|
4
5
|
self * 60
|
5
6
|
end
|
6
|
-
alias_method :minutes, :minute
|
7
|
+
alias_method :minutes, :minute# :nodoc:
|
7
8
|
|
8
|
-
def hour
|
9
|
+
def hour# :nodoc:
|
9
10
|
self * 3600
|
10
11
|
end
|
11
|
-
alias_method :hours, :hour
|
12
|
+
alias_method :hours, :hour# :nodoc:
|
12
13
|
|
13
|
-
def day
|
14
|
+
def day# :nodoc:
|
14
15
|
self * 86400
|
15
16
|
end
|
16
|
-
alias_method :days, :day
|
17
|
+
alias_method :days, :day# :nodoc:
|
17
18
|
|
18
|
-
def week
|
19
|
+
def week# :nodoc:
|
19
20
|
self * 604800
|
20
21
|
end
|
21
|
-
alias_method :weeks, :week
|
22
|
+
alias_method :weeks, :week# :nodoc:
|
22
23
|
|
23
|
-
def month
|
24
|
+
def month# :nodoc:
|
24
25
|
self * 2592000
|
25
26
|
end
|
26
|
-
alias_method :months, :month
|
27
|
+
alias_method :months, :month# :nodoc:
|
27
28
|
|
28
|
-
def year
|
29
|
+
def year# :nodoc:
|
29
30
|
self * 31536000
|
30
31
|
end
|
31
|
-
alias_method :years, :year
|
32
|
-
|
32
|
+
alias_method :years, :year# :nodoc:
|
33
|
+
|
34
|
+
# Changes a number into a Time object num seconds in the past
|
35
|
+
# 60.ago #=> 1 minute ago
|
33
36
|
def ago
|
34
37
|
Time.now - to_i
|
35
38
|
end
|
36
|
-
|
39
|
+
|
40
|
+
# Changes a number into a Time object num seconds in the future
|
41
|
+
# 60.ago #=> 1 minute from now
|
37
42
|
def from_now
|
38
43
|
Time.now + to_i
|
39
44
|
end
|
40
|
-
|
45
|
+
|
46
|
+
# Converts seconds to Epoch time
|
41
47
|
def to_time
|
42
48
|
Time.at(to_i)
|
43
49
|
end
|
44
50
|
end
|
45
51
|
|
46
|
-
class ::Numeric
|
52
|
+
class ::Numeric # :nodoc:
|
47
53
|
unless method_defined?(:minute)
|
48
54
|
include NumToTime
|
49
55
|
end
|
data/lib/koalemos/odd.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
2
|
module Odd
|
3
|
+
# Returns boolean of whether the Numeric is odd or not
|
3
4
|
def odd?
|
4
5
|
self%2 == 1
|
5
6
|
end
|
6
7
|
end
|
7
8
|
|
8
|
-
class ::Numeric
|
9
|
+
class ::Numeric # :nodoc:
|
9
10
|
unless method_defined?(:odd?)
|
10
11
|
include Odd
|
11
12
|
end
|
data/lib/koalemos/rand.rb
CHANGED
data/lib/koalemos/round.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
2
|
# A bit more versatile rounding for Ruby
|
3
3
|
module RoundTo
|
4
4
|
# def self.included(klass)
|
@@ -14,7 +14,9 @@ module Koalemos
|
|
14
14
|
# raise NoMethodError, 'no round method found'
|
15
15
|
# end
|
16
16
|
# end
|
17
|
-
|
17
|
+
|
18
|
+
# A more versitile rounding function that rounds to an arbitrary number
|
19
|
+
# of decimil places
|
18
20
|
def round_to(places = 0)
|
19
21
|
unless Integer === places
|
20
22
|
raise TypeError, "argument places has to be an Integer"
|
@@ -36,7 +38,7 @@ module Koalemos
|
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
39
|
-
class ::Float
|
41
|
+
class ::Float # :nodoc:
|
40
42
|
unless method_defined?(:round_to)
|
41
43
|
include RoundTo
|
42
44
|
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
2
|
module ToProc
|
3
|
+
# Converts a symbol to a proc
|
3
4
|
def to_proc
|
4
5
|
lambda do |obj, *args|
|
5
6
|
obj.__send__(self, *args[0..-1])
|
@@ -7,7 +8,7 @@ module Koalemos
|
|
7
8
|
end
|
8
9
|
end
|
9
10
|
|
10
|
-
class ::Symbol
|
11
|
+
class ::Symbol# :nodoc:
|
11
12
|
unless method_defined?(:to_proc)
|
12
13
|
include ToProc
|
13
14
|
end
|
data/lib/koalemos/try.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
2
|
unless Object.method_defined?(:try)
|
3
|
-
class Object
|
4
|
-
alias_method :try, :__send__
|
3
|
+
class Object# :nodoc
|
4
|
+
alias_method :try, :__send__ # :nodoc
|
5
5
|
end
|
6
6
|
|
7
|
-
class ::NilClass
|
7
|
+
class ::NilClass# :nodoc
|
8
|
+
# Trys to call a method (alias for +send+)
|
8
9
|
def try(*a)
|
9
10
|
nil
|
10
11
|
end
|
data/lib/koalemos/uniq_by.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
|
-
module Koalemos
|
1
|
+
module Koalemos # :nodoc:
|
2
2
|
module UniqBy
|
3
|
+
# Finds unique values in an array based on a block passed
|
4
|
+
# [ {:key => 'foo'},
|
5
|
+
# {:key => 'foo'},
|
6
|
+
# {:key => 'bar'},
|
7
|
+
# {:key => 'baz'},
|
8
|
+
# {:key => 'bbaz'}].uniq_by{|a| a[:key]} #=> [{:key=>"bbaz"}, {:key=>"baz"}, {:key=>"foo"}, {:key=>"bar"}]
|
3
9
|
def uniq_by(&b)
|
4
10
|
b ||= lambda { |x| x }
|
5
11
|
inject({}) { |h, e| h[b[e]] ||= e; h }.values
|
6
12
|
end
|
7
13
|
end
|
8
14
|
|
9
|
-
module ::Enumerable
|
15
|
+
module ::Enumerable# :nodoc:
|
10
16
|
if method_defined?(:uniq_by)
|
11
17
|
warn "#{self}#uniq_by already defined, didn't include at #{__FILE__}:#{__LINE__}"
|
12
18
|
else
|
@@ -14,7 +20,7 @@ module Koalemos
|
|
14
20
|
end
|
15
21
|
end
|
16
22
|
|
17
|
-
class ::Array
|
23
|
+
class ::Array # :nodoc:
|
18
24
|
if method_defined?(:uniq_by)
|
19
25
|
warn "#{self}#uniq_by already defined, didn't include at #{__FILE__}:#{__LINE__}"
|
20
26
|
else
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: koalemos
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tal Atlas
|
@@ -22,16 +22,6 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: 1.2.9
|
24
24
|
version:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: yard
|
27
|
-
type: :development
|
28
|
-
version_requirement:
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: "0"
|
34
|
-
version:
|
35
25
|
description: A bunch of small extensions to built in classes/modules
|
36
26
|
email: me@talatlas.com
|
37
27
|
executables: []
|