Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created August 7, 2013 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimweirich/6175589 to your computer and use it in GitHub Desktop.
Save jimweirich/6175589 to your computer and use it in GitHub Desktop.
Trivial non-factory for valid attributes.
require 'date'
# Trivial collection of valid attributes for Active Record objects.
#
# Usage:
#
# emp = Employee.new(Attrs.for(:employee))
#
module Attrs
Attributes = { }
def self.seq(digits=6)
@seq ||= 0
@seq = @seq.succ
"%0#{digits}d" % @seq
end
def self.for(key, options={})
Attributes[key].merge(options)
end
Attributes[:employee] = {
name: "Adam",
employee_number: "E#{seq(9)}",
employee_type: Employee::EMPLOYEE_TYPE_HOURLY,
payment_method: Employee::PAYMENT_METHOD_PICKUP,
hourly_rate: 10.0,
last_paid_on: Date.parse("Jul 26, 2013"),
next_pay_date: Date.parse("Aug 2, 2013"),
}
Attributes[:hourly_employee] = Attributes[:employee].merge(
employee_type: Employee::EMPLOYEE_TYPE_HOURLY,
hourly_rate: 10.0)
Attributes[:salaried_employee] = Attributes[:employee].merge(
employee_type: Employee::EMPLOYEE_TYPE_SALARIED,
annual_salary: 120_000.00)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment