stringify-time 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fdd25597262865b347c0c23f6c29c2b45c1e122c
4
+ data.tar.gz: 6709ac64e633290f08ce0f08cdbdba37c67be3c9
5
+ SHA512:
6
+ metadata.gz: 0bc1a4c8aa7dc6f186d607d1f60ed9e5bd36c9caac27cadb5add5dafccd0148ee09b0cf0e09ba0d1bb8a4eb075daad485f558a51b86e79419217d49b53123fcf
7
+ data.tar.gz: 6dadb693182d9f02cb17e8917a7092b02c8d3900f796d3ba2f9353a846dfdca46ee9c226e2f26bad580234816c05ce072c62e889f1be7d5fe3d8d31e43a3c143
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stringify_time.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Patrick Perey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ StringifyTime
2
+ =============
3
+
4
+ StrigifyTime is a gem that is modeled after the Railscast episode 033's stringify time rails plugin. I have created this gem for convenience and practice.
5
+
6
+ [Blog Post](http://patrickperey.com/stringify-time-gem) @ [PatrickPerey.com](http://patrickperey.com)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'stringify-time'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install stringify-time
21
+
22
+ ## Usage
23
+
24
+ Within an the model add `stringify_time` passing in a symbol for the attribute you want aliased as a virtual attribute string.
25
+
26
+ class Task < ActiveRecord::Base
27
+ stringify_time :due_at
28
+ end
29
+
30
+ This will create the `due_at_string` getter and setter methods within the Task model. You can then access this attribute in the views like so:
31
+
32
+ `_form.html.erb`
33
+
34
+ <%= form_for @task do |f| %>
35
+ ...
36
+ <div class="field">
37
+ <%= f.label :due_at_string %><br>
38
+ <%= f.text_field :due_at_string %>
39
+ </div>
40
+ ...
41
+ <% end %>
42
+
43
+ Your users are now able to type in the date into a text field instead of using the Rails' default `datetime_select` dropdowns.
44
+
45
+ You can then add a validation within the model using the `due_at_invalid?` method. `due_at_invalid?` returns true if there was an invalid input into the text field, ie an ArgumentError.
46
+
47
+ def validate
48
+ errors.add(:due_at, 'is invalid') if due_at_invalid?
49
+ end
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it ([http://github.com/the4dpatrick/stringify-time-gem](http://github.com/the4dpatrick/stringify-time-gem))
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ require 'active_record'
2
+
3
+ module StringifyTime
4
+ def stringify_time(*names)
5
+ names.each do |name|
6
+ define_method "#{name}_string" do
7
+ read_attribute(name).to_s(:db)
8
+ end
9
+
10
+ define_method "#{name}_string=" do |time_str|
11
+ begin
12
+ write_attribute(name, Time.parse(time_str))
13
+ rescue ArgumentError
14
+ instance_variable_set("@#{name}_invalid", true)
15
+ end
16
+ end
17
+
18
+ define_method "#{name}_invalid?" do
19
+ instance_variable_get("@#{name}_invalid")
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ ActiveRecord::Base.extend StringifyTime
@@ -0,0 +1,3 @@
1
+ module StringifyTime
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stringify-time/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stringify-time"
8
+ spec.version = StringifyTime::VERSION
9
+ spec.authors = ["Patrick Perey"]
10
+ spec.email = ["the4dpatrick@yahoo.com"]
11
+ spec.summary = %q{Railscast 033 stringify-time to a ruby gem}
12
+ spec.description = %q{Stringify Time extends ActiveRecord::Base to include the stringify_time method. You can then dynamically create getter and setter methods for a date attribute editable through a text field. Metaprogramming.}
13
+ spec.homepage = "http://github.com/the4dpatrick/stringify-time-gem"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "activerecord", "~> 3.0"
24
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stringify-time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Perey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Stringify Time extends ActiveRecord::Base to include the stringify_time
56
+ method. You can then dynamically create getter and setter methods for a date attribute
57
+ editable through a text field. Metaprogramming.
58
+ email:
59
+ - the4dpatrick@yahoo.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/stringify-time.rb
70
+ - lib/stringify-time/version.rb
71
+ - stringify-time.gemspec
72
+ homepage: http://github.com/the4dpatrick/stringify-time-gem
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Railscast 033 stringify-time to a ruby gem
96
+ test_files: []