has_duration 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module HasDuration
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/has_duration.rb CHANGED
@@ -50,6 +50,32 @@ module HasDuration
50
50
  def has_duration(field_name)
51
51
  serialize field_name, DurationSerializer
52
52
  validates field_name, duration: true
53
+
54
+ define_method("#{field_name}_unit") do
55
+ return instance_eval("@#{field_name}_unit") if instance_eval("@#{field_name}_unit")
56
+ send(field_name).inspect.split(' ').last if send(field_name).is_a?(ActiveSupport::Duration)
57
+ end
58
+
59
+ define_method("#{field_name}_unit=") do |unit|
60
+ return unless unit =~ /^(year|month|week|day|hour|minute|second)s?$/
61
+ instance_eval "@#{field_name}_unit = '#{unit}'"
62
+ if size = instance_eval("@#{field_name}_size")
63
+ self.send("#{field_name}=", size.to_i.send(unit))
64
+ end
65
+ end
66
+
67
+ define_method("#{field_name}_size") do
68
+ return instance_eval("@#{field_name}_size") if instance_eval("@#{field_name}_size")
69
+ send(field_name).inspect.split(' ').first if send(field_name).is_a?(ActiveSupport::Duration)
70
+ end
71
+
72
+ define_method("#{field_name}_size=") do |size|
73
+ return unless size.to_s =~ /^(\d{0,10})$/
74
+ instance_eval "@#{field_name}_size = '#{size}'"
75
+ if unit = instance_eval("@#{field_name}_unit")
76
+ self.send("#{field_name}=", size.to_i.send(unit))
77
+ end
78
+ end
53
79
  end
54
80
  end
55
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_duration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-15 00:00:00.000000000 Z
12
+ date: 2013-05-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70301796199480 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70301796199480
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: sqlite3
27
- requirement: &70301796196160 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,7 +37,28 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70301796196160
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
36
62
  description: ! 'Extends ActiveRecord to let you store time durations like ''1.month''
37
63
  or ''10.years''.
38
64
 
@@ -70,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
96
  version: '0'
71
97
  requirements: []
72
98
  rubyforge_project:
73
- rubygems_version: 1.8.15
99
+ rubygems_version: 1.8.23
74
100
  signing_key:
75
101
  specification_version: 3
76
102
  summary: Extends ActiveRecord to let you store time durations like '1.month' or '10.years'