Class: PassiveModel::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
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

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
# File 'lib/passive_model/base.rb', line 16

def initialize(hash = {})
  set_attributes(hash)
end

Instance Method Details

#attributes=(hash = {}) ⇒ Object



20
21
22
# File 'lib/passive_model/base.rb', line 20

def attributes=(hash = {})
  set_attributes(hash)
end

#persisted?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/passive_model/base.rb', line 35

def persisted?
  @persisted || false
end

#saveObject



24
25
26
27
28
29
# File 'lib/passive_model/base.rb', line 24

def save
  return false unless self.valid?

  execute_callbacks(@@before_save_callbacks)
  true
end

#save!Object

Raises:

  • (ActiveRecord::RecordInvalid)


31
32
33
# File 'lib/passive_model/base.rb', line 31

def save!
  raise(ActiveRecord::RecordInvalid) unless self.save
end