Created
August 7, 2013 16:18
-
-
Save jimweirich/6175589 to your computer and use it in GitHub Desktop.
Trivial non-factory for valid attributes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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