passive_model 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4426f301237fd27c3c333bcc688149310fdce4d57621bd026b6baa6f64fd621e
4
- data.tar.gz: b0ed7ec5617fa39d2b9fe8ce12be19c8e67e45f316e4d692180d04ba4a6600a6
3
+ metadata.gz: 92b215b614d68f0e42bf4f96475a4c84ee331f3cb9e48c3627d1dc6bf0cf25c3
4
+ data.tar.gz: cb7d3782cab1e2d86390dfad69ede00f0e7176ef092b75327f01aec1d015c5fb
5
5
  SHA512:
6
- metadata.gz: '039752fb9fdee6627266c430d44e303564623b692d2f9f9184fd5f284435d79b0308790ac62343142563b684da01c5695bd749eb7a88a78b4e81f8654cfe235a'
7
- data.tar.gz: 9f7e1b1b562be197c2bd082d6affca432f0a8cfb0ea66c407cb87d390f2ab7b58ec760f43fd4a288aa02dadcabc036a3f9c2ef26ae28deef1c9ed6efe57e4f53
6
+ metadata.gz: 33f7f75e0bb079a39e49f3ea5cd6396e2e299e3dd31e32c7732ff43efe9df65141178ae1a8f8567bb4cae44c37b8571c207b3e1aa901c8eef818f2f955f3fee4
7
+ data.tar.gz: c1f82c6c0e0b372aa9bd1cd9b56091bc024fa01729cce504873756fa43c2d00583689ca6532da2ac53f497c1e5d9cf0ac14e5b62ff509bc7519629172fc6e596
data/README.md CHANGED
@@ -1,15 +1,15 @@
1
- # RailsNonPersistentModel
1
+ # PassiveModel
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails_non_persistent_model`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This is a gem for using Rails ActiveModel functionalities without having an actual database table.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
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 'rails_non_persistent_model'
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 rails_non_persistent_model
21
+ $ gem install passive_model
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ Create a class from `PassiveModel::Base`.
26
26
 
27
- ## Development
27
+ For example:
28
28
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+ ```erbruby
30
+ class ContactForm < PassiveModel::Base
31
+ attr_accessor :first_name, :last_name
30
32
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- ## Contributing
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
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_non_persistent_model.
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
@@ -46,7 +46,7 @@ module PassiveModel
46
46
 
47
47
  def self.before_save(name)
48
48
  @@before_save_callbacks ||= []
49
- @@before_save_callbacks << name
49
+ @@before_save_callbacks << name unless @@before_save_callbacks.include?(name)
50
50
  end
51
51
 
52
52
  def set_attributes(hash)
@@ -1,3 +1,3 @@
1
1
  module PassiveModel
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.2"
3
3
  end
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.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-03-01 00:00:00.000000000 Z
11
+ date: 2024-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails