flexible_date 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +13 -3
  2. data/lib/flexible_date.rb +8 -6
  3. metadata +39 -46
data/README.rdoc CHANGED
@@ -18,10 +18,8 @@ And in config/locales/br.yml (or your native language):
18
18
  messages:
19
19
  with_suffix:
20
20
  invalid: "inválida."
21
- empty: "não pode ser vazia."
22
21
  without_suffix:
23
22
  invalid: "inválida."
24
- empty: "não pode ser vazia."
25
23
 
26
24
 
27
25
  Or, in a reduced form, when messages for with or without suffix are the same:
@@ -32,7 +30,6 @@ Or, in a reduced form, when messages for with or without suffix are the same:
32
30
  format: "%d/%m/%Y"
33
31
  messages:
34
32
  invalid: "inválida."
35
- empty: "não pode ser vazia."
36
33
 
37
34
 
38
35
  Date format follows the same pattern used by strptime and strftime. This will
@@ -69,12 +66,25 @@ Suffix can be customized:
69
66
 
70
67
  class Event < ActiveRecord::Base
71
68
  flexible_date :judgement_day, :suffix => 'yyz'
69
+ end
72
70
 
73
71
  event = Event.new
74
72
  event.judgement_day_yyz = "28/02/2011"
75
73
  event.judgement_day.should == Date.new(2011, 02, 28)
76
74
 
77
75
 
76
+ flexible_date supports conditional execution through the :if option. The object
77
+ itself is passed as parameter to the block:
78
+
79
+ class Event < ActiveRecord::Base
80
+ flexible_date :payday, :if => Proc.new { |n| n.description.blank? }
81
+ end
82
+
83
+ event = Event.new(:description => "some content")
84
+ event.payday_flex = "20/05/2011"
85
+ event.should_not be_valid
86
+
87
+
78
88
  == How to install
79
89
 
80
90
  Just run:
data/lib/flexible_date.rb CHANGED
@@ -3,6 +3,8 @@ module FlexibleDate
3
3
  def flexible_date(*params)
4
4
  params.last.kind_of?(Hash) ? (options, fields = params.pop, params) : (options, fields = {}, params)
5
5
  suffix = options[:suffix] || "flex"
6
+ condition = options[:if]
7
+
6
8
  fields.each do |field|
7
9
  unless methods.include?(:flexible_date_validations)
8
10
  validate :flexible_date_validations
@@ -32,17 +34,17 @@ module FlexibleDate
32
34
  end
33
35
 
34
36
  @flexible_date_errors ||= {}
35
- if value.blank?
37
+ if condition and not condition.call(self)
36
38
  @flexible_date_errors["#{field}".to_sym] = try_t.call(
37
- "flexible_date.messages.without_suffix.empty",
38
- "flexible_date.messages.empty")
39
+ "flexible_date.messages.without_suffix.invalid",
40
+ "flexible_date.messages.invalid")
39
41
  @flexible_date_errors["#{field}_#{suffix}".to_sym] = try_t.call(
40
- "flexible_date.messages.with_suffix.empty",
41
- "flexible_date.messages.empty")
42
+ "flexible_date.messages.with_suffix.invalid",
43
+ "flexible_date.messages.invalid")
42
44
  else
43
45
  begin
44
46
  format = I18n.t("flexible_date.configuration.format")
45
- self.send("#{field}=", Date.strptime(value, format))
47
+ self.send("#{field}=", value.blank? ? "" : Date.strptime(value, format))
46
48
  rescue ArgumentError
47
49
  self.send("#{field}=", nil)
48
50
  @flexible_date_errors["#{field}".to_sym] = try_t.call(
metadata CHANGED
@@ -1,90 +1,83 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: flexible_date
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
4
5
  prerelease:
5
- version: 0.2.0
6
6
  platform: ruby
7
- authors:
8
- - "Rodrigo Manh\xC3\xA3es"
7
+ authors:
8
+ - Rodrigo Manhães
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-05-21 00:00:00 -03:00
12
+ date: 2011-06-10 00:00:00.000000000 -03:00
14
13
  default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
17
16
  name: activerecord
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: &15312300 !ruby/object:Gem::Requirement
20
18
  none: false
21
- requirements:
19
+ requirements:
22
20
  - - ~>
23
- - !ruby/object:Gem::Version
21
+ - !ruby/object:Gem::Version
24
22
  version: 3.0.0
25
23
  type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: sqlite3-ruby
29
24
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: *15312300
26
+ - !ruby/object:Gem::Dependency
27
+ name: sqlite3-ruby
28
+ requirement: &15311840 !ruby/object:Gem::Requirement
31
29
  none: false
32
- requirements:
30
+ requirements:
33
31
  - - ~>
34
- - !ruby/object:Gem::Version
32
+ - !ruby/object:Gem::Version
35
33
  version: 1.3.0
36
34
  type: :development
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
- name: rspec
40
35
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
36
+ version_requirements: *15311840
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &15311380 !ruby/object:Gem::Requirement
42
40
  none: false
43
- requirements:
41
+ requirements:
44
42
  - - ~>
45
- - !ruby/object:Gem::Version
43
+ - !ruby/object:Gem::Version
46
44
  version: 2.6.0
47
45
  type: :development
48
- version_requirements: *id003
46
+ prerelease: false
47
+ version_requirements: *15311380
49
48
  description: Make ActiveRecord understand any date format you want.
50
49
  email: rmanhaes@gmail.com
51
50
  executables: []
52
-
53
51
  extensions: []
54
-
55
52
  extra_rdoc_files: []
56
-
57
- files:
53
+ files:
58
54
  - lib/flexible_date.rb
59
55
  - README.rdoc
60
56
  - LICENSE.txt
61
57
  has_rdoc: true
62
58
  homepage: https://github.com/rodrigomanhaes/flexible_date
63
59
  licenses: []
64
-
65
60
  post_install_message:
66
- rdoc_options:
61
+ rdoc_options:
67
62
  - --charset=UTF-8
68
- require_paths:
63
+ require_paths:
69
64
  - lib
70
- required_ruby_version: !ruby/object:Gem::Requirement
65
+ required_ruby_version: !ruby/object:Gem::Requirement
71
66
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: "0"
76
- required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
72
  none: false
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- version: "0"
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
82
77
  requirements: []
83
-
84
78
  rubyforge_project:
85
- rubygems_version: 1.5.0
79
+ rubygems_version: 1.6.2
86
80
  signing_key:
87
81
  specification_version: 3
88
82
  summary: Make possible enter date fields in any format into your ActiveRecord models
89
83
  test_files: []
90
-