Class: PassiveModel::Base
- Inherits:
-
Object
- Object
- PassiveModel::Base
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations, ActiveModel::Validations::Callbacks
- Defined in:
- lib/passive_model/base.rb
Overview
Class to be a parent class for all model-like classes where you need validations etc To be used for forms and to be mapped to each form independently Behaves like a active record model without the corresponding table Supports before_validation after_validation callbacks Supports before_save (gets run only if valid) - this is a custom implementation Supports passing the attributes .new and .attributes
Class Method Summary collapse
Instance Method Summary collapse
- #attributes=(hash = {}) ⇒ Object
-
#initialize(hash = {}) ⇒ Base
constructor
A new instance of Base.
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #save! ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Base
Returns a new instance of Base.
37 38 39 |
# File 'lib/passive_model/base.rb', line 37 def initialize(hash = {}) set_attributes(hash) end |
Class Method Details
.before_save(name) ⇒ Object
18 19 20 21 22 |
# File 'lib/passive_model/base.rb', line 18 def before_save(name) callback = name.to_sym before_save_callbacks << callback unless before_save_callbacks.include?(callback) end |
.inherited(subclass) ⇒ Object
24 25 26 27 28 |
# File 'lib/passive_model/base.rb', line 24 def inherited(subclass) super subclass.instance_variable_set(:@before_save_callbacks, before_save_callbacks.dup) end |
Instance Method Details
#attributes=(hash = {}) ⇒ Object
41 42 43 |
# File 'lib/passive_model/base.rb', line 41 def attributes=(hash = {}) set_attributes(hash) end |
#persisted? ⇒ Boolean
56 57 58 |
# File 'lib/passive_model/base.rb', line 56 def persisted? @persisted || false end |
#save ⇒ Object
45 46 47 48 49 50 |
# File 'lib/passive_model/base.rb', line 45 def save return false unless self.valid? execute_callbacks(before_save_callbacks) true end |
#save! ⇒ Object
52 53 54 |
# File 'lib/passive_model/base.rb', line 52 def save! raise(PassiveModel::ValidationError.new(self)) unless self.save end |