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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: afcfdbb737a946c2f08e73de14f36121acf79300
4
- data.tar.gz: 32c52dad7be7cd46dc5bc63176e24131b06f1909
3
+ metadata.gz: 86071e11077e8f0fbb5a648383a33fa2285f44d0
4
+ data.tar.gz: 0e204049ea4b6007dbb9280398e2e3d3101e964a
5
5
  SHA512:
6
- metadata.gz: 711338a9d4b320d44cf6aff3f5bc340da6b520b31dafd049c1c7749d76df2053b543fd334e1ec95a964fa9e81460e5c41e1397a2a1286916ce3613b68c2bc5c3
7
- data.tar.gz: 82d067106ed7b91d224ee12e0b89ddbd4b24fc020938e55e2bfd0c2c129089590cbe37d4c21bf8cc320105a4fcb42f5266f7057f4131789981990a079cb8dd90
6
+ metadata.gz: f938f7ff9708a8661dfdb79e54f0c89227e62feab7362166e4e605223551d4e720f93ea98b569c58ee8c7d31e8da4cd3249016b1bd7bfa81733ad39b148b86ad
7
+ data.tar.gz: 61e9fde6e2a30146dfcc7d24025b9a2c60db26e50857df20753f1566ff50bb1b102b9091636bb3e49a70f10fb20007711e057618969d427a509a6b4d5e9d2dec
data/.travis.yml CHANGED
@@ -2,6 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 2.2.2
4
4
  - 2.3.0
5
+ - 2.4.0
5
6
  - ruby-head
6
7
  - jruby-head
7
8
  env:
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 ```valid?```.
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 ```errors``` attribute.
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 ```model``` option.
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 ```attribute``` option.
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 ```#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.
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 ```UserSignupForm``` has automatically been defined with the ```name``` and ```email``` attributes and validations.
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 ```#inherit_from(klass, options = {})``` method also allows you to provide options for managing inherited attributes.
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 ```email``` attribute define it using the ```exclude``` option.
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 ```name``` attribute by using the ```only``` option.
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
- ```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.
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
- ```exclude``` - Don't inherit the attributes and validations for the provided attributes. Accepts strings, symbols and an array of strings or symbols.
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
- ```skip_validators``` - Only inherit attributes. Don't inherit any validators. Accepts a boolean.
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
- ```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.
214
+ ### Serialization
209
215
 
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.
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
- ```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.
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
 
@@ -5,6 +5,7 @@ module Soulless
5
5
  extend ActiveModel::Translation
6
6
 
7
7
  extend Soulless::Inheritance
8
+ extend Soulless::Serialization
8
9
 
9
10
  include ActiveModel::AttributeMethods
10
11
  include ActiveModel::Dirty
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Soulless
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/lib/soulless.rb CHANGED
@@ -5,6 +5,7 @@ require 'soulless/attributes'
5
5
  require 'soulless/callbacks'
6
6
  require 'soulless/dirty'
7
7
  require 'soulless/inheritance'
8
+ require 'soulless/serialization'
8
9
  require 'soulless/validations'
9
10
  require 'soulless/model'
10
11
  require 'soulless/version'
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.0.0
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: 2016-11-14 00:00:00.000000000 Z
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.7
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)