Class: Pod4::BasicModel
- Inherits:
-
Object
- Object
- Pod4::BasicModel
- Extended by:
- Metaxing
- Defined in:
- lib/pod4/basic_model.rb
Overview
The ultimate parent of all models. It has an interface, an id, a status, and alerts. That’s pretty much it.
This is useful to the user for weirder models – for example, where the datasource records and the model instances don’t map one-to-one.
See Pod4::Model for documentation about Models.
Direct Known Subclasses
Constant Summary collapse
- STATII =
Valid values for @model_status: :error :warning :okay :deleted or :unknown
%i|error warning okay deleted empty|
Instance Attribute Summary collapse
-
#model_id ⇒ Object
readonly
The value of the ID field on the record.
-
#model_status ⇒ Object
readonly
one of Model::STATII.
Class Method Summary collapse
- .interface ⇒ Object
-
.set_interface(interface) ⇒ Object
You MUST call this in your model definition to give it an instance of an interface.
Instance Method Summary collapse
-
#alerts ⇒ Object
Return the list of alerts.
-
#clear_alerts ⇒ Object
Clear down the alerts.
-
#initialize(id = nil) ⇒ BasicModel
constructor
Initialize a model by passing it a unique id value.
-
#interface ⇒ Object
Syntactic sugar; same as self.class.interface, which returns the interface instance.
-
#raise_exceptions ⇒ Object
(also: #or_die)
Raise a Pod4 exception for the model if any alerts are status :error; otherwise do nothing.
Methods included from Metaxing
define_class_method, metaclass
Constructor Details
#initialize(id = nil) ⇒ BasicModel
Initialize a model by passing it a unique id value. Override this to set initial values for your column attributes.
51 52 53 54 55 |
# File 'lib/pod4/basic_model.rb', line 51 def initialize(id=nil) @model_status = :unknown @model_id = id @alerts = [] end |
Instance Attribute Details
#model_id ⇒ Object (readonly)
The value of the ID field on the record
24 25 26 |
# File 'lib/pod4/basic_model.rb', line 24 def model_id @model_id end |
#model_status ⇒ Object (readonly)
one of Model::STATII
27 28 29 |
# File 'lib/pod4/basic_model.rb', line 27 def model_status @model_status end |
Class Method Details
.interface ⇒ Object
41 42 43 |
# File 'lib/pod4/basic_model.rb', line 41 def interface raise NotImplemented, "no call to set_interface in the model" end |
.set_interface(interface) ⇒ Object
You MUST call this in your model definition to give it an instance of an interface.
37 38 39 |
# File 'lib/pod4/basic_model.rb', line 37 def set_interface(interface) define_class_method(:interface) {interface} end |
Instance Method Details
#alerts ⇒ Object
Return the list of alerts.
We don’t use attr_reader for this because it won’t protect an array from external changes.
67 |
# File 'lib/pod4/basic_model.rb', line 67 def alerts; @alerts.dup; end |
#clear_alerts ⇒ Object
Clear down the alerts.
Note that we set model_status to :okay. Theoretically it might need to be :unknown or :deleted, but if you are calling clear_alerts before a call to ‘read` or after a call to `delete`, then you have more problems than I can solve.
76 77 78 79 |
# File 'lib/pod4/basic_model.rb', line 76 def clear_alerts @alerts = [] @model_status = :okay end |
#interface ⇒ Object
Syntactic sugar; same as self.class.interface, which returns the interface instance.
60 |
# File 'lib/pod4/basic_model.rb', line 60 def interface; self.class.interface; end |
#raise_exceptions ⇒ Object Also known as: or_die
Raise a Pod4 exception for the model if any alerts are status :error; otherwise do nothing.
Note the alias of or_die for this method, which means that if you have kept to the idiom of CRUD methods returning self, then you can steal a lick from Perl and say:
MyModel.new(14).read.or_die
90 91 92 93 94 |
# File 'lib/pod4/basic_model.rb', line 90 def raise_exceptions al = @alerts.sort.first raise ValidationError.from_alert(al) if al && al.type == :error self end |