flexible_date 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/flexible_date.rb +18 -11
  2. metadata +9 -9
data/lib/flexible_date.rb CHANGED
@@ -6,25 +6,27 @@ module FlexibleDate
6
6
  condition = options[:if]
7
7
 
8
8
  fields.each do |field|
9
+ normalized_field = normalize(field, suffix)
10
+
9
11
  unless methods.include?(:flexible_date_validations)
10
12
  validate :flexible_date_validations
11
13
 
12
14
  define_method :flexible_date_validations do
13
15
  if @flexible_date_errors
14
- @flexible_date_errors.each do |field, message|
15
- errors.add(field, message)
16
+ @flexible_date_errors.each do |f, message|
17
+ errors.add(f, message)
16
18
  end
17
19
  end
18
20
  end
19
21
  end
20
22
 
21
- define_method "#{field}_#{suffix}" do
23
+ define_method "#{normalized_field}_#{suffix}" do
22
24
  format = I18n.t("flexible_date.configuration.format")
23
- date = self.send("#{field}")
25
+ date = self.send("#{normalized_field}")
24
26
  date.try(:strftime, format)
25
27
  end
26
28
 
27
- define_method "#{field}_#{suffix}=" do |value|
29
+ define_method "#{normalized_field}_#{suffix}=" do |value|
28
30
  try_t = lambda do |option1, option2|
29
31
  begin
30
32
  I18n.t option1, :raise => true
@@ -35,22 +37,22 @@ module FlexibleDate
35
37
 
36
38
  @flexible_date_errors ||= {}
37
39
  if condition and not condition.call(self)
38
- @flexible_date_errors["#{field}".to_sym] = try_t.call(
40
+ @flexible_date_errors["#{normalized_field}".to_sym] = try_t.call(
39
41
  "flexible_date.messages.without_suffix.invalid",
40
42
  "flexible_date.messages.invalid")
41
- @flexible_date_errors["#{field}_#{suffix}".to_sym] = try_t.call(
43
+ @flexible_date_errors["#{normalized_field}_#{suffix}".to_sym] = try_t.call(
42
44
  "flexible_date.messages.with_suffix.invalid",
43
45
  "flexible_date.messages.invalid")
44
46
  else
45
47
  begin
46
48
  format = I18n.t("flexible_date.configuration.format")
47
- self.send("#{field}=", value.blank? ? "" : Date.strptime(value, format))
49
+ self.send("#{normalized_field}=", value.blank? ? "" : Date.strptime(value, format))
48
50
  rescue ArgumentError
49
- self.send("#{field}=", nil)
50
- @flexible_date_errors["#{field}".to_sym] = try_t.call(
51
+ self.send("#{normalized_field}=", nil)
52
+ @flexible_date_errors["#{normalized_field}".to_sym] = try_t.call(
51
53
  "flexible_date.messages.without_suffix.invalid",
52
54
  "flexible_date.messages.invalid")
53
- @flexible_date_errors["#{field}_#{suffix}".to_sym] = try_t.call(
55
+ @flexible_date_errors["#{normalized_field}_#{suffix}".to_sym] = try_t.call(
54
56
  "flexible_date.messages.with_suffix.invalid",
55
57
  "flexible_date.messages.invalid")
56
58
  end
@@ -58,6 +60,11 @@ module FlexibleDate
58
60
  end
59
61
  end
60
62
  end
63
+
64
+ private
65
+ def normalize field, suffix
66
+ field.to_s.split(suffix)[0].to_sym
67
+ end
61
68
  end
62
69
 
63
70
  ActiveRecord::Base.extend(FlexibleDate)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexible_date
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
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-02-10 00:00:00.000000000 Z
12
+ date: 2012-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &20975320 !ruby/object:Gem::Requirement
16
+ requirement: &11283900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *20975320
24
+ version_requirements: *11283900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3-ruby
27
- requirement: &20974600 !ruby/object:Gem::Requirement
27
+ requirement: &11283260 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.3.3
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *20974600
35
+ version_requirements: *11283260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &20973740 !ruby/object:Gem::Requirement
38
+ requirement: &11282640 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 2.8.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *20973740
46
+ version_requirements: *11282640
47
47
  description: Make ActiveRecord understand any date format you want.
48
48
  email: rmanhaes@gmail.com
49
49
  executables: []
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  version: '0'
75
75
  requirements: []
76
76
  rubyforge_project:
77
- rubygems_version: 1.8.10
77
+ rubygems_version: 1.8.15
78
78
  signing_key:
79
79
  specification_version: 3
80
80
  summary: Make possible enter date fields in any format into your ActiveRecord models