Created
November 29, 2009 06:18
-
-
Save durran/244814 to your computer and use it in GitHub Desktop.
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
# 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