Last active
December 15, 2015 12:59
-
-
Save statonjr/5264502 to your computer and use it in GitHub Desktop.
Repository Pattern example
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
| # 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 |
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
| # In-Memory | |
| module Simple | |
| class InMemoryRepository | |
| def initialize | |
| @cache = [] | |
| end | |
| def save(obj) | |
| @cache << obj | |
| end | |
| def count | |
| @cache.length | |
| end | |
| 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
| 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