chronorails 0.0.1

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Chronorails
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Chronorails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,5 @@
1
+ require 'chronorails/chronic_accessors'
2
+ require 'chronorails/chronic_parsable_validator'
3
+
4
+ module Chronorails
5
+ end
@@ -0,0 +1,78 @@
1
+ require 'active_support/concern'
2
+ require 'chronic'
3
+ require 'chronic_duration'
4
+
5
+ module Chronorails
6
+ module ChronicAccessors
7
+ extend ActiveSupport::Concern
8
+
9
+ def chronic_errors
10
+ @chronic_errors ||= {}
11
+ end
12
+
13
+ module ClassMethods
14
+ def chronic_duration_field name, options={}
15
+ unless options[:accessible] == false
16
+ attr_accessible "chronic_duration_#{name}"
17
+ end
18
+ unless options[:validates] == false
19
+ validates "chronic_duration_#{name}", :chronic_parsable => true
20
+ end
21
+
22
+ define_method("chronic_duration_#{name}") do
23
+ value = self.send(name)
24
+ ChronicDuration.output(value) unless value.nil?
25
+ end
26
+
27
+ define_method("chronic_duration_#{name}=") do |value|
28
+ if options[:required] == true && value.blank?
29
+ chronic_errors["chronic_duration_#{name}"] = :set_to_blank
30
+ return
31
+ elsif value.blank?
32
+ parsed_value = nil
33
+ else
34
+ parsed_value = ChronicDuration.parse(value)
35
+ if parsed_value.nil?
36
+ chronic_errors["chronic_duration_#{name}"] = :set_to_invalid
37
+ return
38
+ end
39
+ end
40
+
41
+ self.send("#{name}=", parsed_value)
42
+ chronic_errors.delete("chronic_duration_#{name}")
43
+ end
44
+ end
45
+
46
+ def chronic_field name, options={}
47
+ unless options[:accessible] == false
48
+ attr_accessible "chronic_#{name}"
49
+ end
50
+ unless options[:validates] == false
51
+ validates "chronic_#{name}", :chronic_parsable => true
52
+ end
53
+
54
+ define_method("chronic_#{name}") do
55
+ self.send(name)
56
+ end
57
+
58
+ define_method("chronic_#{name}=") do |value|
59
+ if options[:required] == true && value.blank?
60
+ chronic_errors["chronic_#{name}"] = :set_to_blank
61
+ return
62
+ elsif value.blank?
63
+ parsed_value = nil
64
+ else
65
+ parsed_value = Chronic.parse(value)
66
+ if parsed_value.nil?
67
+ chronic_errors["chronic_#{name}"] = :set_to_invalid
68
+ return
69
+ end
70
+ end
71
+
72
+ self.send("#{name}=", parsed_value)
73
+ chronic_errors.delete("chronic_duration_#{name}")
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_model'
2
+ require 'active_model/validator'
3
+
4
+ module Chronorails
5
+ class ChronicParsableValidator < ActiveModel::EachValidator
6
+ def validate_each(record, attribute, value)
7
+ if record.chronic_errors[attribute.to_s] == :set_to_blank
8
+ record.errors[attribute] << (options[:message] || "can't be blank")
9
+ end
10
+ if record.chronic_errors[attribute.to_s] == :set_to_invalid
11
+ record.errors[attribute] << (options[:message] || "must be set to a valid time")
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ # Compatibility with ActiveModel validates method which matches option keys to their validator class
18
+ ChronicParsableValidator = Chronorails::ChronicParsableValidator
@@ -0,0 +1,3 @@
1
+ module Chronorails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :chronorails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chronorails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Simon Hildebrandt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.13
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.13
30
+ - !ruby/object:Gem::Dependency
31
+ name: chronic
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: chronic_duration
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sqlite3
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec-rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Chronorails handles adding virtual attributes to your models (with suitable
95
+ validations, etc) allowing you to set date and duration fields with natural language
96
+ text.
97
+ email:
98
+ - simonhildebrandt@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - lib/chronorails/chronic_accessors.rb
104
+ - lib/chronorails/chronic_parsable_validator.rb
105
+ - lib/chronorails/version.rb
106
+ - lib/chronorails.rb
107
+ - lib/tasks/chronorails_tasks.rake
108
+ - MIT-LICENSE
109
+ - Rakefile
110
+ - README.rdoc
111
+ homepage: http://www.simonhildebrandt.com/chronorails
112
+ licenses: []
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.25
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Chronorails adds chronic and chronic_duration support to your models.
135
+ test_files: []