modis 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c1496e1e5c9b430c4aa6257d4ec122d3cb1ee37
4
- data.tar.gz: 68c023c5b84245530772181c7a2bf564ebe0712f
3
+ metadata.gz: 2bceb3d4a73d55abb1e394c0a5bc3b4d2aba0a5c
4
+ data.tar.gz: 0e12bc5e617999f6c6da40c96add042f9b43feb8
5
5
  SHA512:
6
- metadata.gz: f77b3079fd2c210bb678b952710e188dda3eb1ae8d98d00f30b79b511671305df22a3d5863433c48c2a831bf795580afc8a6c1d6973530a0847e7a5f14cabfad
7
- data.tar.gz: d22b018df72ac21e0bce75fba13816ecf7067b96602afae6f0d8d09321d2f24c8c194809d30bc221e543bcbc5bd4330157248eed20ac761c1560d0d647b99ddc
6
+ metadata.gz: 80a0a72e0ce9d49e440e10e030f5c3c0d91615780ea7bdafba3ff9fc2cfe31e88bcfdecaa7a27c0e7feb62322d576f5d178afebf2fee0fdec296f77fc0685abd
7
+ data.tar.gz: 3896ccbe329b3e308b5deccc2960e89c6bbc21f4121eb99967135b02f7eb12a0fc4f89755f68fc4c2d8f37cf4c7d8f331fe7174162bedbdb7d0c5123b4e085a8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- modis (1.2.0)
4
+ modis (1.3.0)
5
5
  activemodel (>= 3.0)
6
6
  activesupport (>= 3.0)
7
7
  connection_pool (>= 2)
@@ -20,12 +20,13 @@ module Modis
20
20
  attribute :id, :integer
21
21
  end
22
22
 
23
- def attribute(name, type = :string, options = {})
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(type: type)
29
+ attributes[name] = options.update(types: types)
29
30
  define_attribute_methods [name]
30
31
  class_eval <<-EOS, __FILE__, __LINE__
31
32
  def #{name}
@@ -128,11 +128,11 @@ module Modis
128
128
  end
129
129
 
130
130
  def ensure_type(attribute, value)
131
- type = self.class.attributes[attribute.to_s][:type]
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 '#{type}' for attribute '#{name}'."
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
@@ -1,3 +1,3 @@
1
1
  module Modis
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Leitch