sexy_validators 0.0.1 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ class ChangeValidator < ActiveModel::EachValidator
2
+
3
+ # Enforce/prevent attribute change
4
+ #
5
+ # Example: Make attribute immutable once saved
6
+ # validates :attribute, change: false, on: :update
7
+ #
8
+ # Example: Force attribute change on every save
9
+ # validates :attribute, change: true
10
+
11
+ def initialize(options)
12
+ options[:change] = !(options[:with] === false)
13
+ super
14
+ end
15
+
16
+ def validate_each(record, attribute, value)
17
+ unless record.public_send(:"#{attribute}_changed?") == options[:change]
18
+ record.errors[attribute] << ("#{options[:change] ? 'must' : 'cannot'} be modified")
19
+ end
20
+ end
21
+
22
+ end
@@ -1,5 +1,7 @@
1
1
  require "sexy_validators/version"
2
+ require "slug_validator"
3
+ require "change_validator"
2
4
 
3
5
  module SexyValidators
4
6
  # Your code goes here...
5
- end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module SexyValidators
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,7 @@
1
+ class SlugValidator < ActiveModel::EachValidator
2
+ def validate_each(object, attribute, value)
3
+ unless value =~ /^\D(\w*-*)*$/
4
+ object.errors[attribute] << (options[:message] || "is not formatted properly (should contain only letters, digits, dashes and underscores and can't start with digit)")
5
+ end
6
+ end
7
+ end
@@ -14,4 +14,5 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "sexy_validators"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = SexyValidators::VERSION
17
- end
17
+ gem.add_dependency("activemodel", ">=0")
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sexy_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-11-03 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
14
30
  description: Use it as your validators.
15
31
  email:
16
32
  - maks.rydkin@gmail.com
@@ -23,8 +39,10 @@ files:
23
39
  - LICENSE
24
40
  - README.md
25
41
  - Rakefile
42
+ - lib/change_validator.rb
26
43
  - lib/sexy_validators.rb
27
44
  - lib/sexy_validators/version.rb
45
+ - lib/slug_validator.rb
28
46
  - sexy_validators.gemspec
29
47
  homepage: ''
30
48
  licenses: []