d_struct 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6322dce3ef90ced40580ed199de3b4bd7a956f54
4
- data.tar.gz: db08338cebec0db8b7a0514ea4f6ed294159fed6
3
+ metadata.gz: a6840c5eae09929cb404d64d4ec3dd2b1e938319
4
+ data.tar.gz: b5f360621350357630d59731b41eb633a0a7d245
5
5
  SHA512:
6
- metadata.gz: d46d82dfbdd681acf51542621dad92fd425f7e4a202c5e29e0f8d1615ac7c08d41c76d3ac105c2f3a892458036f3ff61993eb33d485720c5749568bb5e79c59c
7
- data.tar.gz: 65c7277fd77a7e87cd66172183d5416c70952c70998b27ee431b737e7b2dca86d8997f0116baa081b711447cf4b60f950de6b52baa309436520876bd1993b813
6
+ metadata.gz: 558a57a25c18473e104a4a8584247689ab118341c0a6614a71abd217cebf9b6de994ffdebd07dd8d345928071f459c2c9e3b8514db80764b316d03226f700f1a
7
+ data.tar.gz: 050007957695b98443da669bfefac051ea780ae3afeb9f9a227e49acf6fe65fe976124f13582e75a75c96e4e9a60b9c660f58afa4de6acc401f7a9af45bd2d2f
data/README.md CHANGED
@@ -25,18 +25,28 @@
25
25
 
26
26
  # unknown key
27
27
  struct = MyStruct.new({unknown_key: '123'})
28
+ struct.add_validation_schema MyValidationSchema
28
29
  puts struct.valid? # => false
29
30
  puts struct.errors # => {unknown_key: 'unknown key'}
30
31
 
31
32
  # missing keys
32
33
  struct = MyStruct.new({})
34
+ struct.add_validation_schema MyValidationSchema
33
35
  puts struct.valid? # => false
34
36
  puts struct.errors # => {:string=>["is missing"], :int=>["is missing"], :bool=>["is missing"], :arr=>["is missing"]}
35
37
 
36
38
  # invalid values
37
39
  struct = MyStruct.new({string: nil, int: nil, bool: nil, arr: nil})
40
+ struct.add_validation_schema MyValidationSchema
38
41
  puts struct.valid? # => false
39
42
  puts struct.errors # => {:string=>["must be filled"], :int=>["must be filled"], :bool=>["must be filled"], :arr=>["must be filled"]}
43
+
44
+ # multiple schemas
45
+ Schema1 = Dry::Validation.Schema{key(:string).required(:str?)}
46
+ Schema2 = Dry::Validation.Schema{key(:int).required(:int?)}
47
+ struct = MyStruct.new({string: 'abc', int: 123})
48
+ struct.add_validation_schema Schema1, Schema2
49
+ puts struct.valid? # => true
40
50
  ```
41
51
 
42
52
  ## Installation
@@ -4,7 +4,6 @@ module DStruct
4
4
  class DStruct
5
5
 
6
6
  attr_reader :to_h
7
- attr_accessor :validation_schema
8
7
 
9
8
  def self.attributes(attributes_hash)
10
9
  @attributes_readers = attributes_hash.values.flatten
@@ -54,12 +53,13 @@ module DStruct
54
53
  end
55
54
  end
56
55
 
57
- def add_validation_schema(schema)
58
- @validation_schema = schema
56
+ def add_validation_schema(*schema)
57
+ @validation_schemas << schema
59
58
  end
60
59
 
61
60
  def initialize(attributes_hash)
62
61
  @to_h = {}
62
+ @validation_schemas = []
63
63
  attributes_hash.each do |k,v|
64
64
  begin
65
65
  send("#{k}=", v)
@@ -71,9 +71,9 @@ module DStruct
71
71
  end
72
72
 
73
73
  def errors
74
- return @errors if @errors # unknown key
75
- return {} unless validation_schema # no schema
76
- @errors ||= validation_schema.call(to_h).messages
74
+ return @errors if @errors # unknown key
75
+ return {} if @validation_schemas == [] # no schemas
76
+ @errors ||= @validation_schemas.flatten.reduce({}){|errors, schema| errors.update(schema.call(to_h).messages)}
77
77
  end
78
78
 
79
79
  def valid?
@@ -1,3 +1,3 @@
1
1
  module DStruct
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: d_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damir Roso
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-18 00:00:00.000000000 Z
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler