d_struct 0.2.0 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6840c5eae09929cb404d64d4ec3dd2b1e938319
4
- data.tar.gz: b5f360621350357630d59731b41eb633a0a7d245
3
+ metadata.gz: 44d2409d6e32c4db67d6177a56e95256c0a72d41
4
+ data.tar.gz: 111559d2c1675f950ea5efee6b4a2334eef1e9dc
5
5
  SHA512:
6
- metadata.gz: 558a57a25c18473e104a4a8584247689ab118341c0a6614a71abd217cebf9b6de994ffdebd07dd8d345928071f459c2c9e3b8514db80764b316d03226f700f1a
7
- data.tar.gz: 050007957695b98443da669bfefac051ea780ae3afeb9f9a227e49acf6fe65fe976124f13582e75a75c96e4e9a60b9c660f58afa4de6acc401f7a9af45bd2d2f
6
+ metadata.gz: 74076d4f0a020913f7e15290a33e4f13ca9192313b353947887e7f7fbf1e764488d8f58f6002c21b02d907c2eb104cce41c5854ea41adc4af678590c5bf8e13a
7
+ data.tar.gz: 4068d291ffa1438324dc838996e4402e22c0d41d36f986989878459d69af6b3f74c2a89c146dd6a132dac8bf6f9a903c8936cb3319332722c62f95129a90e6e2
data/README.md CHANGED
@@ -16,10 +16,17 @@
16
16
  key(:arr).required(:array?)
17
17
  end
18
18
 
19
- # initialize with hash
20
- struct = MyStruct.new({string: 'abc', int: 123, bool: true, arr: [1]})
19
+ # initialize without casting
20
+ struct = MyStruct.new({string: '123', int: 123, bool: true, arr: [1], date: Date.today})
21
21
  struct.add_validation_schema MyValidationSchema
22
- puts struct.to_h # => {string: 'abc', int: 123, bool: true, arr: [1]}
22
+ puts struct.to_h # => {string: '123', int: 123, bool: true, arr: [1], #<Date: 2016-03-22 ...>}
23
+ puts struct.valid? # => true
24
+ puts struct.errors # => {}
25
+
26
+ # initialize with casting
27
+ struct = MyStruct.new({string: 123, int: '123', bool: 'true', arr: 1, date: '2016-03-22'})
28
+ struct.add_validation_schema MyValidationSchema
29
+ puts struct.to_h # => {string: '123', int: 123, bool: true, arr: [1], #<Date: 2016-03-22 ...>}
23
30
  puts struct.valid? # => true
24
31
  puts struct.errors # => {}
25
32
 
@@ -44,7 +51,7 @@
44
51
  # multiple schemas
45
52
  Schema1 = Dry::Validation.Schema{key(:string).required(:str?)}
46
53
  Schema2 = Dry::Validation.Schema{key(:int).required(:int?)}
47
- struct = MyStruct.new({string: 'abc', int: 123})
54
+ struct = MyStruct.new({string: '123', int: 123})
48
55
  struct.add_validation_schema Schema1, Schema2
49
56
  puts struct.valid? # => true
50
57
  ```
@@ -9,8 +9,9 @@ module DStruct
9
9
  @attributes_readers = attributes_hash.values.flatten
10
10
  @string_attributes = attributes_hash[:strings] || []
11
11
  @integer_attributes = attributes_hash[:integers] || []
12
- @booleans_attributes = attributes_hash[:booleans] || []
13
- @array_attributes = attributes_hash[:arrays] || []
12
+ @boolean_attributes = attributes_hash[:booleans] || []
13
+ @array_attributes = attributes_hash[:arrays] || []
14
+ @date_attributes = attributes_hash[:dates] || []
14
15
 
15
16
  # generate readers
16
17
  attr_reader *@attributes_readers
@@ -19,7 +20,7 @@ module DStruct
19
20
  @string_attributes.each do |string_attr|
20
21
  define_method "#{string_attr}=" do |str_arg|
21
22
  value = str_arg.to_s
22
- instance_variable_set("@#{string_attr}", str_arg)
23
+ instance_variable_set("@#{string_attr}", value)
23
24
  @to_h[string_attr] = value
24
25
  end
25
26
  end
@@ -32,13 +33,10 @@ module DStruct
32
33
  end
33
34
  end
34
35
 
35
- @booleans_attributes.each do |boolean_attr|
36
+ @boolean_attributes.each do |boolean_attr|
36
37
  define_method "#{boolean_attr}=" do |boolean_arg|
37
- value = case boolean_arg
38
- when TrueClass then true
39
- when FalseClass then false
40
- else nil
41
- end
38
+ value = !!boolean_arg
39
+ value = nil if boolean_arg.nil?
42
40
  instance_variable_set("@#{boolean_attr}", value)
43
41
  @to_h[boolean_attr] = value
44
42
  end
@@ -51,6 +49,14 @@ module DStruct
51
49
  @to_h[arr_attr] = value
52
50
  end
53
51
  end
52
+
53
+ @date_attributes.each do |date_attr|
54
+ define_method "#{date_attr}=" do |date_arg|
55
+ value = (Date.parse(date_arg.to_s) rescue nil)
56
+ instance_variable_set("@#{date_attr}", value)
57
+ @to_h[date_attr] = value
58
+ end
59
+ end
54
60
  end
55
61
 
56
62
  def add_validation_schema(*schema)
@@ -76,6 +82,7 @@ module DStruct
76
82
  @errors ||= @validation_schemas.flatten.reduce({}){|errors, schema| errors.update(schema.call(to_h).messages)}
77
83
  end
78
84
 
85
+ # call once, it is cached in @errors
79
86
  def valid?
80
87
  errors.empty?
81
88
  end
@@ -1,3 +1,3 @@
1
1
  module DStruct
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.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-19 00:00:00.000000000 Z
11
+ date: 2016-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler