stringify_date 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd5804a988d289ea11a6fc8d7712d3199954343f
4
- data.tar.gz: 4da4feeacc1a9417d2ce3d0d4acb0063bf18d657
3
+ metadata.gz: 14d86eadd473b13290260f5f299bdbb1f2dc3087
4
+ data.tar.gz: f67f1647dfa3311e199241d253bfcf557c062a24
5
5
  SHA512:
6
- metadata.gz: 64b95255f1d1f33c8ed3c4cd3cf61472b167a06051177bdb9411fbc94e84d914eeb31c6ed7269ba4c214f024d7a2d6005fabdcde34c5aa9014ffa1bb24c6e248
7
- data.tar.gz: b3ed078b45b63e2fd1b224efaa5ba4108c49c32a42b5aa88007a3d11f62ece1a4b091e8d30b3956d6eb9c23f4efcbf4f062920190ef91216503067b0341085e7
6
+ metadata.gz: 87e9788265aff1d5f18094be71792d13a0a1e025b3c7199ee5a14584046045e88ddbc37dbdaa95f1e43ebc598adbb7b796fc591ead98966f6a9d28472a08f891
7
+ data.tar.gz: b17d9104dde09b1c20b44fd2f289830a47d4d665941487e0da59de7caa90ac240d43da64623b778bd0dc1228c6814ee7bfb97c69aeaa335f6bfc400a1324bd5e
data/.gitignore CHANGED
@@ -20,3 +20,4 @@ tmp
20
20
  *.o
21
21
  *.a
22
22
  mkmf.log
23
+ spec/dummy/log/test.log
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/README.md CHANGED
@@ -24,7 +24,7 @@ and we want to handle it by using a String object instead:
24
24
  ```ruby
25
25
  class Product < ActiveRecord::Base
26
26
  # the format will be used to format date and datetime as string
27
- stringify :validity, format: '%Y-%m-%d'
27
+ stringify :validity, format: '%d/%m/%Y'
28
28
 
29
29
  end
30
30
  ```
@@ -32,6 +32,18 @@ end
32
32
  Now each Product object will also have attributes called ```validity_string``` and ```validity_string=```
33
33
  to use date and datetime as string.
34
34
 
35
+ So
36
+
37
+ ```ruby
38
+ product = Product.new validity: Date.new(2001,2,3)
39
+ ```
40
+
41
+ We can use now ```_string```
42
+ ```ruby
43
+ product.validity_string # => '03/02/2001'
44
+ ```
45
+
46
+
35
47
  ## Contributing
36
48
 
37
49
  1. Fork it ( https://github.com/matheusvetor/stringify_date/fork )
@@ -1,3 +1,4 @@
1
+ require 'active_record'
1
2
  require 'active_support/concern'
2
3
  require 'active_support/core_ext/array/extract_options'
3
4
 
@@ -20,12 +21,28 @@ module StringifyDate
20
21
  end
21
22
 
22
23
  define_method "#{name}=" do |value|
23
- type = self.class.columns_hash[column_name].type
24
- types = { datetime: DateTime, date: Date }
25
- if value && type.in?([:date, :datetime])
26
- self.send("#{column_name}=", types[type].send('parse', value))
27
- end
24
+ self.send(
25
+ "#{column_name}=",
26
+ begin
27
+ if value.present?
28
+ begin
29
+ Time.parse(value)
30
+ rescue ArgumentError
31
+ instance_variable_set("@#{name}_invalid", true)
32
+ end
33
+ else
34
+ ''
35
+ end
36
+ end
37
+ )
28
38
  end
39
+
40
+ define_method "validate_#{name}" do
41
+ errors.add(name.to_sym, I18n.t('errors.invalid')) if instance_variable_get("@#{name}_invalid")
42
+ end
43
+
44
+ self.send(:validate, :"validate_#{name}")
45
+
29
46
  end
30
47
  end
31
48
  end
@@ -1,3 +1,3 @@
1
1
  module StringifyDate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe StringifyDate::ActiveRecord::Stringify do
4
+
5
+ let(:test_date) do
6
+ TestDate.new(start_at: Date.today, end_at: DateTime.now)
7
+ end
8
+
9
+ let(:test_date_string) do
10
+ TestDate.new(start_at_string: Date.today.to_s, end_at_string: DateTime.now.to_s)
11
+ end
12
+
13
+ it 'should respond to #stringify' do
14
+ expect(TestDate).to respond_to(:stringify)
15
+ end
16
+
17
+ it 'should respond to _string and _string=' do
18
+ expect(test_date).to respond_to(:start_at_string)
19
+ expect(test_date).to respond_to(:start_at_string=)
20
+ expect(test_date).to respond_to(:end_at_string)
21
+ expect(test_date).to respond_to(:end_at_string=)
22
+ end
23
+
24
+ it "should set start_at_string to 22/10/2012 when start_at 2012-10-22 and stringify format is '%d/%m/%Y'" do
25
+ test_date.start_at = Date.new(2012, 10, 22)
26
+
27
+ expect(test_date.start_at_string).to eq('22/10/2012')
28
+ end
29
+
30
+ it 'should be invalid when parse invalid date' do
31
+ test_date.start_at_string = 'invalid'
32
+
33
+ expect(test_date.valid?).to be(false)
34
+ end
35
+
36
+ it 'should be valid when parse empty string' do
37
+ test_date.start_at_string = ''
38
+
39
+ expect(test_date.valid?).to be(true)
40
+ end
41
+
42
+ it "should set start_at_string to 22/10/2012 when start_at 2001-02-03 and stringify format is '%d/%m/%Y %H:%M:%S'" do
43
+ test_date.end_at = DateTime.new(2001,2,3)
44
+
45
+ expect(test_date.end_at_string).to eq('03/02/2001 00:00:00')
46
+ end
47
+ end
@@ -1,4 +1,4 @@
1
1
  class TestDate < ActiveRecord::Base
2
2
  stringify :start_at, format: "%d/%m/%Y"
3
- stringify :end_at, format: "%d/%m/%Y %H:%I:%S"
3
+ stringify :end_at, format: "%d/%m/%Y %H:%M:%S"
4
4
  end
@@ -9,7 +9,7 @@ require "sprockets/railtie"
9
9
  # require "rails/test_unit/railtie"
10
10
 
11
11
  Bundler.require(*Rails.groups)
12
- require "stringfy_date"
12
+ require "stringify_date"
13
13
 
14
14
  module Dummy
15
15
  class Application < Rails::Application
@@ -0,0 +1,7 @@
1
+ ENV["RAILS_ENV"] = 'test'
2
+
3
+ require File.expand_path("../../spec/dummy/config/environment", __FILE__)
4
+
5
+ require 'rspec/rails'
6
+
7
+ require 'stringify_date'
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency "rails"
33
33
  spec.add_development_dependency "rspec-rails"
34
34
  spec.add_development_dependency "sqlite3"
35
+ spec.add_development_dependency 'guard-rspec'
35
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stringify_date
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rute Passos
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-29 00:00:00.000000000 Z
12
+ date: 2014-07-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -109,6 +109,20 @@ dependencies:
109
109
  - - ">="
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: guard-rspec
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
112
126
  description: This gem makes it easy to convert string to date and provides method
113
127
  to create a string version for the attribute
114
128
  email:
@@ -119,12 +133,14 @@ extra_rdoc_files: []
119
133
  files:
120
134
  - ".gitignore"
121
135
  - Gemfile
136
+ - Guardfile
122
137
  - LICENSE.txt
123
138
  - README.md
124
139
  - Rakefile
125
140
  - lib/stringify_date.rb
126
141
  - lib/stringify_date/active_record/stringify.rb
127
142
  - lib/stringify_date/version.rb
143
+ - spec/active_record/stringify_spec.rb
128
144
  - spec/dummy/README.rdoc
129
145
  - spec/dummy/Rakefile
130
146
  - spec/dummy/app/assets/images/.keep
@@ -163,6 +179,7 @@ files:
163
179
  - spec/dummy/db/development.sqlite3
164
180
  - spec/dummy/db/migrate/20140629010823_create_test_dates.rb
165
181
  - spec/dummy/db/schema.rb
182
+ - spec/dummy/db/test.sqlite3
166
183
  - spec/dummy/lib/assets/.keep
167
184
  - spec/dummy/log/.keep
168
185
  - spec/dummy/log/development.log
@@ -170,6 +187,7 @@ files:
170
187
  - spec/dummy/public/422.html
171
188
  - spec/dummy/public/500.html
172
189
  - spec/dummy/public/favicon.ico
190
+ - spec/spec_helper.rb
173
191
  - stringify_date.gemspec
174
192
  homepage: https://github.com/matheusvetor/stringify_date
175
193
  licenses:
@@ -196,6 +214,7 @@ signing_key:
196
214
  specification_version: 4
197
215
  summary: Parser for string an date/datetime
198
216
  test_files:
217
+ - spec/active_record/stringify_spec.rb
199
218
  - spec/dummy/README.rdoc
200
219
  - spec/dummy/Rakefile
201
220
  - spec/dummy/app/assets/images/.keep
@@ -234,6 +253,7 @@ test_files:
234
253
  - spec/dummy/db/development.sqlite3
235
254
  - spec/dummy/db/migrate/20140629010823_create_test_dates.rb
236
255
  - spec/dummy/db/schema.rb
256
+ - spec/dummy/db/test.sqlite3
237
257
  - spec/dummy/lib/assets/.keep
238
258
  - spec/dummy/log/.keep
239
259
  - spec/dummy/log/development.log
@@ -241,3 +261,4 @@ test_files:
241
261
  - spec/dummy/public/422.html
242
262
  - spec/dummy/public/500.html
243
263
  - spec/dummy/public/favicon.ico
264
+ - spec/spec_helper.rb