Reputation: 2131
I added two methods to the Date class and placed it in lib/core_ext
, as follows:
class Date
def self.new_from_hash(hash)
Date.new flatten_date_array hash
end
private
def self.flatten_date_array(hash)
%w(1 2 3).map { |e| hash["date(#{e}i)"].to_i }
end
end
then created a test
require 'test_helper'
class DateTest < ActiveSupport::TestCase
test 'the truth' do
assert true
end
test 'can create regular Date' do
date = Date.new
assert date.acts_like_date?
end
test 'date from hash acts like date' do
hash = ['1i' => 2015, '2i'=> 'February', '3i' => 14]
date = Date.new_from_hash hash
assert date.acts_like_date?
end
end
Now I am getting an error that: Minitest::UnexpectedError: NoMethodError: undefined method 'flatten_date_array' for Date:Class
Did I define my method incorrectly or something? I've even tried moving flatten_date_array
method inside new_from_hash
and still got the error. I tried creating a test in MiniTest also and got the same error.
Upvotes: 0
Views: 133
Reputation: 156
private doesn't work for class methods, and use self.
class Date
def self.new_from_hash(hash)
self.new self.flatten_date_array hash
end
def self.flatten_date_array(hash)
%w(1 2 3).map { |e| hash["date(#{e}i)"].to_i }
end
end
Upvotes: 1