Sha256: 57a1ae58d035578972e3313684b8b5d9156484e13e545b83a152aa81065c1260

Contents?: true

Size: 580 Bytes

Versions: 1

Compression:

Stored size: 580 Bytes

Contents

require 'active_record'

class CachedColumn
  attr_accessor :column, :options, :instance_method

  def initialize(column, instance_method, options = {})
    @column          = column
    @options         = options
    @instance_method = instance_method
  end

  def before_save(record)
    record.send("#{column}=", instance_method.bind(record).call)
  end
end

class ActiveRecord::Base
  def self.cached_column(column, options = {})
    before_save CachedColumn.new(column, instance_method(options[:method] || column), options)
    define_method_attribute(column.to_s)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cached_column-0.1 lib/cached_column.rb