Skip to content

Instantly share code, notes, and snippets.

@durran
Created November 29, 2009 06: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 durran/244814 to your computer and use it in GitHub Desktop.
Save durran/244814 to your computer and use it in GitHub Desktop.
# lib/multi_parameter_attributes.rb
module MultiParameterAttributes
def time(attributes, name)
attrs = attributes.collect do |key, value|
if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
[$1.to_i, value.send("to_#$2")]
end
end.compact.sort_by(&:first).map(&:last)
Time.zone.local(*attrs) unless attrs.empty?
end
end
# spec/lib/multi_parameter_attributes_spec.rb
describe MultiParameterAttributes do
include MultiParameterAttributes
describe "#time" do
context "when attributes are not in order" do
before do
@attributes = {
"rescheduled_at(3i)" => "6",
"rescheduled_at(1i)" => "2009",
"rescheduled_at(4i)" => "09",
"rescheduled_at(5i)" => "44",
"rescheduled_at(2i)" => "10",
}
end
it "returns a time for the specified attributes" do
time(@attributes, "rescheduled_at").should == Time.zone.local(2009, 10, 6, 9, 44)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment