require 'encore/entity/input' require 'encore/entity/output' module Encore module Entity extend ActiveSupport::Concern included do include Input include Output # Make `object` accessible for others attr_accessor :object end # Initialize a new entity, linked to an ActiveRecord object # If no object is passed, let's try to build a new one def initialize(object = nil) @object = object || self.class.linked_class.new end # Delegate everything to the object def method_missing(method, *args, &blk) @object.send(method, *args, &blk) end module ClassMethods # Return the object class. If no object is present, try to # guess the class with our own class name (`UserEntity` would be `User`) def linked_class @object ? @object.class : self.name.split(/Entity$/).first.constantize end end end end