Created
September 23, 2015 17:38
-
-
Save ArturT/a119b72c16151e1651c6 to your computer and use it in GitHub Desktop.
Entity and Repository
This file contains hidden or 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
EntityVirtus = Virtus.model | |
ValueObjectVirtus = Virtus.value_object | |
class UUID < Virtus::Attribute | |
class String < ::String | |
end | |
def coerce(value) | |
value.present? ? String.new(value) : nil | |
end | |
end |
This file contains hidden or 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 ProjectEntity < Entity | |
attribute :id, Integer | |
attribute :name, String | |
set_foreign_keys({ | |
organization_id: Integer | |
}) | |
set_referenced_attrs({ | |
test_suites: Array[TestSuiteEntity] | |
}) | |
end |
This file contains hidden or 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 TestSuiteEntity < Entity | |
attribute :id, Integer | |
attribute :name, String | |
attribute :token, String | |
set_foreign_keys({ | |
project_id: Integer | |
}) | |
end |
This file contains hidden or 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 TestSuiteRepository < BaseRepository | |
def initialize(entity_class = TestSuiteEntity, model_class = TestSuite) | |
super | |
end | |
def find_by_token(token) | |
record = model_class.find_by(token: token) | |
return unless record | |
entity_class.new(record.attributes) | |
end | |
end |
This file contains hidden or 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 BaseRepository | |
def initialize(entity_class = nil, model_class = nil) | |
@entity_class = entity_class | |
@model_class = model_class | |
end | |
def save(entity) | |
if entity.id | |
record = model_class.find_by(id: entity.id) | |
record.update(entity.not_referenced_attrs) | |
else | |
record = model_class.create(entity.not_referenced_attrs) | |
end | |
entity_class.new(record.attributes) | |
end | |
private | |
def entity_class | |
@entity_class || raise(NotImplementedError) | |
end | |
def model_class | |
@model_class || raise(NotImplementedError) | |
end | |
end |
This file contains hidden or 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 Entity | |
include EntityVirtus | |
class << self | |
def set_attrs(attrs={}) | |
attrs.each do |attr, type| | |
attribute attr, type | |
end | |
end | |
alias_method :set_foreign_keys, :set_attrs | |
def set_referenced_attrs(attrs={}) | |
@referenced_attrs = attrs.keys | |
set_attrs(attrs) | |
end | |
def referenced_attrs | |
@referenced_attrs || [] | |
end | |
def set_timestamps | |
set_attrs({ | |
updated_at: DateTime, | |
created_at: DateTime | |
}) | |
end | |
end | |
def not_referenced_attrs | |
attributes.except(*self.class.referenced_attrs) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't use repository to initialize new Entity. Entity can be created outside of the repository. I use repository to store Entity in DB. I can ask repository to get entity so the repository handles database query and returns Entity.