modis 1.2.0 → 1.3.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/modis/attribute.rb +4 -3
- data/lib/modis/persistence.rb +3 -3
- data/lib/modis/version.rb +1 -1
- data/spec/attribute_spec.rb +22 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2bceb3d4a73d55abb1e394c0a5bc3b4d2aba0a5c
|
4
|
+
data.tar.gz: 0e12bc5e617999f6c6da40c96add042f9b43feb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80a0a72e0ce9d49e440e10e030f5c3c0d91615780ea7bdafba3ff9fc2cfe31e88bcfdecaa7a27c0e7feb62322d576f5d178afebf2fee0fdec296f77fc0685abd
|
7
|
+
data.tar.gz: 3896ccbe329b3e308b5deccc2960e89c6bbc21f4121eb99967135b02f7eb12a0fc4f89755f68fc4c2d8f37cf4c7d8f331fe7174162bedbdb7d0c5123b4e085a8
|
data/Gemfile.lock
CHANGED
data/lib/modis/attribute.rb
CHANGED
@@ -20,12 +20,13 @@ module Modis
|
|
20
20
|
attribute :id, :integer
|
21
21
|
end
|
22
22
|
|
23
|
-
def attribute(name,
|
23
|
+
def attribute(name, types, options = {})
|
24
24
|
name = name.to_s
|
25
|
+
types = Array[*types]
|
25
26
|
raise AttributeError, "Attribute with name '#{name}' has already been specified." if attributes.key?(name)
|
26
|
-
raise UnsupportedAttributeType, type unless TYPES.include?(type)
|
27
|
+
types.each { |type| raise UnsupportedAttributeType, type unless TYPES.include?(type) }
|
27
28
|
|
28
|
-
attributes[name] = options.update(
|
29
|
+
attributes[name] = options.update(types: types)
|
29
30
|
define_attribute_methods [name]
|
30
31
|
class_eval <<-EOS, __FILE__, __LINE__
|
31
32
|
def #{name}
|
data/lib/modis/persistence.rb
CHANGED
@@ -128,11 +128,11 @@ module Modis
|
|
128
128
|
end
|
129
129
|
|
130
130
|
def ensure_type(attribute, value)
|
131
|
-
|
132
|
-
type_classes = classes_for_type(type)
|
131
|
+
types = self.class.attributes[attribute.to_s][:types]
|
132
|
+
type_classes = types.map { |type| classes_for_type(type) }.flatten
|
133
133
|
|
134
134
|
return unless value && !type_classes.include?(value.class)
|
135
|
-
raise Modis::AttributeCoercionError, "Received value of type '#{value.class}', expected '#{
|
135
|
+
raise Modis::AttributeCoercionError, "Received value of type '#{value.class}', expected '#{type_classes.join('\', \'')}' for attribute '#{attribute}'."
|
136
136
|
end
|
137
137
|
|
138
138
|
def classes_for_type(type)
|
data/lib/modis/version.rb
CHANGED
data/spec/attribute_spec.rb
CHANGED
@@ -11,6 +11,7 @@ module AttributeSpec
|
|
11
11
|
attribute :flag, :boolean
|
12
12
|
attribute :array, :array
|
13
13
|
attribute :hash, :hash
|
14
|
+
attribute :string_or_hash, [:string, :hash]
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
@@ -121,7 +122,7 @@ describe Modis::Attribute do
|
|
121
122
|
end
|
122
123
|
|
123
124
|
it 'raises an error if assigned a non-boolean value' do
|
124
|
-
expect { model.flag = 'unf!' }.to raise_error(Modis::AttributeCoercionError)
|
125
|
+
expect { model.flag = 'unf!' }.to raise_error(Modis::AttributeCoercionError, "Received value of type 'String', expected 'TrueClass', 'FalseClass' for attribute 'flag'.")
|
125
126
|
end
|
126
127
|
end
|
127
128
|
|
@@ -134,7 +135,7 @@ describe Modis::Attribute do
|
|
134
135
|
end
|
135
136
|
|
136
137
|
it 'raises an error when assigned another type' do
|
137
|
-
expect { model.array = { foo: :bar } }.to raise_error(Modis::AttributeCoercionError)
|
138
|
+
expect { model.array = { foo: :bar } }.to raise_error(Modis::AttributeCoercionError, "Received value of type 'Hash', expected 'Array' for attribute 'array'.")
|
138
139
|
end
|
139
140
|
end
|
140
141
|
|
@@ -147,7 +148,25 @@ describe Modis::Attribute do
|
|
147
148
|
end
|
148
149
|
|
149
150
|
it 'raises an error when assigned another type' do
|
150
|
-
expect { model.hash = [] }.to raise_error(Modis::AttributeCoercionError)
|
151
|
+
expect { model.hash = [] }.to raise_error(Modis::AttributeCoercionError, "Received value of type 'Array', expected 'Hash' for attribute 'hash'.")
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe 'variable type' do
|
156
|
+
it 'is coerced' do
|
157
|
+
model.string_or_hash = { foo: :bar }
|
158
|
+
model.save!
|
159
|
+
found = AttributeSpec::MockModel.find(model.id)
|
160
|
+
expect(found.string_or_hash).to eq(foo: :bar)
|
161
|
+
|
162
|
+
model.string_or_hash = 'test'
|
163
|
+
model.save!
|
164
|
+
found = AttributeSpec::MockModel.find(model.id)
|
165
|
+
expect(found.string_or_hash).to eq('test')
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'raises an error when assigned another type' do
|
169
|
+
expect { model.string_or_hash = [] }.to raise_error(Modis::AttributeCoercionError, "Received value of type 'Array', expected 'String', 'Hash' for attribute 'string_or_hash'.")
|
151
170
|
end
|
152
171
|
end
|
153
172
|
end
|