Class: PassiveModel::Base

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Base

Returns a new instance of Base.



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

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

Instance Method Details

#attributes=(hash = {}) ⇒ Object



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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  @persisted || false
end

#saveObject



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

def save
  return false unless self.valid?

  execute_callbacks(@@before_save_callbacks)
  true
end

#save!Object

Raises:

  • (ActiveRecord::RecordInvalid)


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

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