small 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,136 @@
1
1
  Small
2
2
  ======
3
3
 
4
- Small collection of useful Ruby utilities.
4
+ Small collection of useful Ruby utilities
5
5
 
6
- Tested with 1.9.3-p125
6
+ ## Classes
7
+
8
+ * **Math**
9
+
10
+ * fibanocci(n)
11
+ * factorial(n)
12
+
13
+ * **String**
14
+
15
+ * camelize
16
+ * dashize
17
+ * humanize
18
+ * underscore
19
+ * constantize
20
+ * encode64
21
+ * decode64
22
+ * to_sha1
23
+ * to_sha256(provider)
24
+ * to_md5
25
+ * escape
26
+ * unescape
27
+
28
+ * **Integer**
29
+
30
+ * year, years
31
+ * month, months
32
+ * week, weeks
33
+ * day, days
34
+ * hour, hours
35
+ * minute, minutes
36
+ * second, seconds
37
+
38
+ * **Array**
39
+
40
+ * extract_options
41
+ * sample
42
+
43
+ * **Hash**
44
+
45
+ * stringify_keys!
46
+ * symbolize_keys!
47
+ * to_params
48
+
49
+ * **Time**
50
+
51
+ * since(seconds)
52
+ * ago(seconds)
53
+ * advance(options)
54
+ * change(options)
55
+ * to_date
56
+ * to_datetime
57
+ * to_formatted_s(format)
58
+ * leap_year?
59
+ * weekend?
60
+ * today?
61
+ * future?
62
+ * past?
63
+ * days_in_month
64
+ * days_to_week_start(start_day)
65
+ * weekends
66
+ * business_week(start_day)
67
+ * beginning_of_business_week(start_day)
68
+ * end_of_business_week(start_day)
69
+ * beginning_of_day
70
+ * beginning_of_week(start_day)
71
+ * beginning_of_month
72
+ * beginning_of_year
73
+ * end_of_day
74
+ * end_of_week(start_day)
75
+ * end_of_month
76
+ * end_of_year
77
+ * tomorrow
78
+ * yesterday
79
+ * next_year
80
+ * last_year
81
+ * next_month
82
+ * last_month
83
+ * next_week
84
+ * last_week
85
+
86
+ * **Object**
87
+
88
+ * blank?
89
+ * present?
90
+ * presence
91
+ * try(method, &block)
92
+
93
+ * **Benchmark** - *small DSL for Benchmark class*
94
+
95
+ * benchmark(&block)
96
+ * measure(&block)
97
+ * realtime(&block)
98
+ * run(n, &block)
99
+ <br />
100
+
101
+ require 'small/benchmark'
102
+
103
+ benchmark do |b|
104
+ b.report("test") do
105
+ run 50_000 do
106
+ puts "hello world"
107
+ end
108
+ end
109
+ end
110
+
111
+ **Notes:** + = some codes extracted from ActiveSupport, but some codes are modified
112
+
113
+ ## Benchmark
114
+
115
+ Using a `time` command execution
116
+
117
+
118
+ **Small**: `time ruby -r small -e 'puts Time.now.end_of_week'`
119
+
120
+ 2012-05-06 23:59:59 -0500
121
+
122
+ real 0m0.167s
123
+ user 0m0.113s
124
+ sys 0m0.048s
125
+
126
+ **ActiveSupport**: `time ruby -r active_support/all -e 'puts Time.now.end_of_week'`
127
+
128
+ 2012-05-06 23:59:59 -0500
129
+
130
+ real 0m1.320s
131
+ user 0m1.240s
132
+ sys 0m0.074s
133
+
134
+ ## License
135
+
136
+ **small** is licensed under the MIT license
data/lib/small.rb CHANGED
@@ -1,12 +1,13 @@
1
1
 
2
2
  module Small
3
- VERSION = "0.3"
3
+ VERSION = "0.4"
4
4
  end
5
5
 
6
6
  require 'small/object'
7
7
  require 'small/string'
8
- require 'small/integer'
9
8
  require 'small/math'
10
9
  require 'small/array'
11
10
  require 'small/hash'
11
+ require 'small/units'
12
+ require 'small/integer'
12
13
  require 'small/time'
data/lib/small/array.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class Array
2
+
2
3
  def extract_options!
3
4
  last.is_a?(Hash) ? pop : {}
4
5
  end
@@ -0,0 +1,29 @@
1
+ require 'benchmark'
2
+
3
+ module Small
4
+ module Benchmark
5
+
6
+ def benchmark(tab = 25)
7
+ ::Benchmark.bm(tab) do |b|
8
+ yield b
9
+ end
10
+ end
11
+
12
+ def measure(name = nil)
13
+ ::Benchmark.measure(name) { yield }
14
+ end
15
+
16
+ def realtime
17
+ ::Benchmark.realtime { yield }
18
+ end
19
+
20
+ def run(n = 100_000)
21
+ n.to_i.times do |i|
22
+ yield i
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+
29
+ include Small::Benchmark
data/lib/small/integer.rb CHANGED
@@ -1,39 +1,12 @@
1
1
  class Integer
2
+ extend Small::Units
3
+
4
+ unit :year, 31536000 # http://www.wolframalpha.com/input/?i=year
5
+ unit :month, 2592000
6
+ unit :week, 604800
7
+ unit :day, 86400
8
+ unit :hour, 3600
9
+ unit :minute, 60
10
+ unit :second, 1
2
11
 
3
- def year
4
- # http://www.wolframalpha.com/input/?i=year
5
- self * 31536000
6
- end
7
-
8
- def month
9
- self * 3600 * 24 * 30
10
- end
11
-
12
- def week
13
- self * 3600 * 24 * 7
14
- end
15
-
16
- def day
17
- self * 3600 * 24
18
- end
19
-
20
- def hour
21
- self * 3600
22
- end
23
-
24
- def minute
25
- self * 60
26
- end
27
-
28
- def second
29
- self * 1
30
- end
31
-
32
- alias :years :year
33
- alias :months :month
34
- alias :weeks :week
35
- alias :days :day
36
- alias :hours :hour
37
- alias :minutes :minute
38
- alias :seconds :second
39
12
  end
data/lib/small/object.rb CHANGED
@@ -4,6 +4,15 @@ class Object
4
4
  respond_to?(:empty?) ? empty? : !self
5
5
  end
6
6
 
7
+ def present?
8
+ !blank?
9
+ end
10
+
11
+ def presence
12
+ self if present?
13
+ end
14
+
15
+
7
16
  def try(*a, &b)
8
17
  if a.empty? && block_given?
9
18
  yield self
data/lib/small/time.rb CHANGED
@@ -2,13 +2,55 @@ require 'time'
2
2
  require 'date'
3
3
  require 'datetime' unless defined?(DateTime)
4
4
 
5
- # Some codes extracted from Rails
5
+ # Some codes extracted from Rails, but modified codes and additional codes
6
6
  class Time
7
7
 
8
8
  DATE_FORMATS = {
9
9
  :default => "%Y-%m-%d %H:%M:%S"
10
10
  }
11
11
 
12
+ DAYS_INTO_WEEK = {
13
+ :monday => 0,
14
+ :tuesday => 1,
15
+ :wednesday => 2,
16
+ :thursday => 3,
17
+ :friday => 4,
18
+ :saturday => 5,
19
+ :sunday => 6
20
+ }
21
+
22
+ def since(seconds)
23
+ self + seconds
24
+ end
25
+
26
+ def ago(seconds)
27
+ since(-seconds)
28
+ end
29
+
30
+ def advance(options)
31
+ seconds = (options[:years] || 0).years +
32
+ (options[:months] || 0).months +
33
+ (options[:weeks] || 0).weeks +
34
+ (options[:days] || 0).days +
35
+ (options[:hours] || 0).hours +
36
+ (options[:minutes] || 0).minutes +
37
+ (options[:seconds] || 0).seconds
38
+ since(seconds)
39
+ end
40
+
41
+ def change(options)
42
+ ::Time.send(
43
+ utc? ? :utc : :local,
44
+ options[:year] || year,
45
+ options[:month] || month,
46
+ options[:day] || day,
47
+ options[:hour] || hour,
48
+ options[:min] || (options[:hour] ? 0 : min),
49
+ options[:sec] || ((options[:hour] || options[:min]) ? 0 : sec),
50
+ options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : usec)
51
+ )
52
+ end
53
+
12
54
  def to_date
13
55
  ::Date.new(year, month, day)
14
56
  end
@@ -25,8 +67,120 @@ class Time
25
67
  end
26
68
  end
27
69
 
70
+ def leap_year?
71
+ to_date.leap?
72
+ end
73
+
74
+ def weekend?
75
+ weekends.include?(self)
76
+ end
77
+
28
78
  def today?
29
79
  to_date == ::Time.now.to_date
30
80
  end
31
81
 
82
+ def future?
83
+ self > ::Time.now
84
+ end
85
+
86
+ def past?
87
+ self < ::Time.now
88
+ end
89
+
90
+ def days_in_month
91
+ ::Date.civil(year, month, -1).day
92
+ end
93
+
94
+ def days_to_week_start(start_day = :monday)
95
+ start_day_number = DAYS_INTO_WEEK[start_day]
96
+ current_day_number = wday != 0 ? wday - 1 : 6
97
+ days_span = current_day_number - start_day_number
98
+ days_span >= 0 ? days_span : 7 + days_span
99
+ end
100
+
101
+ def weekends
102
+ [end_of_week.ago(1.day), end_of_week]
103
+ end
104
+
105
+ def business_week(start_day = :monday)
106
+ days = []
107
+ 5.times {|i| days << beginning_of_week(start_day).since(i.days) }
108
+ days
109
+ end
110
+
111
+ def beginning_of_business_week(start_day = :monday)
112
+ business_week(start_day).first
113
+ end
114
+
115
+ def end_of_business_week(start_day = :monday)
116
+ business_week(start_day).last
117
+ end
118
+
119
+ def beginning_of_day
120
+ change(:hour => 0)
121
+ end
122
+ alias :midnight :beginning_of_day
123
+
124
+ def beginning_of_week(start_day = :monday)
125
+ days_to_start = days_to_week_start(start_day)
126
+ (self - days_to_start.days).midnight
127
+ end
128
+
129
+ def beginning_of_month
130
+ change(:day => 1, :hour => 0)
131
+ end
132
+
133
+ def beginning_of_year
134
+ change(:month => 1, :day => 1, :hour => 0)
135
+ end
136
+
137
+ def end_of_day
138
+ change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.99)
139
+ end
140
+
141
+ def end_of_week(start_day = :monday)
142
+ days_to_end = 6 - days_to_week_start(start_day)
143
+ (self + days_to_end.days).end_of_day
144
+ end
145
+
146
+ def end_of_month
147
+ change(:day => days_in_month, :hour => 23, :min => 59, :sec => 59, :usec => 999999.99)
148
+ end
149
+
150
+ def end_of_year
151
+ change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
152
+ end
153
+
154
+ def tomorrow
155
+ advance(:days => 1)
156
+ end
157
+
158
+ def yesterday
159
+ advance(:days => -1)
160
+ end
161
+
162
+ def next_year
163
+ advance(:years => 1)
164
+ end
165
+
166
+ def last_year
167
+ advance(:years => -1)
168
+ end
169
+
170
+ def next_month
171
+ advance(:months => 1)
172
+ end
173
+
174
+ def last_month
175
+ advance(:months => -1)
176
+ end
177
+
178
+ def next_week
179
+ advance(:weeks => 1)
180
+ end
181
+
182
+ def last_week
183
+ advance(:weeks => -1)
184
+ end
185
+
32
186
  end
@@ -0,0 +1,14 @@
1
+ module Small
2
+ module Units
3
+
4
+ def unit(name, value)
5
+ class_eval <<-METHOD
6
+ def #{name}
7
+ self * #{value.to_i}
8
+ end
9
+ alias :#{name}s :#{name}
10
+ METHOD
11
+ end
12
+
13
+ end
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: small
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-27 00:00:00.000000000 Z
12
+ date: 2012-05-04 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: bryann83@gmail.com
@@ -20,12 +20,14 @@ files:
20
20
  - README.md
21
21
  - LICENSE
22
22
  - lib/small/array.rb
23
+ - lib/small/benchmark.rb
23
24
  - lib/small/hash.rb
24
25
  - lib/small/integer.rb
25
26
  - lib/small/math.rb
26
27
  - lib/small/object.rb
27
28
  - lib/small/string.rb
28
29
  - lib/small/time.rb
30
+ - lib/small/units.rb
29
31
  - lib/small.rb
30
32
  homepage: https://github.com/bry4n/small
31
33
  licenses: []