Created
February 19, 2015 20:43
-
-
Save elvanja/aa0cd4e4c597af1717cb to your computer and use it in GitHub Desktop.
Repository template
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
# https://github.com/mikeebert/repository_template/blob/master/lib/base_repository.rb | |
module InMemory | |
class BaseRepository | |
class RecordNotFound < StandardError | |
def initialize(id) | |
@id = id | |
end | |
def to_s | |
"Could not find record with id: #{@id}" | |
end | |
end | |
def initialize | |
clean | |
end | |
def all | |
collection.values | |
end | |
def page(offset, limit) | |
subset = all.drop(offset) | |
limit > 0 ? subset.take(limit) : subset | |
end | |
def count | |
all.count | |
end | |
def save(object) | |
object.id.nil? ? create(object) : update(object) | |
end | |
def find(id) | |
collection[id] | |
end | |
def delete(id) | |
collection.delete(id) | |
end | |
def clean | |
@id = 0 | |
@collection = {} | |
end | |
private | |
attr_accessor :id, :collection | |
def create(object) | |
object.instance_variable_set(:@id, @id += 1) | |
collection[id] = object | |
end | |
def update(object) | |
raise RecordNotFound.new(object.id) unless collection.include?(object.id) | |
collection[object.id] = object | |
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
class CreateThing | |
def initialize(storage = ThingsRepository) | |
@storage = storage | |
end | |
def call(params) | |
attributes = params.require(:thing).permit(:name) | |
@storage.save Thing.new(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 GetThings | |
def initialize(storage = ThingsRepository) | |
@storage = storage | |
end | |
def call(params) | |
attributes = params.permit(:offset, :limit) | |
offset = calculate_offset(attributes) | |
limit = calculate_limit(attributes) | |
Things.new attributes.merge(things: @storage.page(offset, limit), count: @storage.count, offset: offset, limit: limit) | |
end | |
private | |
def calculate_offset(attributes) | |
offset = attributes[:offset].to_i | |
offset >= 0 ? offset : 0 | |
end | |
def calculate_limit(attributes) | |
limit = attributes[:limit].to_i | |
limit > 0 ? limit : -1 | |
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
ThingsRepository = Repository.register('things', InMemory::ThingsRepository.new) |
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 Repository | |
class << self | |
class RepositoryNotFound < StandardError | |
def initialize(key) | |
@key = key | |
end | |
def to_s | |
%Q{Could not find repository for "#{@key}"} | |
end | |
end | |
def repositories | |
@repositories ||= {} | |
end | |
def register(key, repo) | |
repositories[key] = repo | |
end | |
def for(key) | |
repositories[key] || raise(RepositoryNotFound.new(key)) | |
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
class Thing | |
include ActiveModel::Model | |
attr_reader :id, :name, :type | |
def initialize(attributes) | |
@name, @type = attributes.values_at(:name, :type) | |
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 Things | |
include ActiveModel::Model | |
attr_reader :count, :offset, :limit, :things | |
def initialize(attributes) | |
@count, @offset, @limit, @things = attributes.values_at(:count, :offset, :limit, :things) | |
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 ThingsController < ApplicationController | |
respond_to :json | |
def index | |
respond_with GetThings.new.call(params) | |
end | |
def create | |
respond_with CreateThing.new.call(params) | |
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 InMemory | |
class ThingsRepository < BaseRepository | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment