soulless 1.0.0 → 1.1.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 +4 -4
- data/.travis.yml +1 -0
- data/README.md +45 -17
- data/lib/soulless/model.rb +1 -0
- data/lib/soulless/serialization.rb +21 -0
- data/lib/soulless/version.rb +1 -1
- data/lib/soulless.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86071e11077e8f0fbb5a648383a33fa2285f44d0
|
4
|
+
data.tar.gz: 0e204049ea4b6007dbb9280398e2e3d3101e964a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f938f7ff9708a8661dfdb79e54f0c89227e62feab7362166e4e605223551d4e720f93ea98b569c58ee8c7d31e8da4cd3249016b1bd7bfa81733ad39b148b86ad
|
7
|
+
data.tar.gz: 61e9fde6e2a30146dfcc7d24025b9a2c60db26e50857df20753f1566ff50bb1b102b9091636bb3e49a70f10fb20007711e057618969d427a509a6b4d5e9d2dec
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -44,7 +44,7 @@ end
|
|
44
44
|
Soulless lets you define your validations and manage your errors just like you did in Rails.
|
45
45
|
|
46
46
|
```ruby
|
47
|
-
class UserSignupForm
|
47
|
+
class UserSignupForm < Soulless::Model
|
48
48
|
|
49
49
|
...
|
50
50
|
|
@@ -61,14 +61,14 @@ class UserSignupForm
|
|
61
61
|
end
|
62
62
|
```
|
63
63
|
|
64
|
-
Check to see if your object is valid by calling
|
64
|
+
Check to see if your object is valid by calling `valid?`.
|
65
65
|
|
66
66
|
```ruby
|
67
67
|
form = UserSignupForm.new(name: name, email: email)
|
68
68
|
form.valid? # => false
|
69
69
|
```
|
70
70
|
|
71
|
-
See what errors are popping up using the
|
71
|
+
See what errors are popping up using the `errors` attribute.
|
72
72
|
|
73
73
|
```ruby
|
74
74
|
form = UserSignupForm.new(name: name, email: email)
|
@@ -81,7 +81,7 @@ form.errors[:password] # => ["is too short (minimum is 8 characters)"]
|
|
81
81
|
If you're using Soulless in Rails it's even possible to validate uniqueness.
|
82
82
|
|
83
83
|
```ruby
|
84
|
-
class UserSignupForm
|
84
|
+
class UserSignupForm < Soulless::Model
|
85
85
|
|
86
86
|
...
|
87
87
|
|
@@ -93,9 +93,9 @@ class UserSignupForm
|
|
93
93
|
end
|
94
94
|
```
|
95
95
|
|
96
|
-
Just let the validator know what ActiveRecord model to use when performing the validation using the
|
96
|
+
Just let the validator know what ActiveRecord model to use when performing the validation using the `model` option.
|
97
97
|
|
98
|
-
If your Soulless object attribute doesn't match up to the ActiveRecord model attribute just map it using the
|
98
|
+
If your Soulless object attribute doesn't match up to the ActiveRecord model attribute just map it using the `attribute` option.
|
99
99
|
|
100
100
|
### Callbacks
|
101
101
|
|
@@ -141,7 +141,7 @@ person.name_change # => ["Anthony", "Peter Parker"]
|
|
141
141
|
|
142
142
|
One of the biggest pitfalls of the form object pattern is duplication of code. It's not uncommon for a form object to define attributes and validations that are identical to the model it represets.
|
143
143
|
|
144
|
-
To get rid of this annoying issue Soulless implements the
|
144
|
+
To get rid of this annoying issue Soulless implements the `#inherit_from(klass, options = {})` method. This method will allow a Soulless model to inherit attributes and validations from any Rails model, Soulless model or Virtus object.
|
145
145
|
|
146
146
|
```ruby
|
147
147
|
class User < ActiveRecord::Base
|
@@ -158,7 +158,7 @@ class UserSignupForm < Soulless::Model
|
|
158
158
|
end
|
159
159
|
```
|
160
160
|
|
161
|
-
The
|
161
|
+
The `UserSignupForm` has automatically been defined with the `name` and `email` attributes and validations.
|
162
162
|
|
163
163
|
```ruby
|
164
164
|
UserSignupForm.attributes # => name and email attributes
|
@@ -169,9 +169,9 @@ form.errors.messages # => { name: ["can't be blank"], email: ["can't be blank"]
|
|
169
169
|
|
170
170
|
If your model is using the uniqueness validator it will automatically convert it to the [uniqueness validator provided by Soulless](https://github.com/anthonator/soulless#uniqueness-validations).
|
171
171
|
|
172
|
-
The
|
172
|
+
The `#inherit_from(klass, options = {})` method also allows you to provide options for managing inherited attributes.
|
173
173
|
|
174
|
-
If you don't want to inherit the
|
174
|
+
If you don't want to inherit the `email` attribute define it using the `exclude` option.
|
175
175
|
|
176
176
|
```ruby
|
177
177
|
class UserSignupForm < Soulless::Model
|
@@ -184,7 +184,7 @@ form.valid? # => false
|
|
184
184
|
form.errors.messages # => { name: ["can't be blank"] }
|
185
185
|
```
|
186
186
|
|
187
|
-
You can also flip it around if you only want the
|
187
|
+
You can also flip it around if you only want the `name` attribute by using the `only` option.
|
188
188
|
|
189
189
|
```ruby
|
190
190
|
class UserSignupForm < Soulless::Model
|
@@ -199,17 +199,45 @@ form.errors.messages # => { name: ["can't be blank"] }
|
|
199
199
|
|
200
200
|
#### Available Options
|
201
201
|
|
202
|
-
|
202
|
+
`only` - Only inherit the attributes and validations for the provided attributes. Any attributes not specified will be ignored. Accepts strings, symbols and an array of strings or symbols.
|
203
|
+
|
204
|
+
`exclude` - Don't inherit the attributes and validations for the provided attributes. Accepts strings, symbols and an array of strings or symbols.
|
205
|
+
|
206
|
+
`skip_validators` - Only inherit attributes. Don't inherit any validators. Accepts a boolean.
|
207
|
+
|
208
|
+
`use_database_default` - Use the value of the `default` migration option as the default value for an attribute. Accepts either a boolean (for all attributes), a string or symbol for a single attribute or an array of strings and symbols for multiple attributes.
|
203
209
|
|
204
|
-
|
210
|
+
`additional_attributes` - Used to specify attributes that cannot automatically be added to the form model. These are generally attributes that have been specified using `attr_accessor`. Accepts a string, symbol or an array of strings and symbols for multiple attributes.
|
205
211
|
|
206
|
-
|
212
|
+
`validate_attribute_on` - By default any validation that specifies an `:on` option will not be inherited. This option will allow you to inherit a validator that uses the `:on` with a specific value. Example usage: `validate_password_on: :create`. Accepts a string or symbol. This option will accept any value that the Rails `:on` validator option can accept.
|
207
213
|
|
208
|
-
|
214
|
+
### Serialization
|
209
215
|
|
210
|
-
|
216
|
+
Soulless automatically handles serializing and deserializing Soulless models
|
217
|
+
using the standard ActiveRecord serialization methods.
|
218
|
+
|
219
|
+
```ruby
|
220
|
+
class Profile < Soulless::Model
|
221
|
+
attribute :first_name, String
|
222
|
+
attribute :last_name, String
|
223
|
+
|
224
|
+
...
|
225
|
+
end
|
226
|
+
|
227
|
+
class User < ActiveRecord::Base
|
228
|
+
serialize :profile, Profile
|
229
|
+
|
230
|
+
...
|
231
|
+
end
|
232
|
+
|
233
|
+
user = User.new
|
234
|
+
user.profile = { first_name: 'Anthony', last_name: 'Smith' }
|
235
|
+
user.profile # => Profile(first_name: 'Anthony', last_name: 'Smith')
|
236
|
+
```
|
211
237
|
|
212
|
-
|
238
|
+
If you want to implement your own serialization/deserialization process just
|
239
|
+
implement your own `self.load(value)` and `self.dump(value)` methods on your
|
240
|
+
model.
|
213
241
|
|
214
242
|
### I18n
|
215
243
|
|
data/lib/soulless/model.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Soulless
|
2
|
+
module Serialization
|
3
|
+
def load(value)
|
4
|
+
if value.is_a?(Array)
|
5
|
+
value.map do |data|
|
6
|
+
self.new(data)
|
7
|
+
end
|
8
|
+
elsif value.is_a?(Hash)
|
9
|
+
self.new(value)
|
10
|
+
elsif value.is_a?(String)
|
11
|
+
self.new(JSON.parse(value))
|
12
|
+
else
|
13
|
+
value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def dump(value)
|
18
|
+
value.to_json
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/soulless/version.rb
CHANGED
data/lib/soulless.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soulless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anthony Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- lib/soulless/dirty.rb
|
111
111
|
- lib/soulless/inheritance.rb
|
112
112
|
- lib/soulless/model.rb
|
113
|
+
- lib/soulless/serialization.rb
|
113
114
|
- lib/soulless/validations.rb
|
114
115
|
- lib/soulless/validations/uniqueness_validator.rb
|
115
116
|
- lib/soulless/version.rb
|
@@ -134,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
135
|
version: '0'
|
135
136
|
requirements: []
|
136
137
|
rubyforge_project:
|
137
|
-
rubygems_version: 2.6.
|
138
|
+
rubygems_version: 2.6.8
|
138
139
|
signing_key:
|
139
140
|
specification_version: 4
|
140
141
|
summary: Rails models without the database (and Rails)
|