Method: PlainRecord::Callbacks#before

Defined in:
lib/plain_record/callbacks.rb

#before(events, priority = 1, &block) ⇒ Object

Set block as callback before events. Callback with less priority will start earlier.

class File
  include PlainRecord::Callbacks

  attr_accessor :name
  attr_accessor :content

  def save
    use_callbacks(:save, self) do
      File.open(@name, 'w') { |io| io.puts @content }
    end
  end
end

class NewFile < File
  before :save do |file|
    while File.exists? file.name
      file.name = 'another ' + file.name
    end
  end

  before, :save do
    raise ArgumentError if 255 < @name.length
  end
end


55
56
57
58
59
# File 'lib/plain_record/callbacks.rb', line 55

def before(events, priority = 1, &block)
  Array(events).each do |event|
    add_callback(:before, event, priority, block)
  end
end