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 :empty
%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 SwingShift 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.
55 56 57 58 59 |
# File 'lib/pod4/basic_model.rb', line 55 def initialize(id=nil) @model_status = :empty @model_id = id @alerts = [] end |
Instance Attribute Details
#model_id ⇒ Object (readonly)
The value of the ID field on the record
25 26 27 |
# File 'lib/pod4/basic_model.rb', line 25 def model_id @model_id end |
#model_status ⇒ Object (readonly)
one of Model::STATII
28 29 30 |
# File 'lib/pod4/basic_model.rb', line 28 def model_status @model_status end |
Class Method Details
.interface ⇒ Object
43 44 45 |
# File 'lib/pod4/basic_model.rb', line 43 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.
39 40 41 |
# File 'lib/pod4/basic_model.rb', line 39 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.
73 |
# File 'lib/pod4/basic_model.rb', line 73 def alerts; @alerts.dup; end |
#clear_alerts ⇒ Object
Clear down the alerts.
Note that set model_status to :okay. Theoretically it might need to be :empty 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.
83 84 85 86 |
# File 'lib/pod4/basic_model.rb', line 83 def clear_alerts @alerts = [] @model_status = :okay end |
#interface ⇒ Object
Syntactic sugar; same as self.class.interface, which returns the interface instance.
65 |
# File 'lib/pod4/basic_model.rb', line 65 def interface; self.class.interface; end |
#raise_exceptions ⇒ Object Also known as: or_die
Raise a SwingShift 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
98 99 100 101 102 |
# File 'lib/pod4/basic_model.rb', line 98 def raise_exceptions al = @alerts.sort.first raise ValidationError.from_alert(al) if al && al.type == :error self end |