simple_schema 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,7 @@ module SimpleSchema
4
4
 
5
5
  def initialize(schema, options = {})
6
6
  options[:translates] ||= [ConverterItems,ConverterProperties]
7
+ options[:filters] ||= [FilterItems,FilterProperties]
7
8
  @schema = schema
8
9
  @schema = @schema.schema if @schema.respond_to?(:schema)
9
10
  @options = options
@@ -29,6 +30,26 @@ module SimpleSchema
29
30
 
30
31
  translated_data
31
32
  end
33
+
34
+ def filter(data, filter)
35
+ if !data.respond_to?(:each) && !data.respond_to?(:fetch)
36
+ raise InvalidDataType.new(data.class)
37
+ end
38
+
39
+ translated_data = {}
40
+ @schema.each do |key, properties|
41
+
42
+ value = data.fetch(key, nil)
43
+ @options[:filters].each do |normalizer|
44
+ value = normalizer.build(value, properties, @options , filter)
45
+ end
46
+
47
+ translated_data[key] = value if properties[:roles].include?(filter) #&& !value.nil?
48
+ end
49
+
50
+ translated_data
51
+
52
+ end
32
53
  end
33
54
  end
34
55
 
@@ -0,0 +1,14 @@
1
+ module SimpleSchema
2
+ class FilterItems
3
+ def self.build(data, schema, options, filter)
4
+ return data unless (schema[:type] == :array)
5
+ conveter = Converter::new(schema[:items], options)
6
+ items = []
7
+ data.each do |item|
8
+ items << conveter.filter(item, filter)
9
+ end
10
+ return items
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,9 @@
1
+ module SimpleSchema
2
+ class FilterProperties
3
+ def self.build(data, schema, options, filter)
4
+ return data unless (schema[:type] == :object)
5
+ return Converter::new(schema[:properties], options).filter(data, filter)
6
+ end
7
+ end
8
+ end
9
+
@@ -1,4 +1,4 @@
1
1
  module SimpleSchema
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
4
4
 
data/lib/simple_schema.rb CHANGED
@@ -7,6 +7,8 @@ require 'simple_schema/validator_items'
7
7
  require 'simple_schema/validator_properties'
8
8
  require 'simple_schema/validator_lambda'
9
9
  require 'simple_schema/validator'
10
+ require 'simple_schema/filter_items'
11
+ require 'simple_schema/filter_properties'
10
12
  require 'simple_schema/converter_items'
11
13
  require 'simple_schema/converter_properties'
12
14
  require 'simple_schema/converter'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,12 +13,12 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2011-09-02 00:00:00.000000000 -03:00
16
+ date: 2011-09-05 00:00:00.000000000 -03:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: rspec
21
- requirement: &14768220 !ruby/object:Gem::Requirement
21
+ requirement: &9370080 !ruby/object:Gem::Requirement
22
22
  none: false
23
23
  requirements:
24
24
  - - ! '>='
@@ -26,7 +26,7 @@ dependencies:
26
26
  version: '0'
27
27
  type: :development
28
28
  prerelease: false
29
- version_requirements: *14768220
29
+ version_requirements: *9370080
30
30
  description: Simple schema validator for Ruby
31
31
  email:
32
32
  - lenon.marcel@gmail.com
@@ -43,12 +43,13 @@ files:
43
43
  - lib/simple_schema/validator_lambda.rb
44
44
  - lib/simple_schema/converter.rb
45
45
  - lib/simple_schema/schema_items.rb
46
- - lib/simple_schema/injection.rb
46
+ - lib/simple_schema/filter_items.rb
47
47
  - lib/simple_schema/converter_properties.rb
48
48
  - lib/simple_schema/validator.rb
49
49
  - lib/simple_schema/version.rb
50
50
  - lib/simple_schema/schema_properties.rb
51
51
  - lib/simple_schema/errors.rb
52
+ - lib/simple_schema/filter_properties.rb
52
53
  - lib/simple_schema/validator_properties.rb
53
54
  - lib/simple_schema/converter_items.rb
54
55
  - lib/simple_schema.rb
@@ -1,23 +0,0 @@
1
- module SimpleSchema
2
- module Injection
3
-
4
- def normalizers
5
- build_injection
6
- end
7
-
8
- def registry(normalizer)
9
- raise SimpleSchema::InvalidBuilder.new unless normalizer.respond_to?(:build)
10
- build_injection << normalizer
11
- end
12
-
13
- def unregistry(class_name)
14
- build_injection.slice!(build_injection.index(class_name))
15
- end
16
-
17
- def build_injection
18
- self.class_variable_set(:@@normalizers_table, []) unless self.class_variable_defined?(:@@normalizers_table)
19
- self.class_variable_get(:@@normalizers_table)
20
- end
21
- end
22
- end
23
-