Skip to content

Instantly share code, notes, and snippets.

@mech
Last active December 15, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mech/5344023 to your computer and use it in GitHub Desktop.
Save mech/5344023 to your computer and use it in GitHub Desktop.
Trying to see how DDD can help in modeling ATS's Education model.
# Entity - Represent an important domain concept with identity and life cycle.
class Education
def initialize(institution, course, when)
@institution = Institution.new(institution) # Institution is an Value Object that is immutable
@course = Course.for(course) # Course is an Value Object too
@period = period(when) # PostgreSQL's daterange Range Type
end
def course_ended? # Query method - Based on period, is CA still studying?
end
def source # Query method - from social profile (LinkedIn) or self-enter
end
def attributes # Convenient hash for JSON serialization
{ institution: institution.name, course: course }
end
end
# Repository
class EducationStore < ActiveRecord::Base
include StoreFactory # This factory will convert AR data model to domain model
# Persist
def self.add(education)
new(education.attributes).save
end
def self.add_all(educations)
end
# Finder
def self.education_by_id(id)
find(id)
end
def self.educations_by_institution(institution)
where(name: institution.name)
end
def self.educations_by_ids(ids)
where(id: ids)
end
end
# Value Object
# One instance of "NTU" is the same as another instance of "NTU"
# Measure, describe, quantify
class Institution
def initialize(country, name)
@country = Country.find(country)
@name = name
end
# Side-Effect-Free function
def replace(name)
new(@country, name)
end
def ==(other)
@name == other
end
end
# Or simply
class Institution < Struct.new(:country, :name)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment