dynamic_attribute_declaration 0.0.10
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 +7 -0
- data/.gitignore +22 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/dynamic_attribute_declaration.gemspec +30 -0
- data/lib/dynamic_attribute_declaration.rb +89 -0
- data/lib/dynamic_attribute_declaration/version.rb +3 -0
- data/spec/lib/dynamic_attribute_declaration_spec.rb +266 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/support/models.rb +19 -0
- data/spec/support/schema.rb +8 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ad3c08b64a3d53dcc5fd27e5896cb071bb50823
|
4
|
+
data.tar.gz: 8ff5b41d713eb37669edc7f22315fb83050c71bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1881cac321ff34eb75ab1d203f789d5e198f63bed0b31e33e2e4d33ce7625ce58cf37dab8aed481f89741288e9640798ee109ae1da1728bb4152474d8ba57e98
|
7
|
+
data.tar.gz: 8fcb16e746e38c1fa01a5cf9ec75de1055ec2e7eb0c31cb0ff151dc8623906850867345cf77084117f8471b104af693dee1c9fc8d7d2df508c781518112db890
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p481
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Mikkel Wied Frederiksen
|
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,29 @@
|
|
1
|
+
# DynamicAttributeDeclaration
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dynamic_attribute_declaration'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dynamic_attribute_declaration
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/dynamic_attribute_declaration/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dynamic_attribute_declaration/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dynamic_attribute_declaration"
|
8
|
+
spec.version = DynamicAttributeDeclaration::VERSION
|
9
|
+
spec.authors = ["Mikkel Wied Frederiksen"]
|
10
|
+
spec.email = ["mikkel@wied.cc"]
|
11
|
+
spec.summary = %q{This gem lets you dynamically declare validations, that can function as partly applied validation, based on some kind of model instance state.}
|
12
|
+
# spec.description = %q{TODO: Write a longer description. Optional.}
|
13
|
+
spec.homepage = ""
|
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.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "rspec-its"
|
25
|
+
spec.add_development_dependency "sqlite3"
|
26
|
+
spec.add_development_dependency('database_cleaner')
|
27
|
+
|
28
|
+
spec.add_dependency "activerecord", "~> 4.0"
|
29
|
+
spec.add_dependency "activesupport", "~> 4.0"
|
30
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "dynamic_attribute_declaration/version"
|
2
|
+
|
3
|
+
module DynamicAttributeDeclaration
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
class_attribute :_dynamic_attrs
|
8
|
+
class_attribute :_dynamic_attr_state_if
|
9
|
+
self._dynamic_attrs = {}
|
10
|
+
self._dynamic_attr_state_if = Proc.new { false }
|
11
|
+
end
|
12
|
+
|
13
|
+
def values_for attr_name
|
14
|
+
_dynamic_attrs[attr_name][:values] if _dynamic_attrs.key?(attr_name) && _dynamic_attrs[attr_name].key?(:values)
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
|
19
|
+
def inherited(base) #:nodoc:
|
20
|
+
dup = _dynamic_attrs.dup
|
21
|
+
base._dynamic_attrs = dup.each { |k, v| dup[k] = v.dup }
|
22
|
+
base._dynamic_attr_state_if = nil
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
def clear_dynamic_attrs!
|
27
|
+
self._dynamic_attrs = {}
|
28
|
+
self._dynamic_attr_state_if = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_attr_state_if proc
|
32
|
+
throw "define_attr_state_if should be a proc" unless proc.class == Proc
|
33
|
+
self._dynamic_attr_state_if = proc
|
34
|
+
end
|
35
|
+
|
36
|
+
def define_attrs *args
|
37
|
+
attrs = Hash[*args.flatten]
|
38
|
+
self._dynamic_attrs.merge! attrs
|
39
|
+
build_validations_from_dynamic_attrs attrs
|
40
|
+
end
|
41
|
+
|
42
|
+
def attrs_for state=nil, device=nil
|
43
|
+
if state
|
44
|
+
device ||= :desktop
|
45
|
+
_dynamic_attrs.select do |key, val|
|
46
|
+
if val.class == Symbol
|
47
|
+
comparer = val
|
48
|
+
elsif val.respond_to?(:key) && val.key?(:on)
|
49
|
+
comparer = val[:on] if val[:on].class == Symbol
|
50
|
+
comparer = val[:on][device.to_sym] if val[:on].respond_to?(:key) && val[:on].key?(device.to_sym)
|
51
|
+
end
|
52
|
+
|
53
|
+
[*comparer].map(&:to_sym).include? state.to_sym
|
54
|
+
end
|
55
|
+
else
|
56
|
+
_dynamic_attrs
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def attrs_names_for state=nil, device=nil
|
61
|
+
attrs_for(state, device).map(&:first)
|
62
|
+
end
|
63
|
+
|
64
|
+
def build_validations_from_dynamic_attrs attrs
|
65
|
+
# throw "No validation state if defined" unless _rdynamic_attr_state_if
|
66
|
+
attrs.each do |key, val|
|
67
|
+
if val.key?(:validates) && !val[:validates].empty?
|
68
|
+
opts = val[:validates]
|
69
|
+
|
70
|
+
# Check if validation should only be used in specific state
|
71
|
+
if val.key?(:on) && _dynamic_attr_state_if && _dynamic_attr_state_if.class == Proc
|
72
|
+
validates_on = val[:on]
|
73
|
+
# If validates contains if statement, wrap that statement in state check
|
74
|
+
if val[:validates].key?(:if)
|
75
|
+
original_if = val[:validates][:if]
|
76
|
+
opts.merge! if: ->(model) { model.instance_exec(validates_on, &_dynamic_attr_state_if) && model.instance_eval(&original_if) }
|
77
|
+
else
|
78
|
+
opts.merge! if: ->(model) { model.instance_exec(validates_on, &_dynamic_attr_state_if) }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
validates key.to_sym, opts.deep_symbolize_keys()
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
ActiveRecord::Base.send(:include, DynamicAttributeDeclaration)
|
@@ -0,0 +1,266 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Dynamic Attribute Declaration" do
|
4
|
+
|
5
|
+
let(:cls) { Phone }
|
6
|
+
|
7
|
+
describe "Basic gem tests" do
|
8
|
+
|
9
|
+
it "Accessor _dynamic_attrs should be Hash" do
|
10
|
+
expect(cls._dynamic_attrs.class).to eq Hash
|
11
|
+
end
|
12
|
+
|
13
|
+
%w(define_attrs attrs_for attrs_names_for build_validations_from_dynamic_attrs).each do |attr|
|
14
|
+
it "Should respond to #{attr}" do
|
15
|
+
expect(cls).to respond_to(attr.to_sym)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "Defining attrs" do
|
21
|
+
|
22
|
+
it "Clean Model should have no validators" do
|
23
|
+
expect(cls.validators).to eq []
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "Simple definition of presence validator" do
|
27
|
+
let(:defition_array) { [{name:{validates:{presence: true}}}] }
|
28
|
+
let(:definition) { Hash[*defition_array.flatten] }
|
29
|
+
|
30
|
+
before do
|
31
|
+
cls.define_attrs definition
|
32
|
+
end
|
33
|
+
|
34
|
+
it "Should have at least one validator" do
|
35
|
+
expect(cls.validators).not_to eq []
|
36
|
+
expect(cls.validators.length).to be >= 1
|
37
|
+
end
|
38
|
+
|
39
|
+
it "Should have a specific validator" do
|
40
|
+
obj = definition
|
41
|
+
name = obj.keys.first
|
42
|
+
validators = obj[name][:validates]
|
43
|
+
validator_type = validators.keys.first
|
44
|
+
|
45
|
+
validator = cls.validators.first
|
46
|
+
expect(validator.class).to eq "ActiveRecord::Validations::#{validator_type.capitalize}Validator".constantize
|
47
|
+
end
|
48
|
+
|
49
|
+
it "_dynamic_attrs should have configuration from define_attrs" do
|
50
|
+
expect(cls._dynamic_attrs).to eq definition
|
51
|
+
end
|
52
|
+
|
53
|
+
it "attrs_for with no state should return definition" do
|
54
|
+
expect(cls.attrs_for).to eq definition
|
55
|
+
end
|
56
|
+
|
57
|
+
it "attrs_names_for with no state should return definition" do
|
58
|
+
expect(cls.attrs_names_for).to eq definition.keys
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "Simple definition of presence validator, with on parameter" do
|
63
|
+
let(:defition_array) { [{name:{validates:{presence: true}, on: :right}}] }
|
64
|
+
let(:definition) { Hash[*defition_array.flatten] }
|
65
|
+
let(:instance) { cls.new }
|
66
|
+
|
67
|
+
before do
|
68
|
+
cls.define_attrs definition
|
69
|
+
end
|
70
|
+
|
71
|
+
it "Should have at least one validator" do
|
72
|
+
expect(cls.validators).not_to eq []
|
73
|
+
expect(cls.validators.length).to be >= 1
|
74
|
+
end
|
75
|
+
|
76
|
+
it "Should have a specific validator" do
|
77
|
+
obj = definition
|
78
|
+
name = obj.keys.first
|
79
|
+
validators = obj[name][:validates]
|
80
|
+
validator_type = validators.keys.first
|
81
|
+
|
82
|
+
validator = cls.validators.first
|
83
|
+
expect(validator.class).to eq "ActiveRecord::Validations::#{validator_type.capitalize}Validator".constantize
|
84
|
+
end
|
85
|
+
|
86
|
+
it "_dynamic_attrs should have configuration from define_attrs" do
|
87
|
+
expect(cls._dynamic_attrs).to eq definition
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "attrs_for" do
|
91
|
+
it "with no state should return definition" do
|
92
|
+
expect(cls.attrs_for).to eq definition
|
93
|
+
end
|
94
|
+
describe "with right state" do
|
95
|
+
it "with symbol should return definition" do
|
96
|
+
expect(cls.attrs_for(:right)).to eq definition
|
97
|
+
end
|
98
|
+
it "with string should return definition" do
|
99
|
+
expect(cls.attrs_for("right")).to eq definition
|
100
|
+
end
|
101
|
+
end
|
102
|
+
describe "with wrong state" do
|
103
|
+
it "with symbol should return empty object" do
|
104
|
+
expect(cls.attrs_for(:wrong)).to eq({})
|
105
|
+
end
|
106
|
+
it "with string should return empty object" do
|
107
|
+
expect(cls.attrs_for("wrong")).to eq({})
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "attrs_names_for" do
|
113
|
+
it "with no state should return definition keys" do
|
114
|
+
expect(cls.attrs_names_for).to eq definition.keys
|
115
|
+
end
|
116
|
+
describe "with right state" do
|
117
|
+
it "with symbol should return definition keys" do
|
118
|
+
expect(cls.attrs_names_for(:right)).to eq definition.keys
|
119
|
+
end
|
120
|
+
it "with string should return definition keys" do
|
121
|
+
expect(cls.attrs_names_for("right")).to eq definition.keys
|
122
|
+
end
|
123
|
+
end
|
124
|
+
describe "with wrong state" do
|
125
|
+
it "with symbol should return empty array" do
|
126
|
+
expect(cls.attrs_names_for(:wrong)).to eq []
|
127
|
+
end
|
128
|
+
it "with string should return empty array" do
|
129
|
+
expect(cls.attrs_names_for("wrong")).to eq []
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe "values_for" do
|
135
|
+
pending "TEST VALUES FOR"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "Defining define_attr_state_if" do
|
141
|
+
before do
|
142
|
+
cls.clear_dynamic_attrs!
|
143
|
+
end
|
144
|
+
|
145
|
+
it "Should have no _dynamic_attr_state_if as standard" do
|
146
|
+
expect(cls._dynamic_attr_state_if).to be_nil
|
147
|
+
end
|
148
|
+
|
149
|
+
it "lala" do
|
150
|
+
proc = Proc.new { true }
|
151
|
+
cls.define_attr_state_if proc
|
152
|
+
expect(cls._dynamic_attr_state_if).to eq proc
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe "Model Instance" do
|
157
|
+
let(:cls) { Phone }
|
158
|
+
|
159
|
+
describe "With no validator" do
|
160
|
+
let(:instance) { cls.new }
|
161
|
+
|
162
|
+
before do
|
163
|
+
cls.clear_validators!
|
164
|
+
end
|
165
|
+
|
166
|
+
it "Should be valid" do
|
167
|
+
instance.valid?
|
168
|
+
expect(instance.valid?).to eq true
|
169
|
+
expect(instance.errors.full_messages).to eq []
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe "With validator" do
|
174
|
+
let(:defition_array) { [{name:{validates:{presence: true}, on: :right}}] }
|
175
|
+
let(:definition) { Hash[*defition_array.flatten] }
|
176
|
+
|
177
|
+
before do
|
178
|
+
cls.clear_validators!
|
179
|
+
cls.clear_dynamic_attrs!
|
180
|
+
|
181
|
+
cls.define_attr_state_if Proc.new { |value|
|
182
|
+
self.validator value
|
183
|
+
}
|
184
|
+
cls.define_attrs definition
|
185
|
+
end
|
186
|
+
|
187
|
+
describe "With rigth validatable state" do
|
188
|
+
let(:instance) { cls.new }
|
189
|
+
|
190
|
+
before do
|
191
|
+
instance.state = :right
|
192
|
+
end
|
193
|
+
|
194
|
+
it "Should not be valid when having no value" do
|
195
|
+
instance.valid?
|
196
|
+
expect(instance.valid?).to eq false
|
197
|
+
expect(instance.errors.full_messages).not_to eq []
|
198
|
+
end
|
199
|
+
it "Should be valid when having a value" do
|
200
|
+
instance.name = 'My Phone'
|
201
|
+
instance.valid?
|
202
|
+
expect(instance.valid?).to eq true
|
203
|
+
expect(instance.errors.full_messages).to eq []
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
describe "With wrong validatable state" do
|
208
|
+
let(:instance) { cls.new }
|
209
|
+
|
210
|
+
before do
|
211
|
+
instance.state = :wrong
|
212
|
+
end
|
213
|
+
|
214
|
+
it "Should be valid when having no value" do
|
215
|
+
instance.valid?
|
216
|
+
expect(instance.valid?).to eq true
|
217
|
+
expect(instance.errors.full_messages).to eq []
|
218
|
+
end
|
219
|
+
it "Should be valid when having a value" do
|
220
|
+
instance.name = 'My Phone'
|
221
|
+
instance.valid?
|
222
|
+
expect(instance.valid?).to eq true
|
223
|
+
expect(instance.errors.full_messages).to eq []
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
describe "With no validatable state" do
|
228
|
+
let(:instance) { cls.new }
|
229
|
+
|
230
|
+
it "Should be valid when having no value" do
|
231
|
+
instance.valid?
|
232
|
+
expect(instance.valid?).to eq true
|
233
|
+
expect(instance.errors.full_messages).to eq []
|
234
|
+
end
|
235
|
+
it "Should be valid when having a value" do
|
236
|
+
instance.name = 'My Phone'
|
237
|
+
instance.valid?
|
238
|
+
expect(instance.valid?).to eq true
|
239
|
+
expect(instance.errors.full_messages).to eq []
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe "Multiple models" do
|
246
|
+
|
247
|
+
before do
|
248
|
+
[Phone, Car].each do |model|
|
249
|
+
model.clear_validators!
|
250
|
+
model.clear_dynamic_attrs!
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
it "_dynamic_attrs should be different" do
|
255
|
+
expect(Phone._dynamic_attrs).to eq({})
|
256
|
+
expect(Car._dynamic_attrs).to eq({})
|
257
|
+
|
258
|
+
Phone.define_attrs [{name:{validates:{presence: true}, on: :right}}]
|
259
|
+
Car.define_attrs [{name:{validates:{presence: true}, on: :another}}]
|
260
|
+
expect(Phone._dynamic_attrs).not_to eq({})
|
261
|
+
expect(Car._dynamic_attrs).not_to eq({})
|
262
|
+
|
263
|
+
expect(Phone._dynamic_attrs).not_to eq Car._dynamic_attrs
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'database_cleaner'
|
3
|
+
require 'rspec/its'
|
4
|
+
|
5
|
+
# Initialize AR connection
|
6
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
7
|
+
# Require dynamic_attribute_declaration gem
|
8
|
+
require 'dynamic_attribute_declaration'
|
9
|
+
# Load and build test models
|
10
|
+
require 'support/models'
|
11
|
+
|
12
|
+
# Configure RSpec
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.before(:suite) do
|
15
|
+
DatabaseCleaner.strategy = :transaction
|
16
|
+
DatabaseCleaner.clean_with(:truncation)
|
17
|
+
end
|
18
|
+
|
19
|
+
config.before(:each) do
|
20
|
+
DatabaseCleaner.start
|
21
|
+
end
|
22
|
+
|
23
|
+
config.after(:each) do
|
24
|
+
DatabaseCleaner.clean
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
load File.dirname(__FILE__) + '/schema.rb'
|
4
|
+
|
5
|
+
class Phone < ActiveRecord::Base
|
6
|
+
attr_accessor :state
|
7
|
+
|
8
|
+
def validator value
|
9
|
+
state == value
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Car < ActiveRecord::Base
|
14
|
+
attr_accessor :state
|
15
|
+
|
16
|
+
def validator value
|
17
|
+
state == value
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynamic_attribute_declaration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.10
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mikkel Wied Frederiksen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-01 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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: rspec
|
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-its
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: database_cleaner
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activerecord
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4.0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '4.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activesupport
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '4.0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- mikkel@wied.cc
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".ruby-version"
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- dynamic_attribute_declaration.gemspec
|
140
|
+
- lib/dynamic_attribute_declaration.rb
|
141
|
+
- lib/dynamic_attribute_declaration/version.rb
|
142
|
+
- spec/lib/dynamic_attribute_declaration_spec.rb
|
143
|
+
- spec/spec_helper.rb
|
144
|
+
- spec/support/models.rb
|
145
|
+
- spec/support/schema.rb
|
146
|
+
homepage: ''
|
147
|
+
licenses:
|
148
|
+
- MIT
|
149
|
+
metadata: {}
|
150
|
+
post_install_message:
|
151
|
+
rdoc_options: []
|
152
|
+
require_paths:
|
153
|
+
- lib
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
requirements: []
|
165
|
+
rubyforge_project:
|
166
|
+
rubygems_version: 2.3.0
|
167
|
+
signing_key:
|
168
|
+
specification_version: 4
|
169
|
+
summary: This gem lets you dynamically declare validations, that can function as partly
|
170
|
+
applied validation, based on some kind of model instance state.
|
171
|
+
test_files:
|
172
|
+
- spec/lib/dynamic_attribute_declaration_spec.rb
|
173
|
+
- spec/spec_helper.rb
|
174
|
+
- spec/support/models.rb
|
175
|
+
- spec/support/schema.rb
|