Skip to content

Instantly share code, notes, and snippets.

@josephbridgwaterrowe
Last active September 17, 2015 17:47
Show Gist options
  • Save josephbridgwaterrowe/a620a502a9614c37e17c to your computer and use it in GitHub Desktop.
Save josephbridgwaterrowe/a620a502a9614c37e17c to your computer and use it in GitHub Desktop.
Diplomat Classes WIP
# /app/models/interface/interface_factory.rb
module Interface
class InterfaceFactory
class << self
def build(context)
new(context).build
end
end
def initialize(context)
@context = context
end
def build
return IgnoreOldVersion.new(@context) if old?
interface_class.new(@context)
end
def old?
!new? && @context.adhesive.version > @context.object._v
end
def interface_class
[
@context.organization.integration.interface_namespace,
@context.object.class.name,
action.to_s
].join('::').constantize
end
def action
new? ? :Insert : :Update
end
def new?
@context.adhesive.nil?
end
end
end
# /app/models/interface/entity/request.rb
module Interface
module Entity
class Request
def initialize(context, interface = nil)
@context = context
@interface = interface || build_interface
end
def call
@interface.call(payload)
end
protected
def build_interface
Interface::InterfaceFactory.build(@context)
end
def payload
@payload ||= build_payload
end
def build_payload
nil # TODO
end
end
end
end
# /app/models/interface/entity/upsert.rb
module Interface
module Entity
class Upsert
def initialize(entity, organization, request = nil)
@context = Interface::ObjectContext.new(entity, organization)
@request = request || build_request
end
def call
@request.call
# persist!
end
protected
def build_request
EntityRequest.new(context)
end
# def persist!
# @context.object.save!
# end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment