passive_model 1.0.0 → 1.0.2
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/README.md +70 -11
- data/lib/passive_model/base.rb +1 -1
- data/lib/passive_model/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92b215b614d68f0e42bf4f96475a4c84ee331f3cb9e48c3627d1dc6bf0cf25c3
|
4
|
+
data.tar.gz: cb7d3782cab1e2d86390dfad69ede00f0e7176ef092b75327f01aec1d015c5fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33f7f75e0bb079a39e49f3ea5cd6396e2e299e3dd31e32c7732ff43efe9df65141178ae1a8f8567bb4cae44c37b8571c207b3e1aa901c8eef818f2f955f3fee4
|
7
|
+
data.tar.gz: c1f82c6c0e0b372aa9bd1cd9b56091bc024fa01729cce504873756fa43c2d00583689ca6532da2ac53f497c1e5d9cf0ac14e5b62ff509bc7519629172fc6e596
|
data/README.md
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
#
|
1
|
+
# PassiveModel
|
2
2
|
|
3
|
-
|
3
|
+
This is a gem for using Rails ActiveModel functionalities without having an actual database table.
|
4
4
|
|
5
|
-
|
5
|
+
Supports validation, callbacks etc. And behaves like a model object, so can be replaced with actual model objects.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem '
|
12
|
+
gem 'passive_model'
|
13
13
|
```
|
14
14
|
|
15
15
|
And then execute:
|
@@ -18,19 +18,78 @@ And then execute:
|
|
18
18
|
|
19
19
|
Or install it yourself as:
|
20
20
|
|
21
|
-
$ gem install
|
21
|
+
$ gem install passive_model
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
Create a class from `PassiveModel::Base`.
|
26
26
|
|
27
|
-
|
27
|
+
For example:
|
28
28
|
|
29
|
-
|
29
|
+
```erbruby
|
30
|
+
class ContactForm < PassiveModel::Base
|
31
|
+
attr_accessor :first_name, :last_name
|
30
32
|
|
31
|
-
|
33
|
+
validates :first_name, presence: true
|
34
|
+
validates :last_name, presence: true
|
35
|
+
validate :first_name_is_not_spam
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def first_name_is_not_spam
|
40
|
+
return if first_name.downcase != 'test'
|
32
41
|
|
33
|
-
|
42
|
+
errors.add(:first_name, 'is not valid')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
#### Create new objects
|
48
|
+
|
49
|
+
`form = ContactForm.new(first_name: 'John', last_name: 'Doe')`
|
50
|
+
|
51
|
+
#### Set attributes
|
52
|
+
|
53
|
+
`form.attributes = { first_name: 'Lewis' }`
|
54
|
+
|
55
|
+
#### Emulate a save
|
56
|
+
|
57
|
+
It runs the validations and also calls `before_save` callbacks if the object is valid
|
58
|
+
|
59
|
+
`form.save # returns true if valid?`
|
60
|
+
|
61
|
+
#### Emulate a save!
|
62
|
+
|
63
|
+
Raise an error if cannot be saved
|
34
64
|
|
35
|
-
|
65
|
+
`form.save!`
|
66
|
+
|
67
|
+
#### persisted?
|
68
|
+
|
69
|
+
Return if save has been called
|
70
|
+
|
71
|
+
`form.persisted?`
|
72
|
+
|
73
|
+
#### before_save callback
|
74
|
+
|
75
|
+
Add a callback as follows:
|
76
|
+
|
77
|
+
```erbruby
|
78
|
+
class ContactForm < PassiveModel::Base
|
79
|
+
before_save :send_info_to_mailchimp
|
80
|
+
|
81
|
+
validate :first_name, presence: true
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def send_info_to_mailchimp
|
86
|
+
# API call
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
The callback will be executed only if validations are clear.
|
92
|
+
|
93
|
+
## Contributing
|
36
94
|
|
95
|
+
If you need more functionality, for bug reports raise a request at https://github.com/RocketApex/passive_model
|
data/lib/passive_model/base.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: passive_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jey Geethan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|