memory_model 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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +1 -0
- data/lib/concerned_inheritance/class_methods.rb +28 -0
- data/lib/concerned_inheritance/delegator.rb +21 -0
- data/lib/concerned_inheritance/module_methods.rb +11 -0
- data/lib/concerned_inheritance.rb +27 -0
- data/lib/memory_model/base/actionable.rb +95 -0
- data/lib/memory_model/base/attributable.rb +76 -0
- data/lib/memory_model/base/collectable.rb +22 -0
- data/lib/memory_model/base/comparable.rb +16 -0
- data/lib/memory_model/base/fieldable/field.rb +35 -0
- data/lib/memory_model/base/fieldable/field_set.rb +74 -0
- data/lib/memory_model/base/fieldable.rb +45 -0
- data/lib/memory_model/base/persistence.rb +15 -0
- data/lib/memory_model/base/versionable.rb +17 -0
- data/lib/memory_model/base.rb +51 -0
- data/lib/memory_model/collection.rb +80 -0
- data/lib/memory_model/core_ext/object.rb +5 -0
- data/lib/memory_model/version.rb +3 -0
- data/lib/memory_model.rb +13 -0
- data/memory_model.gemspec +30 -0
- data/spec/concerned_inheritance/class_methods_spec.rb +57 -0
- data/spec/concerned_inheritance/delegator_spec.rb +52 -0
- data/spec/concerned_inheritance/module_methods_spec.rb +27 -0
- data/spec/memory_model/base/actionable_spec.rb +359 -0
- data/spec/memory_model/base/attributable_spec.rb +143 -0
- data/spec/memory_model/base/collectable_spec.rb +24 -0
- data/spec/memory_model/base/comparable_spec.rb +155 -0
- data/spec/memory_model/base/fieldable/field_set_spec.rb +160 -0
- data/spec/memory_model/base/fieldable/field_spec.rb +96 -0
- data/spec/memory_model/base/fieldable_spec.rb +23 -0
- data/spec/memory_model/base/persistence_spec.rb +37 -0
- data/spec/memory_model/base/versionable_spec.rb +31 -0
- data/spec/memory_model/base_spec.rb +52 -0
- data/spec/memory_model/collection_spec.rb +216 -0
- data/spec/memory_model/concerned_inheritance_spec.rb +24 -0
- data/spec/memory_model/core_ext/object_spec.rb +12 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/active_model_lint.rb +16 -0
- metadata +253 -0
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MemoryModel::Base::Attributable do
|
4
|
+
|
5
|
+
let(:model) do
|
6
|
+
Class.new(MemoryModel::Base) do
|
7
|
+
field :foo
|
8
|
+
field :bar
|
9
|
+
end
|
10
|
+
end
|
11
|
+
let(:instance) do
|
12
|
+
model.new
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#any_field_name' do
|
16
|
+
it 'should read an attribute' do
|
17
|
+
instance.write_attribute :foo, "bar"
|
18
|
+
instance.foo.should == "bar"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#any_field_name=' do
|
23
|
+
it 'should read an attribute' do
|
24
|
+
instance.foo= "bar"
|
25
|
+
instance.read_attribute(:foo).should == "bar"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#has_attribute?' do
|
30
|
+
context 'given nil' do
|
31
|
+
it 'should be false' do
|
32
|
+
instance.foo = nil
|
33
|
+
instance.has_attribute?(:foo).should be_false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'given an empty string' do
|
38
|
+
it 'should be true' do
|
39
|
+
instance.foo = ''
|
40
|
+
instance.has_attribute?(:foo).should be_true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'given a Hash' do
|
45
|
+
it 'should be true' do
|
46
|
+
instance.foo = { foo: :bar }
|
47
|
+
instance.has_attribute?(:foo).should be_true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'given an emtpy Hash' do
|
52
|
+
it 'should be true' do
|
53
|
+
instance.foo = {}
|
54
|
+
instance.has_attribute?(:foo).should be_false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#inspect' do
|
60
|
+
it 'inspect into a readable format' do
|
61
|
+
instance.inspect.should match /#<#{instance.class}/
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should contain the attributes' do
|
65
|
+
instance.attributes.each do |name, value|
|
66
|
+
value = instance.send :attribute_for_inspect, name
|
67
|
+
instance.inspect.should include "#{name}: #{value}" if instance.has_attribute? name
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should read not initialized' do
|
72
|
+
model.allocate.inspect.should match /not initialized/
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#read_attribute' do
|
77
|
+
it 'should return a value' do
|
78
|
+
instance.write_attribute(:foo, "bar")
|
79
|
+
instance.read_attribute(:foo).should == 'bar'
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should return nil if the key doesn't exist" do
|
83
|
+
instance.read_attribute(:bar).should be_nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '#write_attribute' do
|
88
|
+
it 'should raise an error with an invalid field' do
|
89
|
+
expect { instance.write_attribute(:baz, 'razzle') }.to raise_error MemoryModel::InvalidFieldError
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#attribute_for_inspect' do
|
94
|
+
it 'truncates values over 50 chars' do
|
95
|
+
value = instance.foo = 'barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar'
|
96
|
+
instance.send(:attribute_for_inspect, :foo).should_not == value.inspect
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should convert a time to string' do
|
100
|
+
instance.foo = Time.now
|
101
|
+
instance.send(:attribute_for_inspect, :foo).should be_a String
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should convert a date to string' do
|
105
|
+
instance.foo = Date.today
|
106
|
+
instance.send(:attribute_for_inspect, :foo).should be_a String
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'inspects objects' do
|
110
|
+
value = instance.foo = 'bar'
|
111
|
+
instance.send(:attribute_for_inspect, :foo).should == value.inspect
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe '#reset_attribute_to_default' do
|
116
|
+
let(:model) do
|
117
|
+
Class.new(MemoryModel::Base) do
|
118
|
+
field :foo, default: 'bar'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
it 'should reset an attribute to its default value' do
|
122
|
+
instance.foo = 'baz'
|
123
|
+
instance.foo.should == 'baz'
|
124
|
+
instance.reset_foo_to_default!
|
125
|
+
instance.foo.should == 'bar'
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe '#clear_attribute' do
|
130
|
+
let(:model) do
|
131
|
+
Class.new(MemoryModel::Base) do
|
132
|
+
field :foo
|
133
|
+
end
|
134
|
+
end
|
135
|
+
it 'should reset an attribute to its default value' do
|
136
|
+
instance.foo = 'baz'
|
137
|
+
instance.foo.should == 'baz'
|
138
|
+
instance.clear_foo
|
139
|
+
instance.foo.should be_nil
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MemoryModel::Base::Collectable do
|
4
|
+
|
5
|
+
let(:model) do
|
6
|
+
Class.new(MemoryModel::Base) do
|
7
|
+
field :foo
|
8
|
+
field :bar
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '.collection' do
|
13
|
+
it 'should be a collection' do
|
14
|
+
model.collection.should be_a MemoryModel::Collection
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.inherited' do
|
19
|
+
it 'should use its parents collection' do
|
20
|
+
Class.new(model).collection.should == model.collection
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MemoryModel::Base::Comparable do
|
4
|
+
let(:model) do
|
5
|
+
Class.new(MemoryModel::Base) do
|
6
|
+
field :foo
|
7
|
+
end
|
8
|
+
end
|
9
|
+
let(:value) { 'bar' }
|
10
|
+
subject(:instance) { model.new(foo: value) }
|
11
|
+
|
12
|
+
describe '#!=' do
|
13
|
+
context 'given a symbolized hash' do
|
14
|
+
let(:valid_hash) { { 'foo' => value } }
|
15
|
+
let(:invalid_hash) { { 'foo' => 'baz' } }
|
16
|
+
it 'should return true when given a valid hash' do
|
17
|
+
(instance != valid_hash).should be_false
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return false when given a invalid hash' do
|
21
|
+
(instance != invalid_hash).should be_true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'given a symbolized hash' do
|
26
|
+
let(:valid_hash) { { foo: value } }
|
27
|
+
let(:invalid_hash) { { foo: 'baz' } }
|
28
|
+
it 'should return true when given a valid hash' do
|
29
|
+
(instance != valid_hash).should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should return false when given a invalid hash' do
|
33
|
+
(instance != invalid_hash).should be_true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'given an instance of the same class' do
|
38
|
+
let(:valid_instance) { model.new(foo: value) }
|
39
|
+
let(:invalid_instance) { model.new(foo: 'baz') }
|
40
|
+
it 'should be true when given a valid instance' do
|
41
|
+
(instance != valid_instance).should be_false
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should be false when given a invalid instance' do
|
45
|
+
(instance != invalid_instance).should be_true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'given an instance of a different class' do
|
50
|
+
let(:other_class) { Class.new(MemoryModel::Base) }
|
51
|
+
let(:valid_instance) { other_class.new(foo: value) }
|
52
|
+
let(:invalid_instance) { other_class.new(foo: 'baz') }
|
53
|
+
it 'should be true when given a valid instance' do
|
54
|
+
(instance != valid_instance).should be_false
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should be false when given a invalid instance' do
|
58
|
+
(instance != invalid_instance).should be_true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '#==' do
|
64
|
+
context 'given a symbolized hash' do
|
65
|
+
let(:valid_hash) { { 'foo' => value } }
|
66
|
+
let(:invalid_hash) { { 'foo' => 'baz' } }
|
67
|
+
it 'should return true when given a valid hash' do
|
68
|
+
(instance == valid_hash).should be_true
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should return false when given a invalid hash' do
|
72
|
+
(instance == invalid_hash).should be_false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'given a symbolized hash' do
|
77
|
+
let(:valid_hash) { { foo: value } }
|
78
|
+
let(:invalid_hash) { { foo: 'baz' } }
|
79
|
+
it 'should return true when given a valid hash' do
|
80
|
+
(instance == valid_hash).should be_true
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should return false when given a invalid hash' do
|
84
|
+
(instance == invalid_hash).should be_false
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'given an instance of the same class' do
|
89
|
+
let(:valid_instance) { model.new(foo: value) }
|
90
|
+
let(:invalid_instance) { model.new(foo: 'baz') }
|
91
|
+
it 'should be true when given a valid instance' do
|
92
|
+
(instance == valid_instance).should be_true
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should be false when given a invalid instance' do
|
96
|
+
(instance == invalid_instance).should be_false
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'given an instance of a different class' do
|
101
|
+
let(:other_class) { Class.new(MemoryModel::Base) }
|
102
|
+
let(:valid_instance) { other_class.new(foo: value) }
|
103
|
+
let(:invalid_instance) { other_class.new(foo: 'baz') }
|
104
|
+
it 'should be true when given a valid instance' do
|
105
|
+
(instance == valid_instance).should be_true
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'should be false when given a invalid instance' do
|
109
|
+
(instance == invalid_instance).should be_false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#===' do
|
115
|
+
context 'given a hash' do
|
116
|
+
it 'should return false' do
|
117
|
+
hash = { foo: value }
|
118
|
+
(instance === hash).should be_false
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'given an instance of the same class' do
|
123
|
+
let(:valid_instance) { model.new(foo: value) }
|
124
|
+
let(:invalid_instance) { model.new(foo: 'baz') }
|
125
|
+
it 'should be true when given a valid instance' do
|
126
|
+
(instance === valid_instance).should be_true
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'should be false when given a invalid instance' do
|
130
|
+
(instance === invalid_instance).should be_false
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'given an instance of an inherited class' do
|
135
|
+
let(:inherited_model) { Class.new(model) }
|
136
|
+
let(:valid_instance) { inherited_model.new(foo: value) }
|
137
|
+
let(:invalid_instance) { inherited_model.new(foo: 'baz') }
|
138
|
+
it 'should be true when given a valid instance' do
|
139
|
+
(instance === valid_instance).should be_true
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should be false when given a invalid instance' do
|
143
|
+
(instance === invalid_instance).should be_false
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'given an instance of a different class' do
|
148
|
+
let(:other_class) { Class.new(MemoryModel::Base) }
|
149
|
+
let(:other_instance) { other_class.new(foo: value) }
|
150
|
+
it 'should be false' do
|
151
|
+
(instance === other_instance).should be_false
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MemoryModel::Base::Fieldable::FieldSet do
|
4
|
+
|
5
|
+
let(:klass) { MemoryModel::Base::Fieldable::FieldSet }
|
6
|
+
subject(:field_set) { klass.new }
|
7
|
+
|
8
|
+
describe '.new' do
|
9
|
+
it "should have an empty array of fields" do
|
10
|
+
field_set.instance_variable_get(:@fields).should be_a Array
|
11
|
+
field_set.size.should == 0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#[]' do
|
16
|
+
it "should return a field" do
|
17
|
+
field_set.add(:foo)
|
18
|
+
field_set[:foo].should be_present
|
19
|
+
field_set[:foo].name.should == :foo
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#<<' do
|
24
|
+
it 'should add a field with the symbol' do
|
25
|
+
expect { field_set << :foo }.to change { field_set.fields }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#add' do
|
30
|
+
it "should add a field" do
|
31
|
+
expect { field_set.add(:foo) }.to change { field_set.fields }
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should add a field with options" do
|
35
|
+
options = { foo: :bar }
|
36
|
+
expect { field_set.add(:foo, options) }.to change { field_set.fields }
|
37
|
+
field_set[:foo].options[:foo].should == :bar
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#comparable' do
|
42
|
+
it 'should only return comparable fields' do
|
43
|
+
field_set.add(:foo, comparable: true)
|
44
|
+
field_set.add(:bar, comparable: false)
|
45
|
+
field_set.comparable.each do |field|
|
46
|
+
field_set[field].should be_comparable
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#inspect' do
|
52
|
+
it 'should delegate inspect to #to_a' do
|
53
|
+
names_mock = mock
|
54
|
+
names_mock.should_receive(:inspect)
|
55
|
+
field_set.stub(:to_a).and_return(names_mock)
|
56
|
+
field_set.inspect
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#default_values' do
|
61
|
+
let(:mock_model) { mock }
|
62
|
+
|
63
|
+
context 'with a symbol' do
|
64
|
+
it 'should call the method on the model' do
|
65
|
+
mock_model.should_receive :foo_val
|
66
|
+
field_set.add :foo, default: :foo_val
|
67
|
+
field_set.default_values(mock_model)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'with a string' do
|
72
|
+
it 'should set the string' do
|
73
|
+
field_set.add :foo, default: 'foo_val'
|
74
|
+
defaults = field_set.default_values(mock_model)
|
75
|
+
defaults[:foo].should == 'foo_val'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'with a lambda with an arity of 0' do
|
80
|
+
it 'should evaluate the block' do
|
81
|
+
field_set.add :foo, default: -> { 5 + 5 }
|
82
|
+
field_set.default_values(mock_model)[:foo].should == 10
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'with a lambda with an arity of 1' do
|
87
|
+
it 'should evaluate the block' do
|
88
|
+
mock_model.should_receive :foo_val
|
89
|
+
field_set.add :foo, default: ->(model) { model.foo_val }
|
90
|
+
field_set.default_values(mock_model)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'with a lambda with an arity of 2' do
|
95
|
+
it 'should raise an error' do
|
96
|
+
field_set.add :foo, default: ->(model, other_var) { nil }
|
97
|
+
expect { field_set.default_values(mock_model) }.to raise_error ArgumentError
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'with a proc with an arity of 0' do
|
102
|
+
it 'should evaluate the block' do
|
103
|
+
mock_model.should_receive :foo_val
|
104
|
+
field_set.add :foo, default: proc { foo_val }
|
105
|
+
field_set.default_values(mock_model)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'with a proc with an arity of 1' do
|
110
|
+
it 'should evaluate the block' do
|
111
|
+
mock_model.should_receive :foo_val
|
112
|
+
field_set.add :foo, default: proc { |model| model.foo_val }
|
113
|
+
field_set.default_values(mock_model)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'with a proc with an arity of 2' do
|
118
|
+
it 'should raise an error' do
|
119
|
+
field_set.add :foo, default: proc { |model, other_var| model.foo_val }
|
120
|
+
expect { field_set.default_values(mock_model) }.to raise_error ArgumentError
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'with a nil value' do
|
125
|
+
it 'should return nil' do
|
126
|
+
field_set.add :foo
|
127
|
+
field_set.default_values(mock_model)[:foo].should be_nil
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'when an invalid object' do
|
132
|
+
it 'should raise an error' do
|
133
|
+
field_set.add :foo, default: Object.new
|
134
|
+
expect { field_set.default_values(mock_model) }.to raise_error ArgumentError
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe '#to_a' do
|
140
|
+
it 'should return a list of names' do
|
141
|
+
field_set.add(:foo, comparable: true)
|
142
|
+
field_set.add(:bar, comparable: false)
|
143
|
+
field_set.to_a.should include :foo, :bar
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe '#method_missing' do
|
148
|
+
it 'should delegate off to #to_a' do
|
149
|
+
mock_array = mock
|
150
|
+
mock_array.should_receive :fubar
|
151
|
+
field_set.stub(:to_a).and_return(mock_array)
|
152
|
+
field_set.fubar
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'should raise an error' do
|
156
|
+
expect { field_set.fubar }.to raise_error NoMethodError
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MemoryModel::Base::Fieldable::Field do
|
4
|
+
|
5
|
+
subject(:field) { MemoryModel::Base::Fieldable::Field.new(:foo) }
|
6
|
+
|
7
|
+
describe '.new' do
|
8
|
+
it 'should not raise an error' do
|
9
|
+
expect { MemoryModel::Base::Fieldable::Field.new(:foo) }.to_not raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should have default options' do
|
13
|
+
field = MemoryModel::Base::Fieldable::Field.new(:foo)
|
14
|
+
field.options.should be_present
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should set_options' do
|
18
|
+
field = MemoryModel::Base::Fieldable::Field.new(:foo, bar: :baz)
|
19
|
+
field.options[:bar].should == :baz
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#==' do
|
24
|
+
context 'given a field' do
|
25
|
+
it 'should be equal to an object with the same name' do
|
26
|
+
field_a = MemoryModel::Base::Fieldable::Field.new(:foo, comparable: true)
|
27
|
+
field_b = MemoryModel::Base::Fieldable::Field.new(:foo, comparable: false)
|
28
|
+
(field_a == field_b).should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should not be equal to an object with a different name' do
|
32
|
+
field_a = MemoryModel::Base::Fieldable::Field.new(:foo)
|
33
|
+
field_b = MemoryModel::Base::Fieldable::Field.new(:bar)
|
34
|
+
(field_a == field_b).should be_false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'given a symbol' do
|
39
|
+
it 'should be equal to an object with the same name' do
|
40
|
+
field_a = MemoryModel::Base::Fieldable::Field.new(:foo, comparable: true)
|
41
|
+
field_b = :foo
|
42
|
+
(field_a == field_b).should be_true
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should not be equal to an object with a different name' do
|
46
|
+
field_a = MemoryModel::Base::Fieldable::Field.new(:foo)
|
47
|
+
field_b = :bar
|
48
|
+
(field_a == field_b).should be_false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#comparable?' do
|
54
|
+
it 'should return true' do
|
55
|
+
field = MemoryModel::Base::Fieldable::Field.new(:foo, comparable: true)
|
56
|
+
field.comparable?.should be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should return false' do
|
60
|
+
field = MemoryModel::Base::Fieldable::Field.new(:foo, comparable: false)
|
61
|
+
field.comparable?.should be_false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#default' do
|
66
|
+
it 'should return the default value' do
|
67
|
+
field = MemoryModel::Base::Fieldable::Field.new(:foo, default: 'foo')
|
68
|
+
field.default.should == 'foo'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#readonly?' do
|
73
|
+
it 'should return true' do
|
74
|
+
field = MemoryModel::Base::Fieldable::Field.new(:foo, readonly: true)
|
75
|
+
field.readonly?.should be_true
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should return false' do
|
79
|
+
field = MemoryModel::Base::Fieldable::Field.new(:foo, readonly: false)
|
80
|
+
field.readonly?.should be_false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#to_sym' do
|
85
|
+
it 'should be the symbolized name' do
|
86
|
+
field.to_sym.should == :foo
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#to_s' do
|
91
|
+
it 'should be the symbolized name' do
|
92
|
+
field.to_s.should == 'foo'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MemoryModel::Base::Fieldable do
|
4
|
+
|
5
|
+
let(:model_a) { Class.new(MemoryModel::Base) }
|
6
|
+
let(:model_b) { Class.new(MemoryModel::Base) }
|
7
|
+
|
8
|
+
describe '.field' do
|
9
|
+
it 'should add a field to fields' do
|
10
|
+
field = :foo
|
11
|
+
model_a.send :field, field
|
12
|
+
model_a.fields.should include field
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should not dirty other classes' do
|
16
|
+
field = :bar
|
17
|
+
model_a.send :field, field
|
18
|
+
model_a.fields.should include field
|
19
|
+
model_b.fields.should_not include field
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MemoryModel::Base::Persistence do
|
4
|
+
let(:model) do
|
5
|
+
Class.new(MemoryModel::Base) do
|
6
|
+
field :foo
|
7
|
+
end
|
8
|
+
end
|
9
|
+
let(:value) { 'bar' }
|
10
|
+
subject(:instance) { model.new(foo: value) }
|
11
|
+
before(:each) do
|
12
|
+
stub_const('MyModel', model)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#persisted?' do
|
16
|
+
it 'should be true if persisted' do
|
17
|
+
instance.commit
|
18
|
+
instance.persisted?.should be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should be false if not persisted' do
|
22
|
+
instance.persisted?.should be_false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#new_record?' do
|
27
|
+
it 'should be true unless persisted' do
|
28
|
+
instance.commit
|
29
|
+
instance.new_record?.should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should be false unless not persisted' do
|
33
|
+
instance.new_record?.should be_true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MemoryModel::Base::Versionable do
|
4
|
+
|
5
|
+
let(:model) do
|
6
|
+
Class.new(MemoryModel::Base)
|
7
|
+
end
|
8
|
+
let(:instance) do
|
9
|
+
model.new
|
10
|
+
end
|
11
|
+
before(:each) do
|
12
|
+
stub_const('MyModel', model)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#versions' do
|
16
|
+
it 'should have a number of versions' do
|
17
|
+
10.times.each do |index|
|
18
|
+
instance.versions.size.should == index
|
19
|
+
instance.commit
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#version' do
|
25
|
+
it 'should be the latest version' do
|
26
|
+
3.times.each { instance.commit }
|
27
|
+
instance.version.should == instance.versions.keys.last
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|