activemodel-immutable_validator 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: a28070427a356f08f0b5d11d9921cbc2b4d3fc35
4
+ data.tar.gz: 7993367c79961c278fffc382a0331daf3a256395
5
+ SHA512:
6
+ metadata.gz: 610077de9c4bc14b48b1e196d371121cb0724508baf059214bace8ad09cac09d3d4951ff287f0a20e24144743ca451237527b47032f7ef771227700514f58e63
7
+ data.tar.gz: e4898cc2bea700464f5262f8a172f91befc9fc832b7ec2533dbc90a72d15e1bc595714df8b5c7da1bb204c81c4d76efb8cb321b54994e1837e0f1cbf8ff69414
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --require spec_helper
2
+ --color
3
+ --order random
4
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activemodel-immutable_validator.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Yuku TAKAHASHI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # activemodel-immutable_validator
2
+
3
+ [![Build Status](https://travis-ci.org/yuku-t/activemodel-immutable_validator.svg?branch=master)](https://travis-ci.org/yuku-t/activemodel-immutable_validator) [![Code Climate](https://codeclimate.com/github/yuku-t/activemodel-immutable_validator/badges/gpa.svg)](https://codeclimate.com/github/yuku-t/activemodel-immutable_validator) [![Coverage Status](https://coveralls.io/repos/yuku-t/activemodel-immutable_validator/badge.svg)](https://coveralls.io/r/yuku-t/activemodel-immutable_validator)
4
+
5
+ Validate immutable attributes.
6
+
7
+ ## Usage
8
+
9
+ Add to your Gemfile:
10
+
11
+ ```rb
12
+ gem 'activemodel-immutable_validator'
13
+ ```
14
+
15
+ Run:
16
+
17
+ ```
18
+ bundle install
19
+ ```
20
+
21
+ Then add the followng to your model which belongs to a user:
22
+
23
+ ```rb
24
+ validates :attr, immutable: true
25
+ ```
26
+
27
+ ### Sample
28
+
29
+ A human cannot become a bot vice versa.
30
+
31
+ ```rb
32
+ class User < ActiveRecord::Base
33
+ validated :type, immutable: true
34
+ end
35
+
36
+ class Bot < User
37
+ end
38
+
39
+ class Human < User
40
+ end
41
+ ```
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task default: :spec
@@ -0,0 +1,22 @@
1
+ file = File.open(File.expand_path('../lib/immutable_validator/version.rb', __FILE__))
2
+ version = file.read.scan(/\d+\.\d+\.\d+/).first
3
+ file.close
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activemodel-immutable_validator'
7
+ spec.version = version
8
+ spec.authors = ['Yuku Takahashi']
9
+ spec.email = ['taka849u@gmail.com']
10
+ spec.summary = 'A Rails validator for immutable attributes.'
11
+ spec.homepage = 'https://github.com/yuku-t/activemodel-immutable_validator'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_dependency 'activemodel'
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.7'
20
+ spec.add_development_dependency 'rake', '~> 10.0'
21
+ spec.add_development_dependency 'rspec'
22
+ end
@@ -0,0 +1,3 @@
1
+ require 'immutable_validator'
2
+ require 'immutable_validator/version'
3
+ require 'immutable_validator/engine'
@@ -0,0 +1,7 @@
1
+ class ImmutableValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, _value)
3
+ return if record.new_record?
4
+ return unless record.public_send("#{attribute}_changed?")
5
+ record.errors.add(attribute, options[:message] || :immutable)
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ class ImmutableValidator
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ class IummutableValidator
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,48 @@
1
+ describe ImmutableValidator do
2
+ describe 'validation' do
3
+ subject(:model) do
4
+ model_class.new(attr: value)
5
+ end
6
+
7
+ let(:model_class) do
8
+ Class.new(TestModel) do
9
+ validates :attr, immutable: true
10
+ end
11
+ end
12
+
13
+ let(:value) do
14
+ 'foo'
15
+ end
16
+
17
+ context 'on create' do
18
+ before do
19
+ allow(model).to receive(:new_record?).and_return(true)
20
+ allow(model).to receive(:attr_changed?).and_return(true)
21
+ end
22
+
23
+ it { should be_valid }
24
+ end
25
+
26
+ context 'on update' do
27
+ before do
28
+ allow(model).to receive(:new_record?).and_return(false)
29
+ end
30
+
31
+ context 'with change' do
32
+ before do
33
+ allow(model).to receive(:attr_changed?).and_return(true)
34
+ end
35
+
36
+ it { should be_invalid }
37
+ end
38
+
39
+ context 'without change' do
40
+ before do
41
+ allow(model).to receive(:attr_changed?).and_return(false)
42
+ end
43
+
44
+ it { should be_valid }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,25 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'active_model'
5
+ require 'immutable_validator'
6
+
7
+ RSpec.configure do |config|
8
+ config.run_all_when_everything_filtered = true
9
+ end
10
+
11
+ class TestModel
12
+ include ActiveModel::Validations
13
+
14
+ def self.name
15
+ 'TestModel'
16
+ end
17
+
18
+ def initialize(attributes = {})
19
+ @attributes = attributes
20
+ end
21
+
22
+ def read_attribute_for_validation(key)
23
+ @attributes[key]
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-immutable_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuku Takahashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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:
70
+ email:
71
+ - taka849u@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - activemodel-immutable_validator.gemspec
84
+ - lib/activemodel-immutable_validator.rb
85
+ - lib/immutable_validator.rb
86
+ - lib/immutable_validator/engine.rb
87
+ - lib/immutable_validator/version.rb
88
+ - spec/immutable_validator_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/yuku-t/activemodel-immutable_validator
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A Rails validator for immutable attributes.
114
+ test_files: []
115
+ has_rdoc: