module Populator
# This is what is passed to the block when calling populate.
class Record
attr_accessor :attributes
# Creates a new instance of Record. Some attributes are set by default:
#
# * id - defaults to id passed
# * created_at - defaults to current time
# * updated_at - defaults to current time
# * created_on - defaults to current date
# * updated_on - defaults to current date
def initialize(model_class, id)
@attributes = { :id => id }
@columns = model_class.column_names
@columns.each do |column|
if column == 'created_at' || column == 'updated_at'
@attributes[column.to_sym] = Time.now
end
if column == 'created_on' || column == 'updated_on'
@attributes[column.to_sym] = Date.today
end
end
end
# override id since method_missing won't catch this column name
def id
@attributes[:id]
end
# Return values for all columns inside an array.
def attribute_values
@columns.map do |column|
@attributes[column.to_sym]
end
end
private
def method_missing(sym, *args, &block)
name = sym.to_s
if @columns.include?(name.sub('=', ''))
if name.include? '='
@attributes[name.sub('=', '').to_sym] = Populator.interpret_value(args.first)
else
@attributes[sym]
end
else
super
end
end
end
end