Skip to content

Instantly share code, notes, and snippets.

@eqbal
Created December 6, 2016 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eqbal/cd710d54a0f9842d69764a6be4c9dc67 to your computer and use it in GitHub Desktop.
Save eqbal/cd710d54a0f9842d69764a6be4c9dc67 to your computer and use it in GitHub Desktop.
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
Copy link

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