schemata-dea 0.0.1.beta11 → 0.0.1.beta12

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ module Schemata
6
6
  class UpdateAttributeError < StandardError
7
7
  def initialize(key, message)
8
8
  super("#{key}: #{message}")
9
- end
9
+ end
10
10
  end
11
11
 
12
12
  class IncompatibleVersionError < DecodeError
@@ -1,4 +1,5 @@
1
1
  require 'yajl'
2
+ require 'membrane'
2
3
  require 'schemata/common/error'
3
4
  require 'schemata/helpers/stringify'
4
5
  require 'membrane'
@@ -29,7 +30,7 @@ module Schemata
29
30
 
30
31
  begin
31
32
  field_schema.validate(field_value)
32
- rescue ::Membrane::SchemaValidationError => e
33
+ rescue Membrane::SchemaValidationError => e
33
34
  raise Schemata::UpdateAttributeError.new(key, e.message)
34
35
  end
35
36
 
@@ -54,7 +55,7 @@ module Schemata
54
55
  field_value = Schemata::HashCopyHelpers.stringify(field_value)
55
56
  begin
56
57
  field_schema.validate(field_value)
57
- rescue ::Membrane::SchemaValidationError => e
58
+ rescue Membrane::SchemaValidationError => e
58
59
  raise Schemata::UpdateAttributeError.new(key, e.message)
59
60
  end
60
61
  @contents[key] = Schemata::HashCopyHelpers.deep_copy(field_value)
@@ -96,7 +97,7 @@ module Schemata
96
97
  begin
97
98
  validate_contents
98
99
  validate_aux_data
99
- rescue ::Membrane::SchemaValidationError => e
100
+ rescue Membrane::SchemaValidationError => e
100
101
  raise Schemata::EncodeError.new(e.message)
101
102
  end
102
103
 
@@ -194,16 +195,16 @@ module Schemata
194
195
 
195
196
  module Dsl
196
197
  def define_schema(&blk)
197
- schema = ::Membrane::SchemaParser.parse(&blk)
198
- unless schema.kind_of? ::Membrane::Schema::Record
198
+ schema = Membrane::SchemaParser.parse(&blk)
199
+ unless schema.kind_of? Membrane::Schema::Record
199
200
  Schemata::SchemaDefinitionError.new("Schema must be a hash")
200
201
  end
201
202
  self::const_set(:SCHEMA, schema)
202
203
  end
203
204
 
204
205
  def define_aux_schema(&blk)
205
- aux_schema = ::Membrane::SchemaParser.parse(&blk)
206
- unless aux_schema.kind_of? ::Membrane::Schema::Record
206
+ aux_schema = Membrane::SchemaParser.parse(&blk)
207
+ unless aux_schema.kind_of? Membrane::Schema::Record
207
208
  Schemata::SchemaDefinitionError.new("Schema must be a hash")
208
209
  end
209
210
 
@@ -223,7 +224,7 @@ module Schemata
223
224
  # decode, when aux_data is irrelevant
224
225
  begin
225
226
  previous_version::SCHEMA.validate(old_data)
226
- rescue ::Membrane::SchemaValidationError => e
227
+ rescue Membrane::SchemaValidationError => e
227
228
  raise Schemata::DecodeError.new(e.message)
228
229
  end
229
230
 
@@ -264,7 +265,7 @@ module Schemata
264
265
  begin
265
266
  self.schema.validate(mock)
266
267
  define_constant(:MOCK_VALUES, hash)
267
- rescue ::Membrane::SchemaValidationError => e
268
+ rescue Membrane::SchemaValidationError => e
268
269
  raise SchemaDefinitionError.new("Sample mock values do not match schema: #{e}")
269
270
  end
270
271
  end
@@ -1,5 +1,5 @@
1
1
  module Schemata
2
2
  module DEA
3
- VERSION = "0.0.1.beta11"
3
+ VERSION = "0.0.1.beta12"
4
4
  end
5
5
  end
@@ -52,3 +52,46 @@ def num_mandatory_fields(msg_obj)
52
52
  diff = mandatory - optional
53
53
  return diff.size
54
54
  end
55
+
56
+ def get_allowed_classes(schema)
57
+ case schema
58
+ when Membrane::Schema::Bool
59
+ return Set.new([TrueClass, FalseClass])
60
+ when Membrane::Schema::Enum
61
+ return Set.new(schema.elem_schemas.map {|x| get_allowed_classes(x)}).flatten
62
+ when Membrane::Schema::Class
63
+ return Set.new([schema.klass])
64
+ when Membrane::Schema::Record
65
+ return Set.new([Hash])
66
+ when Membrane::Schema::List
67
+ return Set.new([Array])
68
+ end
69
+ end
70
+
71
+ def get_unallowed_classes(schema)
72
+ all_classes = Set.new([Integer, String, Float, TrueClass, FalseClass, NilClass, Hash, Array])
73
+ allowed_classes = get_allowed_classes(schema)
74
+ all_classes - allowed_classes
75
+ end
76
+
77
+ def default_value(klass)
78
+ # Yes this is silly
79
+ if klass == Integer
80
+ return 0
81
+ elsif klass == String
82
+ return "foo"
83
+ elsif klass == Float
84
+ return 3.14
85
+ elsif klass == TrueClass
86
+ return true
87
+ elsif klass == FalseClass
88
+ return false
89
+ elsif klass == NilClass
90
+ return nil
91
+ elsif klass == Hash
92
+ return {}
93
+ elsif klass == Array
94
+ return []
95
+ else
96
+ end
97
+ end
@@ -35,11 +35,10 @@ shared_examples "a message" do
35
35
  mock_hash = component.send(mock_method, 1).contents
36
36
  first_key = mock_hash.keys[0]
37
37
  first_value = mock_hash[first_key]
38
- if first_value.kind_of? Integer
39
- bad_value = "foo"
40
- else
41
- bad_value = 1
42
- end
38
+
39
+ schema = message.schema.schemas[first_key]
40
+ unallowed_classes = get_unallowed_classes(schema)
41
+ bad_value = default_value(unallowed_classes.to_a[0])
43
42
 
44
43
  expect {
45
44
  msg_obj = message.new({first_key => bad_value})
@@ -120,11 +119,9 @@ shared_examples "a message" do
120
119
  it "should raise an error if the wrong type is written" do
121
120
  mock_value = component.send(mock_method, version).contents[attr]
122
121
 
123
- if mock_value.kind_of? Integer
124
- bad_value = "foo"
125
- else
126
- bad_value = 1
127
- end
122
+ schema = message.schema.schemas[attr]
123
+ unallowed_classes = get_unallowed_classes(schema)
124
+ bad_value = default_value(unallowed_classes.to_a[0])
128
125
 
129
126
  msg_obj = message.new
130
127
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemata-dea
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta11
4
+ version: 0.0.1.beta12
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors: