Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class Entry < ActiveRecord::Base
validates :status_weather, inclusion: {
in: EntryStatus::OPTIONS[:weather]
}
validates :status_landform, inclusion: {
in: EntryStatus::OPTIONS[:landform]
}
....
def status
@status ||= EntryStatus.new(status_weather, status_landform)
end
def status=(status)
self[:status_weather] = status.weather
self[:status_landform] = status.landform
@status = status
end
....
end

fedegos commented Aug 3, 2017

Wouldn't a more robust implementation for status=(status) be:

def status=(status)
  status = EntryStatus.new(status.weather, status.landform)
  self[:status_weather]  = status.weather
  self[:status_landform] = status.landform

  @status = status
end

In that way, you decouple from the implementation of the value passed in, which may be a mutable struct instead of your immutable value object. Would you agree?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment