creative_rails_utilities 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 659c881b3a274cfde2d12897738873d28251e418
4
- data.tar.gz: 3f02b2a1c7371c4f63cf0dcef947c2dbbe12d336
3
+ metadata.gz: df58ca7b40c5342a60e7870b8dc7eb7981a5658e
4
+ data.tar.gz: ac593955b0f0414407b126202593840fd96b12a1
5
5
  SHA512:
6
- metadata.gz: b33798102b44262376951f6380247d0b2bf54abfce0f071f06bf5ae5d85eb4c63e6dd1ed93a01da8179bc2179ea05d9a3bd96ed45699e263731291b4eda66682
7
- data.tar.gz: 7eae1eab13c24e4bf4e4ef06d62b55754ec7bd1fe59f712f1c6a49577093d77d10bcc5a559d2bbefd878d9158d3b20d0337d405b55860e42a21bf0079e14ff02
6
+ metadata.gz: 96eb6862958090754f5c7d6dc40a0a32ff52db575dbfc594ee2006d0c4939c512a9ce114c80784907a8ff2fe54d79661fbd822b67a6803e71327c3224dcd848d
7
+ data.tar.gz: 472ec2660a18dda63c7eb0fee2694fa287cc5cd1067cf525add8d2d5ebf23dd1d60826bc29a195b34583fe6c7141f2d12d0616ced5c495746c5d65361976ece8
data/CHANGELOG.md CHANGED
@@ -8,7 +8,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
8
8
  -
9
9
  ```
10
10
 
11
- ## [0.3.5] - 2016-04-22
11
+ ## [0.4.1] - 2016-05-02
12
+ ### Added
13
+ - Numeric#safe_per
14
+
15
+ ## [0.4.0] - 2016-04-22
12
16
  ### Added
13
17
  - Hash#dig via 'ruby_dig' dep
14
18
 
data/README.md CHANGED
@@ -12,67 +12,6 @@ Intended for `Rails >= 4.0.0`, but will probably work for older projects with ro
12
12
 
13
13
  Feel free to read the source under `/lib/creative_rails_utilities/` and peruse the method "it" lines under `/spec`
14
14
 
15
- #### View Helpers
16
- Rails-only (via railtie) view helpers have been added, they are automagically loaded and usable upon gem inclusion.
17
-
18
- ##### relative_time_parse(earlier_time, later_time=optional)
19
- turns a timestamp into "time ago" format.
20
- Examples:
21
-
22
- ```ruby
23
- # If used with only one argument, will default the second argument to Time.now
24
- # at "2015-01-15 12:00".to_datetime
25
-
26
- relative_time_parse("2015-01-15 12:00".to_datetime)
27
- #=> {key: "now", count: nil}
28
- relative_time_parse("2015-01-15 11:59:59".to_datetime)
29
- #=> {key: "second", count: nil}
30
- relative_time_parse("2015-01-15 11:59:58".to_datetime)
31
- #=> {key: "seconds", count: "1"}
32
- relative_time_parse("2015-01-15 11:59:00".to_datetime)
33
- #=> {key: "minute", count: nil}
34
- relative_time_parse("2015-01-15 11:58:00".to_datetime)
35
- #=> {key: "minutes", count: "2"}
36
- relative_time_parse("2015-01-15 11:00".to_datetime)
37
- #=> {key: "hour", count: nil}
38
- relative_time_parse("2015-01-15 09:00".to_datetime)
39
- #=> {key: "hours", count: "3"}
40
- relative_time_parse("2015-01-14 11:00".to_datetime)
41
- #=> {key: "day", count: nil}
42
- relative_time_parse("2015-01-10 09:00".to_datetime)
43
- #=> {key: "days", count: 5}
44
-
45
- # if used with both arguments, expects the second to be later than the first and will calculate relative time between them
46
- relative_time_parse("2015-01-01 12:00".to_datetime, "2015-01-01 12:02".to_datetime)
47
- #=> {key: "minutes", count: "2"}
48
- ```
49
-
50
- The keys are intended to be used together with `I18n.t`
51
- Sample yaml for English
52
-
53
- ```yml
54
- en:
55
- time:
56
- now: "just now"
57
- second: "a second ago"
58
- seconds: "%{count} seconds ago"
59
- minute: "a minute ago"
60
- minutes: "%{count} minutes ago"
61
- hour: "an hour ago"
62
- hours: "%{count} hours ago"
63
- day: "a day ago"
64
- days: "%{count} days ago"
65
- ```
66
-
67
- Then you can write a simple helper that specifies localization key location and returns the correct value based on the hash returned by `relative_time_parse`
68
- ```ruby
69
- def relative_short_time(t1, t2=nil)
70
- hash = relative_time_parse(t1, t2)
71
- count_hash = hash.count.present? ? hash.except(:key): {}
72
- return I18n.t("time.#{hash[:key]}", count_hash)
73
- end
74
- ```
75
-
76
15
  ##### Date
77
16
 
78
17
  ```ruby
@@ -150,6 +89,15 @@ some_hash.fast_sort_keys #=> some_sorted_hash
150
89
  0.safe_percent(0) #=> 0
151
90
  ```
152
91
 
92
+ ```ruby
93
+ # get a portion even when deleting with zero
94
+ # whole.safe_per(part)
95
+ 100.safe_per(50) #=> 2.0
96
+ 3.12.safe_per(2.6) #=> 1.2
97
+ 1.safe_per(3) #=> 0.3333333333333333
98
+ 0.safe_per(0) #=> 0
99
+ ```
100
+
153
101
  ```ruby
154
102
  # transform a Numeric that denotes seconds into a hash of how many :days, :hours, :minutes ad :seconds that is
155
103
  61.5.to_time_hash #=> {days: 0, hours: 0, minutes: 1, seconds: 1.5}
@@ -191,6 +139,67 @@ string #=> "1..." instead of " 1..."
191
139
  "y".to_bool #=> true
192
140
  ```
193
141
 
142
+ #### View Helpers
143
+ Rails-only (via railtie) view helpers have been added, they are automagically loaded and usable upon gem inclusion.
144
+
145
+ ##### relative_time_parse(earlier_time, later_time=optional)
146
+ turns a timestamp into "time ago" format.
147
+ Examples:
148
+
149
+ ```ruby
150
+ # If used with only one argument, will default the second argument to Time.now
151
+ # at "2015-01-15 12:00".to_datetime
152
+
153
+ relative_time_parse("2015-01-15 12:00".to_datetime)
154
+ #=> {key: "now", count: nil}
155
+ relative_time_parse("2015-01-15 11:59:59".to_datetime)
156
+ #=> {key: "second", count: nil}
157
+ relative_time_parse("2015-01-15 11:59:58".to_datetime)
158
+ #=> {key: "seconds", count: "1"}
159
+ relative_time_parse("2015-01-15 11:59:00".to_datetime)
160
+ #=> {key: "minute", count: nil}
161
+ relative_time_parse("2015-01-15 11:58:00".to_datetime)
162
+ #=> {key: "minutes", count: "2"}
163
+ relative_time_parse("2015-01-15 11:00".to_datetime)
164
+ #=> {key: "hour", count: nil}
165
+ relative_time_parse("2015-01-15 09:00".to_datetime)
166
+ #=> {key: "hours", count: "3"}
167
+ relative_time_parse("2015-01-14 11:00".to_datetime)
168
+ #=> {key: "day", count: nil}
169
+ relative_time_parse("2015-01-10 09:00".to_datetime)
170
+ #=> {key: "days", count: 5}
171
+
172
+ # if used with both arguments, expects the second to be later than the first and will calculate relative time between them
173
+ relative_time_parse("2015-01-01 12:00".to_datetime, "2015-01-01 12:02".to_datetime)
174
+ #=> {key: "minutes", count: "2"}
175
+ ```
176
+
177
+ The keys are intended to be used together with `I18n.t`
178
+ Sample yaml for English
179
+
180
+ ```yml
181
+ en:
182
+ time:
183
+ now: "just now"
184
+ second: "a second ago"
185
+ seconds: "%{count} seconds ago"
186
+ minute: "a minute ago"
187
+ minutes: "%{count} minutes ago"
188
+ hour: "an hour ago"
189
+ hours: "%{count} hours ago"
190
+ day: "a day ago"
191
+ days: "%{count} days ago"
192
+ ```
193
+
194
+ Then you can write a simple helper that specifies localization key location and returns the correct value based on the hash returned by `relative_time_parse`
195
+ ```ruby
196
+ def relative_short_time(t1, t2=nil)
197
+ hash = relative_time_parse(t1, t2)
198
+ count_hash = hash.count.present? ? hash.except(:key): {}
199
+ return I18n.t("time.#{hash[:key]}", count_hash)
200
+ end
201
+ ```
202
+
194
203
  ## Development
195
204
  Use ruby >=2.1
196
205
 
@@ -43,6 +43,12 @@ class Numeric
43
43
  end
44
44
  end
45
45
 
46
+ def safe_per(parts)
47
+ return 0 if self == 0 || parts == 0
48
+
49
+ return self.to_f/parts.to_f
50
+ end
51
+
46
52
  def to_time_hash
47
53
  mm, ss = self.divmod(60)
48
54
  hh, mm = mm.divmod(60)
@@ -1,3 +1,3 @@
1
1
  module CreativeRailsUtilities
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: creative_rails_utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Creative
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-27 00:00:00.000000000 Z
11
+ date: 2016-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport