validated_object 2.0.0 → 2.0.1

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
  SHA256:
3
- metadata.gz: ba509cb442a5ef4c67d7186bd58b329551c00e137ec2c2b1cd7c4e268b92344a
4
- data.tar.gz: 79a4f93be0f9919bf189f01258ed0379382df90aa01bc69a5ae883d924d53527
3
+ metadata.gz: ead35b1f631106e09d5ed245b18ced301b0fd22a642136d2198dc65f592feb72
4
+ data.tar.gz: 119b6594bedc0b63a11256ae7e40dccf67561cab4acccf6a67b7c3a08789642d
5
5
  SHA512:
6
- metadata.gz: d626a9f7e53258a92e5a0fa3a8cfabf4c7ba38e2c960466e29aaf98dacb7e433c02e3facaca3d2c96a3bd769077a3c3d6e1378b9631a48b6e307db88c8b91f98
7
- data.tar.gz: 73771320ac4176387bbe336057c2459b5772898a68b380518e149f93af8a29243312462adda6f498a15fb777cb8e803e0db684bc41c130fc316135b63dcc9dac
6
+ metadata.gz: 371f7b30919720b846b2d0a8f1b065e011da484632f889cf98b21847f34b41d1b4ed0bf213662f6876bdbbd088faede79e054ed83284cc9d589bdc8dd4fc52a3
7
+ data.tar.gz: 8c9c3ea80f2924d23bd9297852b557f373a4bdcdb26785ef279730dbec5d3c372740be475f36652ece87884e6ec92bae13a5ea7b488e032ba684fdb29354ee09
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # ValidatedObject
4
4
 
5
- Plain Old Ruby Objects + Rails Validations = self-checking Ruby objects.
5
+ Plain Old Ruby Objects + Rails Validations = self-checking Ruby objects.
6
6
 
7
7
 
8
8
  ## Goals
@@ -19,12 +19,14 @@ This is a small layer around
19
19
 
20
20
  ### Writing a self-validating object
21
21
 
22
- All of the [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) are available, plus a new one, `TypeValidator`.
22
+ All of the [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) are available, plus a new one, `TypeValidator`.
23
23
 
24
24
  ```ruby
25
25
  class Dog < ValidatedObject::Base
26
+ # Plain old Ruby
26
27
  attr_accessor :name, :birthday # attr_reader is supported as well for read-only attributes
27
28
 
29
+ # Plain old Rails
28
30
  validates :name, presence: true
29
31
  validates :birthday, type: Date, allow_nil: true # Strongly typed but optional
30
32
  end
@@ -62,7 +64,7 @@ used to test an instance, plus the custom `check_validations!` convenience metho
62
64
  ```ruby
63
65
  spot.birthday = '2015-01-23'
64
66
  spot.valid? # => false
65
- spot.check_validations! # => ArgumentError: Birthday is class String, not Date
67
+ spot.check_validations! # => ArgumentError: Birthday is a String, not a Date
66
68
  ```
67
69
 
68
70
  Note the clear, explicit error message. These are great when reading a log
@@ -71,7 +73,7 @@ test it by making another attribute invalid:
71
73
 
72
74
  ```ruby
73
75
  spot.name = nil
74
- spot.check_validations! # => ArgumentError: Name can't be blank; Birthday is class String, not Date
76
+ spot.check_validations! # => ArgumentError: Name can't be blank; Birthday is a String, not a Date
75
77
  ```
76
78
 
77
79
 
@@ -95,6 +97,10 @@ The result is that `dogs` is an array of guaranteed valid Dog objects. And the
95
97
  error log lists unparseable rows with good info for tracking down problems in
96
98
  the data.
97
99
 
100
+ ### Use in code generation
101
+
102
+ My [Schema.org microdata generation gem](https://github.com/dogweather/schema-dot-org) uses ValidatedObjects to recursively create well formed HTML / JSON-LD.
103
+
98
104
  ## Installation
99
105
 
100
106
  Add this line to your application's Gemfile:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model'
2
4
  require 'validated_object/version'
3
5
 
@@ -55,9 +57,7 @@ module ValidatedObject
55
57
  def initialize(attributes=EMPTY_HASH)
56
58
  raise ArgumentError, "#{attributes} is not a Hash" unless attributes.is_a?(Hash)
57
59
 
58
- attributes.keys.each do |key|
59
- self.instance_variable_set "@#{key}".to_sym, attributes.fetch(key)
60
- end
60
+ set_instance_variables from_hash: attributes
61
61
  check_validations!
62
62
  self
63
63
  end
@@ -84,15 +84,41 @@ module ValidatedObject
84
84
  def validate_each(record, attribute, value)
85
85
  expected_class = options[:with]
86
86
 
87
- if expected_class == Boolean
88
- return if value.is_a?(TrueClass) || value.is_a?(FalseClass)
89
- else
90
- return if value.is_a?(expected_class)
91
- end
87
+ return if pseudo_boolean?(expected_class, value) ||
88
+ expected_class?(expected_class, value)
92
89
 
93
- msg = options[:message] || "is a #{value.class}, not a #{expected_class}"
94
- record.errors.add attribute, msg
90
+ save_error(record, attribute, value, options)
91
+ end
92
+
93
+
94
+ private
95
+
96
+ def pseudo_boolean?(expected_class, value)
97
+ expected_class == Boolean && boolean?(value)
98
+ end
99
+
100
+ def expected_class?(expected_class, value)
101
+ value.is_a?(expected_class)
102
+ end
103
+
104
+ def boolean?(value)
105
+ value.is_a?(TrueClass) || value.is_a?(FalseClass)
106
+ end
107
+
108
+ def save_error(record, attribute, value, options)
109
+ record.errors.add attribute,
110
+ options[:message] || "is a #{value.class}, not a #{options[:with]}"
111
+ end
112
+ end
113
+
114
+
115
+ private
116
+
117
+ def set_instance_variables(from_hash:)
118
+ from_hash.each do |variable_name, variable_value|
119
+ self.instance_variable_set "@#{variable_name}".to_sym, variable_value
95
120
  end
96
121
  end
122
+
97
123
  end
98
124
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ValidatedObject
3
- VERSION = '2.0.0'.freeze
3
+ VERSION = '2.0.1'
4
4
  end
@@ -13,5 +13,5 @@ puts phoebe.inspect
13
13
  maru = Dog.new(birthday: Date.today, name: 'Maru')
14
14
  puts maru.inspect
15
15
 
16
- paris = Dog.new
17
- puts paris.inspect
16
+ hiro = Dog.new(birthday: 'today')
17
+ puts hiro.inspect
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validated_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-23 00:00:00.000000000 Z
11
+ date: 2018-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler