useful_duration 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -18,16 +18,27 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- require 'useful_duration'
22
-
23
21
  > require 'useful_duration'
24
22
 
23
+ > include UsefulDuration
24
+
25
25
  > 10.in_a_gadda_da_vidas.ago
26
26
  => 2013-04-18 11:25:56 -0400
27
27
 
28
28
  > 2.bravehearts.from_now
29
29
  => 2013-04-18 20:10:45 -0400
30
30
 
31
+ ## Defining Your Own Duration
32
+
33
+ > require 'useful_duration'
34
+
35
+ > include UsefulDuration
36
+
37
+ > useful_duration :fortnights, 1209600
38
+
39
+ > 1.fortnight.from_now
40
+ => 2013-07-21 08:47:37 -0400
41
+
31
42
  ## Dependencies
32
43
 
33
44
  This requires ActiveSupport to play nicely with ActiveSupport duration.
@@ -2,22 +2,34 @@ require "useful_duration/version"
2
2
  require 'active_support/duration'
3
3
  require 'active_support/core_ext/time/calculations'
4
4
  require 'active_support/core_ext/time/acts_like'
5
+ require 'active_support/core_ext/string/inflections.rb'
5
6
 
6
- class Numeric
7
+ module UsefulDuration
7
8
 
8
- def bravehearts
9
- ActiveSupport::Duration.new(self * 10620, [[:seconds, self * 10620]])
9
+ def self.included(base)
10
+ # Some nice - built in durations
11
+ useful_duration :bravehearts, 10620
12
+ useful_duration :in_a_gadda_da_vidas, 1023
13
+ useful_duration :doctor_zhivagos, 11820
10
14
  end
11
- alias :braveheart :bravehearts
12
15
 
13
- def in_a_gadda_da_vidas
14
- ActiveSupport::Duration.new(self * 1023, [[:seconds, self * 1023]])
16
+ def useful_duration name, value
17
+ # Send a `define_method` to Numeric class
18
+ Numeric.send(:define_method, name) do
19
+ ActiveSupport::Duration.new(self * value, [[:seconds, self * value]])
20
+ end
21
+
22
+ # Alias the "singular" method with it
23
+ # For example, useful_duration :fortnights, 1209600
24
+ # will create #fortnights and #fortnight
25
+ Numeric.send(:alias_method, singular_method(name), name)
15
26
  end
16
- alias :in_a_gadda_da_vida :in_a_gadda_da_vidas
17
27
 
18
- def doctor_zhivagos
19
- ActiveSupport::Duration.new(self * 11820, [[:seconds, self * 11820]])
28
+ private
29
+ # Helper function to "singularlize" a method name (as a symbol)
30
+ # Using ActiveSupport::Inflector::Inflections
31
+ def singular_method name
32
+ name.to_s.singularize.to_sym
20
33
  end
21
- alias :doctor_zhivago :doctor_zhivagos
22
34
 
23
- end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module UsefulDuration
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: useful_duration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-05-24 00:00:00.000000000 Z
14
+ date: 2013-07-07 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport