Skip to content

Instantly share code, notes, and snippets.

@statonjr
Last active December 15, 2015 12:59
Show Gist options
  • Save statonjr/5264502 to your computer and use it in GitHub Desktop.
Save statonjr/5264502 to your computer and use it in GitHub Desktop.
Repository Pattern example
# FileMaker Pro!
module Simple
class FMPRepository
attr_reader :cases
def initialize
@cases = []
end
def add(matter)
@cases << matter
end
def save
require 'jdbc-helper'
conn = JDBCHelper::FileMaker.connect("localhost", "username", "password", "db")
@cases.each do |matter|
query = "INSERT INTO ..."
conn.execute(query)
end
conn.close
end
def empty
@cases = []
end
end
end
# In-Memory
module Simple
class InMemoryRepository
def initialize
@cache = []
end
def save(obj)
@cache << obj
end
def count
@cache.length
end
end
end
module Simple
class Repository
def self.configure(options = {})
@mappings ||= {}
@mappings.merge!(options)
end
def self.reset!
@mappings = {}
end
def self.for(model_class_or_name)
@mappings[model_class_or_name] || @mappings[model_class_or_name.to_s]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment