Created
October 17, 2014 01:32
-
-
Save sandeep45/5bafb1eef331ccd74c56 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
class MessageDetail | |
include ActiveModel::Validations | |
attr_accessor :body, :length | |
validates :body, presence: true, length: { minimum: 1 } | |
validates :length, presence: true, :numericality => {:greater_than => 0} | |
def initialize(body,length) | |
@body = body | |
@length = length | |
end | |
def self.build(body,length) | |
message_detail = self.new(body,length) | |
if message_detail.valid? == false | |
raise StandardError, "Invalid Message Detail Passed" | |
end | |
return message_detail | |
end | |
end |
@pmarreck length is a different thing, it corresponds to the duration the message is valid for. Perhaps i should rename the variable name.
so using build
as the instance creator method is cool?
should i consider doing the following:
- override/protect the
new
method from being called directly? - have
new
callbuild
instead?
is build reserved in terms of rspec. From inside my rspec tests, after some painful debugging i realized that my build function never ran and i always got nil as return value.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the length not implicit from the body?