modelish 0.1.0
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/.gitignore +8 -0
- data/.rspec +1 -0
- data/.rvmrc +3 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.md +95 -0
- data/Rakefile +17 -0
- data/lib/modelish.rb +8 -0
- data/lib/modelish/base.rb +44 -0
- data/lib/modelish/property_types.rb +133 -0
- data/lib/modelish/validations.rb +208 -0
- data/lib/modelish/version.rb +3 -0
- data/modelish.gemspec +30 -0
- data/spec/modelish/base_spec.rb +285 -0
- data/spec/modelish/property_types_spec.rb +128 -0
- data/spec/modelish/validations_spec.rb +695 -0
- data/spec/modelish_spec.rb +7 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/property_examples.rb +38 -0
- data/spec/support/typed_property_examples.rb +90 -0
- data/spec/support/validation_examples.rb +70 -0
- metadata +161 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Assumes the following have been defined
|
2
|
+
# let(:model) -- with a model initializer that calls init_options
|
3
|
+
# let(:property_name)
|
4
|
+
# let(:property_value)
|
5
|
+
# let(:default_value)
|
6
|
+
shared_examples_for 'a modelish property' do
|
7
|
+
subject { model }
|
8
|
+
|
9
|
+
let(:init_options) { {property_name => property_value} }
|
10
|
+
|
11
|
+
it { should respond_to(property_name) }
|
12
|
+
|
13
|
+
describe "getter" do
|
14
|
+
subject { model.send(property_name) }
|
15
|
+
|
16
|
+
context "without init options" do
|
17
|
+
let(:init_options) { nil }
|
18
|
+
|
19
|
+
it { should == default_value }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "with init options" do
|
23
|
+
it { should == property_value }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it { should respond_to("#{property_name}=".to_sym) }
|
28
|
+
|
29
|
+
describe "setter" do
|
30
|
+
subject { model.send("#{property_name}=", new_property_value) }
|
31
|
+
|
32
|
+
let(:new_property_value) { 'a new value' }
|
33
|
+
|
34
|
+
it "should change the property value" do
|
35
|
+
expect { subject }.to change{model.send(property_name)}.from(property_value).to(new_property_value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# Assume the following are defined:
|
2
|
+
# model - the model on which the property is defined
|
3
|
+
# valid_string - a string with a valid value for the property
|
4
|
+
# valid_typed_value - an instance of property_type that is valid for the property
|
5
|
+
# invalid_value - an object that cannot be translated to the property type
|
6
|
+
# default_value - the default value for the property
|
7
|
+
shared_examples_for "a typed property" do |prop_name, prop_type|
|
8
|
+
accessor = prop_name.to_sym
|
9
|
+
raw_accessor = "raw_#{prop_name}".to_sym
|
10
|
+
bang_accessor = "#{prop_name}!".to_sym
|
11
|
+
mutator = "#{prop_name}=".to_sym
|
12
|
+
|
13
|
+
subject { model }
|
14
|
+
|
15
|
+
it { should respond_to(accessor) }
|
16
|
+
its(prop_name) { should == default_value }
|
17
|
+
|
18
|
+
it { should respond_to(raw_accessor) }
|
19
|
+
its(raw_accessor) { should == default_value }
|
20
|
+
|
21
|
+
it { should respond_to(bang_accessor) }
|
22
|
+
its(bang_accessor) { should == default_value }
|
23
|
+
|
24
|
+
it { should respond_to(mutator) }
|
25
|
+
|
26
|
+
describe "property_type mapping" do
|
27
|
+
subject { model.class.property_types }
|
28
|
+
|
29
|
+
it { should be }
|
30
|
+
it { should have_key(prop_name) }
|
31
|
+
|
32
|
+
its([prop_name]) { should == property_type }
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "assignment" do
|
36
|
+
before { model.send(mutator, property_value) }
|
37
|
+
|
38
|
+
context "with nil" do
|
39
|
+
let(:property_value) { nil }
|
40
|
+
|
41
|
+
its(accessor) { should be_nil }
|
42
|
+
its(raw_accessor) { should be_nil }
|
43
|
+
|
44
|
+
it "should not raise an error when the bang accessor is invoked" do
|
45
|
+
expect { subject.send(bang_accessor) }.to_not raise_error
|
46
|
+
end
|
47
|
+
|
48
|
+
its(bang_accessor) { should be_nil }
|
49
|
+
end
|
50
|
+
|
51
|
+
context "with valid string" do
|
52
|
+
let(:property_value) { valid_string }
|
53
|
+
|
54
|
+
its(accessor) { should == valid_typed_value }
|
55
|
+
its(raw_accessor) { should == property_value }
|
56
|
+
|
57
|
+
it "should not raise an error when the bang accessor is invoked" do
|
58
|
+
expect { subject.send(bang_accessor) }.to_not raise_error
|
59
|
+
end
|
60
|
+
|
61
|
+
its(bang_accessor) { should == valid_typed_value }
|
62
|
+
end
|
63
|
+
|
64
|
+
context "with valid typed value" do
|
65
|
+
let(:property_value) { valid_typed_value }
|
66
|
+
|
67
|
+
its(accessor) { should == property_value }
|
68
|
+
its(raw_accessor) { should == property_value }
|
69
|
+
|
70
|
+
it "should not raise an error when the bang accessor is invoked" do
|
71
|
+
expect { subject.send(bang_accessor) }.to_not raise_error
|
72
|
+
end
|
73
|
+
|
74
|
+
its(bang_accessor) { should == valid_typed_value }
|
75
|
+
end
|
76
|
+
|
77
|
+
unless [String, Array, Symbol].include?(prop_type)
|
78
|
+
context "with a value that cannot be converted" do
|
79
|
+
let(:property_value) { invalid_value }
|
80
|
+
|
81
|
+
its(accessor) { should == property_value }
|
82
|
+
its(raw_accessor) { should == property_value }
|
83
|
+
|
84
|
+
it "should raise an error when the bang accessor is invoked" do
|
85
|
+
expect { subject.send(bang_accessor) } .to raise_error
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# Assumes that let(:model) has been defined
|
2
|
+
shared_examples_for 'a valid model' do
|
3
|
+
subject { model }
|
4
|
+
|
5
|
+
it { should be_valid }
|
6
|
+
|
7
|
+
it { should respond_to(:validate) }
|
8
|
+
|
9
|
+
describe "validate" do
|
10
|
+
subject { model.validate }
|
11
|
+
|
12
|
+
it { should be_empty }
|
13
|
+
end
|
14
|
+
|
15
|
+
it { should respond_to(:validate!) }
|
16
|
+
|
17
|
+
describe "validate!" do
|
18
|
+
subject { model.validate! }
|
19
|
+
|
20
|
+
it "should not raise any errors" do
|
21
|
+
expect { subject }.to_not raise_error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Assumes the following let statements have been defined
|
27
|
+
# model - the model on which the property has been defined
|
28
|
+
# property_name - the name of the property that is invalid
|
29
|
+
# error_count - the number of expected errors on the property
|
30
|
+
shared_examples_for 'a model with an invalid property' do
|
31
|
+
subject { model }
|
32
|
+
|
33
|
+
it { should_not be_valid }
|
34
|
+
|
35
|
+
it { should respond_to(:validate) }
|
36
|
+
|
37
|
+
describe "validate" do
|
38
|
+
subject { errors }
|
39
|
+
let(:errors) { model.validate }
|
40
|
+
|
41
|
+
it { should have_key(property_name) }
|
42
|
+
|
43
|
+
describe "[property_name]" do
|
44
|
+
subject { prop_errors }
|
45
|
+
let(:prop_errors) { errors[property_name] }
|
46
|
+
|
47
|
+
it { should have(error_count).errors }
|
48
|
+
|
49
|
+
it "should be a collection of ArgumentErrors" do
|
50
|
+
prop_errors.each { |p| p.should be_an ArgumentError }
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should reference the property name in the error message(s)" do
|
54
|
+
prop_errors.each { |p| p.message.should match(/#{property_name}/i) }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "validate!" do
|
60
|
+
subject { model.validate! }
|
61
|
+
|
62
|
+
it "should raise an ArgumentError" do
|
63
|
+
expect { subject }.to raise_error(ArgumentError)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should reference the property name in the error message" do
|
67
|
+
expect { subject }.to raise_error { |e| e.message.should match(/#{property_name}/i) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: modelish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Maeve Revels
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-03-28 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: hashie
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
version: "1.0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 2
|
44
|
+
- 5
|
45
|
+
version: "2.5"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: yard
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
- 6
|
59
|
+
version: "0.6"
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bluecloth
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 2
|
72
|
+
- 0
|
73
|
+
- 11
|
74
|
+
version: 2.0.11
|
75
|
+
type: :development
|
76
|
+
version_requirements: *id004
|
77
|
+
- !ruby/object:Gem::Dependency
|
78
|
+
name: autotest
|
79
|
+
prerelease: false
|
80
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
type: :development
|
89
|
+
version_requirements: *id005
|
90
|
+
description: Sometimes you need something just a little modelish.
|
91
|
+
email:
|
92
|
+
- maeve.revels@g5platform.com
|
93
|
+
executables: []
|
94
|
+
|
95
|
+
extensions: []
|
96
|
+
|
97
|
+
extra_rdoc_files: []
|
98
|
+
|
99
|
+
files:
|
100
|
+
- .gitignore
|
101
|
+
- .rspec
|
102
|
+
- .rvmrc
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/modelish.rb
|
108
|
+
- lib/modelish/base.rb
|
109
|
+
- lib/modelish/property_types.rb
|
110
|
+
- lib/modelish/validations.rb
|
111
|
+
- lib/modelish/version.rb
|
112
|
+
- modelish.gemspec
|
113
|
+
- spec/modelish/base_spec.rb
|
114
|
+
- spec/modelish/property_types_spec.rb
|
115
|
+
- spec/modelish/validations_spec.rb
|
116
|
+
- spec/modelish_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- spec/support/property_examples.rb
|
119
|
+
- spec/support/typed_property_examples.rb
|
120
|
+
- spec/support/validation_examples.rb
|
121
|
+
has_rdoc: true
|
122
|
+
homepage: http://github.com/maeve/modelish
|
123
|
+
licenses: []
|
124
|
+
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
version: "0"
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
segments:
|
144
|
+
- 0
|
145
|
+
version: "0"
|
146
|
+
requirements: []
|
147
|
+
|
148
|
+
rubyforge_project: modelish
|
149
|
+
rubygems_version: 1.3.7
|
150
|
+
signing_key:
|
151
|
+
specification_version: 3
|
152
|
+
summary: A lightweight pseudo-modeling not-quite-framework
|
153
|
+
test_files:
|
154
|
+
- spec/modelish/base_spec.rb
|
155
|
+
- spec/modelish/property_types_spec.rb
|
156
|
+
- spec/modelish/validations_spec.rb
|
157
|
+
- spec/modelish_spec.rb
|
158
|
+
- spec/spec_helper.rb
|
159
|
+
- spec/support/property_examples.rb
|
160
|
+
- spec/support/typed_property_examples.rb
|
161
|
+
- spec/support/validation_examples.rb
|