rails_legit 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 855b56aca820aa680b2a95ec3572d28a617d06bf
4
+ data.tar.gz: 41482d5be38796fff7d213e38dd5c1d7e04958a7
5
+ SHA512:
6
+ metadata.gz: e33a786477a8919de7133ed63008aa73e06050b143aeb285148c737a064ecb25fd86ce3de0d4afbd1aff8320ef4db3dc9173cd45b731bac618e5f88e02618ec3
7
+ data.tar.gz: 1a5dd6eb87d1847579fa906da2b1c22ef45ca74d01789391d68ac5c35364e8897079c6096c3926d608560675f505e6b0a20727fa0cd042074b4094475f8ca13a
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ vendor/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-legit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kashyap
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.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Rails::Legit
2
+
3
+ It is quite common to perform checks like:
4
+
5
+ * Check if an array is a subset of another array
6
+ * A date supplied by the user does not be in the past
7
+ * An event start date will always fall after the event end date
8
+
9
+ and so on...
10
+
11
+ This gem abstracts out this common logic and lets you do things like:
12
+
13
+ validates :start_date, verify_date: { before: Date.current }
14
+
15
+ or
16
+
17
+ validates :end_date, verify_date: { before: :current}
18
+
19
+ As of now, only `Date` and `DateTime` validators are implemented.
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ gem 'rails_legit'
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install rails_legit
34
+
35
+ ## Usage
36
+
37
+ The gem uses Rails' `ActiveModel::Validator` to perform these checks.
38
+
39
+
40
+ ### Date Validator
41
+
42
+ Say we are building an event management application where the management
43
+ can create events where they can specify the start times and the end times
44
+ of the event. `EventForm` is such a class that is a non-persistent model of
45
+ the event record.
46
+
47
+ class Event < ActiveRecord::Base
48
+ # has the following attributes:
49
+ # name
50
+ # description
51
+ # to
52
+ # from
53
+ end
54
+
55
+ The `to` and `from` are DateTime objects that get stored in the DB. And the
56
+ corresponding ActiveModel form: (assuming Rails 4)
57
+
58
+ class EventForm
59
+ include ActiveModel::Model
60
+ attr_accessor :name, :description, :to, :from
61
+ attr_accessor :to_date, :to_time
62
+ attr_accessor :from_date, :from_time
63
+ end
64
+
65
+ The `:to_date`, `:to_time`, `:from_date`, `:from_time` are the attributes
66
+ for the data that is returned from the UI where there are separate inputs
67
+ for start date, end date, start time and end time.
68
+
69
+ To use the `DateValidator`, include the `RailsLegit` module in the
70
+ `EventForm` class:
71
+
72
+ class EventForm
73
+ # unchanged from above
74
+ include RailsLegit
75
+ end
76
+
77
+ Adding validations is as simple as:
78
+
79
+ class EventForm
80
+ # unchanged from above
81
+ validates :from_date, :to_date, verify_date: true
82
+ end
83
+
84
+ Currently supported validation syntax elements are:
85
+
86
+ verify_date: true
87
+
88
+ This will check if the date returned from the input fields in the UI is
89
+ valid or not.
90
+
91
+ verify_date: { before: Date.current }
92
+ # same as:
93
+ verify_date: { before: :current }
94
+
95
+ The valid options are `:before`, `:after`, `:on_or_before`, `:on_or_after`, `:on`.
96
+
97
+ You can pass in a method as a symbol, string or a `Date` object. By default, all symbols except
98
+ `:current`, `:today`, `:now` are sent to the object under validation.
99
+
100
+ validates :from_date, verify_date: { before: :current } # Date.current is used
101
+ validates :from_date, verify_date: { before: :today } # Date.current is used
102
+ validates :from_date, verify_date: { before: :now } # Date.current is used
103
+
104
+ validates :from_date, verify_date: { before: :end_date } # <EventForm Object>.end_date is used
105
+
106
+ Finally,
107
+
108
+ class EventForm
109
+ attr_accessor :to_date, :from_date
110
+ include ActiveModel::Model
111
+ include RailsLegit
112
+ validates :from_date, verify_date: { before: :today }
113
+ validates :to_date, verify_date: { before: :from_date }
114
+ end
115
+
116
+ ## Contributing
117
+
118
+ 1. Fork it
119
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
120
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
121
+ 4. Push to the branch (`git push origin my-new-feature`)
122
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,91 @@
1
+ module RailsLegit
2
+ class VerifyDateValidator < ActiveModel::EachValidator
3
+
4
+ VALID_COMPARISIONS = {
5
+ :before => :<,
6
+ :after => :>,
7
+ :on => :==,
8
+ :on_or_before => :<=,
9
+ :on_or_after => :>=,
10
+ }.freeze
11
+
12
+ attr_accessor :comparisions
13
+
14
+ def initialize(options)
15
+ super
16
+ @comparisions = {}
17
+ process_options!
18
+ end
19
+
20
+ def validate_each(record, attribute, value)
21
+
22
+ date_to_check = try_to_convert_to_date(value)
23
+
24
+ if value.nil?
25
+ record.errors.add(attribute, "Can't be nil")
26
+ return
27
+ end
28
+
29
+ unless date_to_check
30
+ record.errors.add(attribute, "Invalid Date Format")
31
+ return
32
+ end
33
+
34
+ comparisions.each do |key, value|
35
+ date_to_be_checked_with_before_type_cast = value.is_a?(Symbol) ? record.send(value) : value
36
+ date_to_be_checked_with = try_to_convert_to_date(date_to_be_checked_with_before_type_cast)
37
+
38
+ # TODO: should check for :current instead
39
+ message = if date_to_check < Date.today
40
+ "Occurs in the past"
41
+ else
42
+ "Occurs before #{value.to_s.humanize}"
43
+ end
44
+
45
+ unless date_to_check.send(VALID_COMPARISIONS[key], date_to_be_checked_with)
46
+ record.errors.add(attribute, message)
47
+ end
48
+ end
49
+ end
50
+
51
+ def check_validity!
52
+ options.keys.each do |key|
53
+ unless VALID_COMPARISIONS.member?(key)
54
+ raise ArgumentError, "Valid keys for options are #{VALID_COMPARISIONS.keys.join(", ")}"
55
+ end
56
+ end
57
+ end
58
+
59
+ def process_options!
60
+ options.each do |k, v|
61
+ if v.respond_to?(:to_date)
62
+ comparisions[k] = v.send(:to_date)
63
+ elsif v.is_a? Proc
64
+ comparisions[k] = v.call
65
+ elsif v.is_a? Symbol
66
+ if Date.respond_to? v
67
+ comparisions[k] = Date.send v
68
+ else
69
+ comparisions[k] = v
70
+ end
71
+ elsif date = try_to_convert_to_date(v)
72
+ comparisions[k] = date
73
+ else
74
+ raise ArgumentError, "Unable to understand the value for #{k}"
75
+ end
76
+ end
77
+ end
78
+
79
+ def try_to_convert_to_date(arg)
80
+ if arg.respond_to? :to_date
81
+ arg.to_date
82
+ else
83
+ begin
84
+ Date.parse(arg.to_s)
85
+ rescue ArgumentError => e
86
+ return false
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module RailsLegit
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "rails_legit/version"
2
+ require "active_support"
3
+
4
+ module RailsLegit
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :VerifyDateValidator
8
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_legit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_legit"
8
+ spec.version = RailsLegit::VERSION
9
+ spec.authors = ["Kashyap"]
10
+ spec.email = ["kashyap.kmbc@gmail.com"]
11
+ spec.description = %q{Provides a DSL for common validation formats like Date, Array, DateTime etc.}
12
+ spec.summary = %q{Provides a DSL for common validation formats like Date, Array, DateTime etc.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rails"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require File.expand_path("../../lib/rails_legit.rb", __FILE__)
9
+ Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1,27 @@
1
+ shared_examples "basic date validations" do
2
+
3
+ context "Invalid Date" do
4
+ let(:date) { "Invalid Date" }
5
+ it { should be_invalid }
6
+ end
7
+
8
+ context "Valid Date as a String" do
9
+ let(:date) { "21 December 2013" }
10
+ it { should be_valid }
11
+ end
12
+
13
+ context "Valid Date as a Date object" do
14
+ let(:date) { Date.today }
15
+ it { should be_valid }
16
+ end
17
+
18
+ context "Valid Date as a DateTime object" do
19
+ let(:date) { DateTime.now }
20
+ it { should be_valid }
21
+ end
22
+
23
+ context "Valid Date as a Time object" do
24
+ let(:date) { Time.now }
25
+ it { should be_valid }
26
+ end
27
+ end
@@ -0,0 +1,159 @@
1
+ require "spec_helper"
2
+ require "active_model"
3
+
4
+ class TestRecordWithNoExtraOptions
5
+ include ActiveModel::Validations
6
+ include RailsLegit
7
+
8
+ validates :date, verify_date: true
9
+
10
+ attr_accessor :date
11
+ def initialize(date)
12
+ @date = date
13
+ end
14
+ end
15
+
16
+ class TestRecordWithNoExtraOptionsMultipleAttributes
17
+ include ActiveModel::Validations
18
+ include RailsLegit
19
+
20
+ validates :date, :anotherdate, verify_date: true
21
+
22
+ attr_accessor :date, :anotherdate
23
+ def initialize(date, anotherdate)
24
+ @date = date
25
+ @anotherdate = anotherdate
26
+ end
27
+ end
28
+
29
+ class TestRecordWithMultipleAttributesBeforeOption
30
+ include ActiveModel::Validations
31
+ include RailsLegit
32
+
33
+ validates :date, :anotherdate, verify_date: { before: :before_date }
34
+
35
+ attr_accessor :date, :anotherdate, :before_date
36
+ def initialize(date, anotherdate, before_date)
37
+ @date = date
38
+ @anotherdate = anotherdate
39
+ @before_date = before_date
40
+ end
41
+ end
42
+
43
+ class TestRecordWithMultipleAttributesBeforeOptionCurrentSymbol
44
+ include ActiveModel::Validations
45
+ include RailsLegit
46
+
47
+ validates :date, :anotherdate, verify_date: { before: :today }
48
+
49
+ attr_accessor :date, :anotherdate, :before_date
50
+ def initialize(date, anotherdate, before_date)
51
+ @date = date
52
+ @anotherdate = anotherdate
53
+ @before_date = before_date
54
+ end
55
+ end
56
+
57
+ class TestRecordWithMultipleAttributesBeforeOptionProc
58
+ include ActiveModel::Validations
59
+ include RailsLegit
60
+
61
+ validates :date, :anotherdate, verify_date: { before: ->{ Date.today + 1 } }
62
+
63
+ attr_accessor :date, :anotherdate
64
+ def initialize(date, anotherdate)
65
+ @date = date
66
+ @anotherdate = anotherdate
67
+ end
68
+ end
69
+
70
+ describe RailsLegit::VerifyDateValidator do
71
+ describe "No Extra Options provided" do
72
+ let(:record) { TestRecordWithNoExtraOptions.new(date) }
73
+ subject { record }
74
+
75
+ include_examples "basic date validations"
76
+
77
+ context "Invalid Date" do
78
+ let(:date) { "Invalid Date" }
79
+
80
+ it "should attach error on appropriate method" do
81
+ record.valid?
82
+ expect(record.errors[:date]).to include("Invalid Date Format")
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "No Extra Options provided and multiple attributes" do
88
+ let(:record) { TestRecordWithNoExtraOptionsMultipleAttributes.new(date, anotherdate) }
89
+ let(:anotherdate) { Date.today }
90
+ subject { record }
91
+
92
+ include_examples "basic date validations"
93
+
94
+ context "Invalid Date on comparision method" do
95
+ let(:anotherdate) { "Invalid Date" }
96
+ let(:date) { Date.today }
97
+
98
+ it "should attach error on appropirate method" do
99
+ record.valid?
100
+ expect(record.errors[:anotherdate]).to include("Invalid Date Format")
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "Before option, multiple attributes" do
106
+ let(:date) { "12 december 1900" }
107
+ subject { record }
108
+
109
+ context "Dates in the past" do
110
+ let(:anotherdate) { Date.today - 1 }
111
+
112
+ context "comparision object is a Proc" do
113
+ let(:record) { TestRecordWithMultipleAttributesBeforeOptionProc.new(date, anotherdate) }
114
+ it { should be_valid }
115
+ end
116
+
117
+ context "comparision object is a Symbol" do
118
+ let(:before_date) { Date.today + 1 }
119
+ let(:record) { TestRecordWithMultipleAttributesBeforeOption.new(date, anotherdate, before_date) }
120
+
121
+ it { should be_valid }
122
+ end
123
+
124
+ context "comparision object is :current" do
125
+ let(:before_date) { :current }
126
+ let(:record) { TestRecordWithMultipleAttributesBeforeOptionCurrentSymbol.new(date, anotherdate, before_date) }
127
+
128
+ it { should be_valid }
129
+ end
130
+ end
131
+
132
+ context "Dates in the future" do
133
+ let(:anotherdate) { Date.today + 100 }
134
+
135
+ context "comparision object is a Proc" do
136
+ let(:record) { TestRecordWithMultipleAttributesBeforeOptionProc.new(date, anotherdate) }
137
+ it { should be_invalid }
138
+
139
+ it "should attach error to the correct attribute" do
140
+ record.valid?
141
+ message = "Occurs before #{Date.today + 1}"
142
+ expect(record.errors[:anotherdate]).to include(message)
143
+ end
144
+ end
145
+
146
+ context "comparision object is a Symbol" do
147
+ let(:before_date) { Date.today + 1 }
148
+ let(:record) { TestRecordWithMultipleAttributesBeforeOption.new(date, anotherdate, before_date) }
149
+
150
+ it { should be_invalid }
151
+
152
+ it "should attach error to the correct attribute" do
153
+ record.valid?
154
+ expect(record.errors[:anotherdate]).to include("Occurs before Before date")
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_legit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kashyap
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-07 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Provides a DSL for common validation formats like Date, Array, DateTime
70
+ etc.
71
+ email:
72
+ - kashyap.kmbc@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/rails_legit.rb
83
+ - lib/rails_legit/verify_date_validator.rb
84
+ - lib/rails_legit/version.rb
85
+ - rails_legit.gemspec
86
+ - spec/spec_helper.rb
87
+ - spec/support/date.rb
88
+ - spec/verify_date_validator_spec.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.14
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Provides a DSL for common validation formats like Date, Array, DateTime etc.
113
+ test_files:
114
+ - spec/spec_helper.rb
115
+ - spec/support/date.rb
116
+ - spec/verify_date_validator_spec.rb