class Pod4::BasicModel

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.

Constants

STATII

Valid values for @model_status: :error :warning :okay :deleted or :unknown

Attributes

model_id[R]

The value of the ID field on the record

model_status[R]

one of Model::STATII

Public Class Methods

interface() click to toggle source
# File lib/pod4/basic_model.rb, line 41
def interface
  raise NotImplemented, "no call to set_interface in the model"
end
new(id=nil) click to toggle source

Initialize a model by passing it a unique id value. Override this to set initial values for your column attributes.

# File lib/pod4/basic_model.rb, line 51
def initialize(id=nil)
  @model_status = :unknown
  @model_id     = id
  @alerts       = []
end
set_interface(interface) click to toggle source

You MUST call this in your model definition to give it an instance of an interface.

# File lib/pod4/basic_model.rb, line 37
def set_interface(interface)
  define_class_method(:interface) {interface}
end

Public Instance Methods

alerts() click to toggle source

Return the list of alerts.

We don't use attr_reader for this because it won't protect an array from external changes.

# File lib/pod4/basic_model.rb, line 67
def alerts; @alerts.dup; end
clear_alerts() click to toggle source

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.

# File lib/pod4/basic_model.rb, line 76
def clear_alerts
  @alerts       = []
  @model_status = :okay
end
interface() click to toggle source

Syntactic sugar; same as self.class.interface, which returns the interface instance.

# File lib/pod4/basic_model.rb, line 60
def interface; self.class.interface; end
or_die()
Alias for: raise_exceptions
raise_exceptions() click to toggle source

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
# 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
Also aliased as: or_die

Private Instance Methods

add_alert(type, field=nil, message) click to toggle source

Add a Pod4::Alert to the model instance @alerts attribute

Call this from your validation method.

# File lib/pod4/basic_model.rb, line 104
def add_alert(type, field=nil, message)
  return if @alerts.any? do |a| 
    a.type == type && a.field == field && a.message == message
  end

  lert = Alert.new(type, field, message).log(caller.first.split(':').first)
  @alerts << lert

  st = @alerts.sort.first.type
  @model_status = st if %i|error warning|.include?(st)
end