formatted_date 1.0.0 → 1.1.0
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/CHANGELOG.rdoc +5 -0
- data/LICENSE +1 -1
- data/README.rdoc +2 -8
- data/Rakefile +33 -0
- data/lib/formatted_date/conversion.rb +5 -2
- data/lib/formatted_date/conversion_plugin.rb +15 -18
- data/lib/formatted_date/formatting.rb +5 -2
- data/test/conversion_test.rb +46 -0
- data/test/database.yml +4 -0
- data/test/fixtures/schema.rb +8 -0
- data/test/fixtures/user.rb +5 -0
- data/test/fixtures/users.yml +6 -0
- data/test/formatted_date_test.rb +37 -0
- data/test/lib/activerecord_test_case.rb +20 -0
- data/test/lib/activerecord_test_connector.rb +32 -0
- data/test/lib/load_fixtures.rb +9 -0
- data/test/tasks.rake +8 -0
- metadata +13 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 1.1.0 released 2009-11-16
|
2
|
+
|
3
|
+
* Adicionados alguns testes
|
4
|
+
* Corre��o de dois pequenos bugs. O plugin n�o respeitava a op��o <tt>format</tt>. Estava faltando uma verifica��o no range da data caso j� fosse passado uma data para convers�o e n�o uma string.
|
5
|
+
|
1
6
|
== 1.0.0 released 2009-11-10
|
2
7
|
|
3
8
|
* Primeiro release
|
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -14,8 +14,6 @@ n
|
|
14
14
|
consideradas inv�lidas). Para capturar esses erros existe o famoso
|
15
15
|
<tt>validates_presence_of</tt>.
|
16
16
|
|
17
|
-
----
|
18
|
-
|
19
17
|
=== Configura��o
|
20
18
|
|
21
19
|
No arquivo <i>config/environment.rb</i> adicione o seguinte:
|
@@ -25,8 +23,6 @@ No arquivo <i>config/environment.rb</i> adicione o seguinte:
|
|
25
23
|
E ent�o execute a task:
|
26
24
|
|
27
25
|
rake gems:install
|
28
|
-
|
29
|
-
----
|
30
26
|
|
31
27
|
=== Exemplos
|
32
28
|
|
@@ -54,8 +50,8 @@ abaixo.
|
|
54
50
|
<tt>default</tt>::
|
55
51
|
O padr�o � <tt>nil</tt>, mas qualquer valor pode ser passado.
|
56
52
|
<tt>validate</tt>::
|
57
|
-
Valor booleano para dizer se o plugin deve
|
58
|
-
|
53
|
+
Valor booleano para dizer se o plugin deve validar o(s) atributo(s) caso
|
54
|
+
ele(s) seja(m) inv�lido(s). Padr�o <tt>true</tt>.
|
59
55
|
<tt>message</tt>::
|
60
56
|
Mensagem a ser exibida caso ocorra algum erro na convers�o para uma data.
|
61
57
|
O padr�o � uma mensagem do tipo <tt>invalid</tt> definida no seu arquivo de
|
@@ -66,8 +62,6 @@ abaixo.
|
|
66
62
|
<tt>range</tt>::
|
67
63
|
Intervalo v�lido para a(s) data(s). Deve ser do tipo <tt>Range</tt>.
|
68
64
|
Padr�o: 1850 � 2038.
|
69
|
-
|
70
|
-
----
|
71
65
|
|
72
66
|
=== M�dulo de Convers�o
|
73
67
|
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
load 'test/tasks.rake'
|
6
|
+
|
7
|
+
FORMATTED_DATE_VERSION = '1.1.0'
|
8
|
+
CLEAN.include('pkg')
|
9
|
+
|
10
|
+
spec = Gem::Specification.new do |s|
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.name = "formatted_date"
|
13
|
+
s.summary = "Customize o formato de datas no Rails."
|
14
|
+
s.description = "Customize o formato de datas no Rails."
|
15
|
+
s.version = FORMATTED_DATE_VERSION
|
16
|
+
|
17
|
+
s.author = "Ícaro Leopoldino da Motta"
|
18
|
+
s.email = "icaro.ldm@gmail.com"
|
19
|
+
|
20
|
+
s.has_rdoc = true
|
21
|
+
s.require_path = "lib"
|
22
|
+
|
23
|
+
s.extra_rdoc_files = %w( README.rdoc CHANGELOG.rdoc )
|
24
|
+
s.files = FileList['init.rb', 'README.rdoc', 'CHANGELOG.rdoc', 'LICENSE', 'Rakefile', 'rails/init.rb', 'lib/**/*', 'test/**/*']
|
25
|
+
end
|
26
|
+
|
27
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
28
|
+
pkg.define
|
29
|
+
end
|
30
|
+
|
31
|
+
task :build => [:clean, :repackage]
|
32
|
+
|
33
|
+
task :default => :test
|
@@ -2,7 +2,9 @@ module FormattedDate
|
|
2
2
|
|
3
3
|
module Conversion
|
4
4
|
|
5
|
+
#:stopdoc:
|
5
6
|
DefaultOptions = FormattedDate::DefaultOptions.tap { |opts| opts.delete(:message); opts.delete(:validate) }
|
7
|
+
#:startdoc:
|
6
8
|
|
7
9
|
#
|
8
10
|
# Tenta fazer a convers�o de cada uma das datas. Em caso de erro,
|
@@ -44,12 +46,13 @@ module FormattedDate
|
|
44
46
|
options[:default]
|
45
47
|
end
|
46
48
|
end
|
47
|
-
elsif date.is_a?(Time) || date.is_a?(Date)
|
48
|
-
date
|
49
|
+
elsif date.is_a?(Time) || date.is_a?(Date) || date.is_a?(DateTime)
|
50
|
+
options[:range].include?(date.year) ? date : options[:default]
|
49
51
|
else
|
50
52
|
options[:default]
|
51
53
|
end
|
52
54
|
end
|
55
|
+
|
53
56
|
dates.many? ? dates : dates.first
|
54
57
|
end
|
55
58
|
end
|
@@ -44,27 +44,24 @@ module FormattedDate
|
|
44
44
|
|
45
45
|
private
|
46
46
|
|
47
|
-
def perform_conversion(klass)
|
48
|
-
@status, @output =
|
49
|
-
if @input.
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
[VALID, parsed]
|
56
|
-
|
57
|
-
[
|
47
|
+
def perform_conversion(klass)
|
48
|
+
@status, @output =
|
49
|
+
if @input.is_a? String
|
50
|
+
if @input.blank?
|
51
|
+
[INVALID, @options.default]
|
52
|
+
else
|
53
|
+
begin
|
54
|
+
parsed = klass.strptime(@input, @options.format)
|
55
|
+
@options.range.include?(parsed.year) ? [VALID, parsed] : [OUT_OF_RANGE, @options.default]
|
56
|
+
rescue ArgumentError
|
57
|
+
[PARSE_FAIL, @options.default]
|
58
58
|
end
|
59
|
-
rescue ArgumentError
|
60
|
-
[PARSE_FAIL, @options.default]
|
61
59
|
end
|
60
|
+
elsif @input.is_a?(Time) || @input.is_a?(Date) || @input.is_a?(DateTime)
|
61
|
+
@options.range.include?(@input.year) ? [VALID, @input] : [OUT_OF_RANGE, @options.default]
|
62
|
+
else
|
63
|
+
[INVALID, @options.default]
|
62
64
|
end
|
63
|
-
elsif @input.is_a?(Time) || @input.is_a?(Date)
|
64
|
-
[VALID, @input]
|
65
|
-
else
|
66
|
-
[INVALID, @options.default]
|
67
|
-
end
|
68
65
|
|
69
66
|
@output
|
70
67
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
module FormattedDate
|
2
2
|
|
3
|
+
#:stopdoc:
|
3
4
|
DefaultOptions = {
|
4
5
|
:format => '%d/%m/%Y',
|
5
6
|
:default => nil,
|
@@ -8,6 +9,7 @@ module FormattedDate
|
|
8
9
|
:message => :invalid,
|
9
10
|
:type => :datetime
|
10
11
|
}
|
12
|
+
#:startdoc:
|
11
13
|
|
12
14
|
#
|
13
15
|
# Formata as datas de acordo com o formato passado.
|
@@ -37,7 +39,7 @@ module FormattedDate
|
|
37
39
|
# pela op��o <format>.
|
38
40
|
#
|
39
41
|
define_method "#{name}_before_type_cast" do
|
40
|
-
if self[name].is_a?(Time) || self[name].is_a?(Date)
|
42
|
+
if self[name].is_a?(Time) || self[name].is_a?(Date) || self[name].is_a?(DateTime)
|
41
43
|
self[name].strftime(options[:format])
|
42
44
|
else
|
43
45
|
self.read_attribute_before_type_cast(name)
|
@@ -50,6 +52,7 @@ module FormattedDate
|
|
50
52
|
define_method "#{name}=" do |value|
|
51
53
|
conv = ConversionPlugin.new(value, options)
|
52
54
|
date = conv.convert!
|
55
|
+
|
53
56
|
if conv.valid?
|
54
57
|
self[name] = date.strftime(options[:db_format])
|
55
58
|
else # data inv�lida
|
@@ -57,7 +60,7 @@ module FormattedDate
|
|
57
60
|
# sabemos que ela � inv�lida. De qualquer forma � necess�rio porque
|
58
61
|
# depois n�s devemos ser capazes de resgatar a data sem o cast
|
59
62
|
# (valor de entrada original).
|
60
|
-
self[name] =
|
63
|
+
self[name] = date
|
61
64
|
|
62
65
|
if options[:validate]
|
63
66
|
self.class.class_eval do
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/lib/activerecord_test_case'
|
3
|
+
require 'test/lib/load_fixtures'
|
4
|
+
require 'lib/formatted_date/conversion'
|
5
|
+
|
6
|
+
class ConversionTest < ActiveRecordTestCase
|
7
|
+
def test_conversion_methods
|
8
|
+
datetime = to_datetime('26/11/1986')
|
9
|
+
date = to_date('26/11/1986')
|
10
|
+
assert_equal date, datetime.to_date
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_return_type
|
14
|
+
date = to_datetime(Time.now)
|
15
|
+
dates = to_datetime(1.year.ago, nil, '21/12/2012')
|
16
|
+
assert_kind_of Time, date
|
17
|
+
assert_kind_of Array, dates
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_format_option
|
21
|
+
date = to_datetime('2000 - 24/01 - [15h 30min]', :format => '%Y - %d/%m - [%Hh %Mmin]')
|
22
|
+
assert_kind_of DateTime, date
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_default_option
|
26
|
+
date_01, date_02 = to_datetime(nil, '30/02/2000', :default => Time.now)
|
27
|
+
assert_kind_of Time, date_01
|
28
|
+
assert_kind_of Time, date_02
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_range_option
|
32
|
+
date_01, date_02 = to_datetime('26/11/1986', '01/01/2001', :range => 1980..2000)
|
33
|
+
assert_kind_of DateTime, date_01
|
34
|
+
assert_nil date_02
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def to_datetime(*args)
|
40
|
+
FormattedDate::Conversion.to_datetime(*args)
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_date(*args)
|
44
|
+
FormattedDate::Conversion.to_date(*args)
|
45
|
+
end
|
46
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/lib/activerecord_test_case'
|
3
|
+
require 'test/lib/load_fixtures'
|
4
|
+
|
5
|
+
class FormattedDateTest < ActiveRecordTestCase
|
6
|
+
fixtures :users
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@user = User.first
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_format_option
|
13
|
+
@user.alone_since = '26-11-1986'
|
14
|
+
assert_kind_of Time, @user.alone_since
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_default_option
|
18
|
+
@user.first_job_on = nil
|
19
|
+
assert_kind_of Date, @user.first_job_on
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_range_option
|
23
|
+
@user.born_at = 200.years.ago
|
24
|
+
assert_nil @user.born_at
|
25
|
+
@user.born_at = Time.now
|
26
|
+
assert_kind_of Time, @user.born_at
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_before_type_cast
|
30
|
+
@user.born_at = '30/02/2000'
|
31
|
+
@user.alone_since = 'ancient times'
|
32
|
+
assert_nil @user.born_at_before_type_cast
|
33
|
+
assert_nil @user.alone_since_before_type_cast
|
34
|
+
@user.born_at = '21/12/2012'
|
35
|
+
assert_equal '21/12/2012', @user.born_at_before_type_cast
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'lib/activerecord_test_connector'
|
2
|
+
|
3
|
+
class ActiveRecordTestCase < Test::Unit::TestCase
|
4
|
+
if defined? ActiveSupport::Testing::SetupAndTeardown
|
5
|
+
include ActiveSupport::Testing::SetupAndTeardown
|
6
|
+
end
|
7
|
+
|
8
|
+
if defined? ActiveRecord::TestFixtures
|
9
|
+
include ActiveRecord::TestFixtures
|
10
|
+
end
|
11
|
+
|
12
|
+
self.fixture_path = ActiveRecordTestConnector::FIXTURES_PATH
|
13
|
+
self.use_transactional_fixtures = true
|
14
|
+
|
15
|
+
def self.fixtures(*args)
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveRecordTestConnector.setup
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_record/fixtures'
|
4
|
+
|
5
|
+
class ActiveRecordTestConnector
|
6
|
+
FIXTURES_PATH = File.join(File.dirname(__FILE__), '..', 'fixtures')
|
7
|
+
|
8
|
+
def self.setup
|
9
|
+
setup_connection
|
10
|
+
load_schema
|
11
|
+
add_load_path FIXTURES_PATH
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def self.add_load_path(path)
|
17
|
+
dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
|
18
|
+
dep.load_paths.unshift path
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.load_schema
|
22
|
+
ActiveRecord::Base.silence do
|
23
|
+
ActiveRecord::Migration.verbose = false
|
24
|
+
load File.join(FIXTURES_PATH, 'schema.rb')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.setup_connection
|
29
|
+
configurations = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'database.yml'))
|
30
|
+
ActiveRecord::Base.establish_connection configurations['sqlite3']
|
31
|
+
end
|
32
|
+
end
|
data/test/tasks.rake
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formatted_date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "\xC3\x8Dcaro Leopoldino da Motta"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-16 00:00:00 -02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -27,11 +27,22 @@ files:
|
|
27
27
|
- README.rdoc
|
28
28
|
- CHANGELOG.rdoc
|
29
29
|
- LICENSE
|
30
|
+
- Rakefile
|
30
31
|
- rails/init.rb
|
31
32
|
- lib/formatted_date/conversion.rb
|
32
33
|
- lib/formatted_date/conversion_plugin.rb
|
33
34
|
- lib/formatted_date/formatting.rb
|
34
35
|
- lib/formatted_date.rb
|
36
|
+
- test/conversion_test.rb
|
37
|
+
- test/database.yml
|
38
|
+
- test/fixtures/schema.rb
|
39
|
+
- test/fixtures/user.rb
|
40
|
+
- test/fixtures/users.yml
|
41
|
+
- test/formatted_date_test.rb
|
42
|
+
- test/lib/activerecord_test_case.rb
|
43
|
+
- test/lib/activerecord_test_connector.rb
|
44
|
+
- test/lib/load_fixtures.rb
|
45
|
+
- test/tasks.rake
|
35
46
|
has_rdoc: true
|
36
47
|
homepage:
|
37
48
|
licenses: []
|