class Foxy::Repository

Attributes

class_key[R]
collection[R]
model[R]
pk[R]
storage[R]

Public Class Methods

new(collection: nil, pk: :id, storage: Foxy::Storages::Yaml, model: true, class_key: :class) click to toggle source
# File lib/foxy/repository.rb, line 9
def initialize(collection: nil, pk: :id, storage: Foxy::Storages::Yaml, model: true, class_key: :class)
  @collection = collection || class_name.downcase
  @pk = pk
  @storage = storage
  @model = model == true ? find_model : model
  @class_key = class_key
end

Public Instance Methods

all() click to toggle source
# File lib/foxy/repository.rb, line 25
def all
  deserialize_collection store.all
end
create(entity) click to toggle source
# File lib/foxy/repository.rb, line 33
def create(entity)
  deserialize create! serialize entity
end
destroy(entity) click to toggle source
# File lib/foxy/repository.rb, line 45
def destroy(entity)
  destroy! serialize entity
end
destroy_all() click to toggle source
# File lib/foxy/repository.rb, line 49
def destroy_all
  store.destroy_all
end
find(id) click to toggle source
# File lib/foxy/repository.rb, line 21
def find(id)
  deserialize find! id
end
find_or_create(entity) click to toggle source
# File lib/foxy/repository.rb, line 17
def find_or_create(entity)
  deserialize find_or_create! serialize entity
end
save(entity) click to toggle source
# File lib/foxy/repository.rb, line 41
def save(entity)
  deserialize save! serialize entity
end
update(entity, attrs) click to toggle source
# File lib/foxy/repository.rb, line 37
def update(entity, attrs)
  deserialize update! (serialize entity), attrs
end
where(query={}) click to toggle source
# File lib/foxy/repository.rb, line 29
def where(query={})
  deserialize_collection where!(query)
end

Private Instance Methods

class_name() click to toggle source
# File lib/foxy/repository.rb, line 107
def class_name
  self.class.name.split('::').last
end
create!(attrs) click to toggle source
# File lib/foxy/repository.rb, line 78
def create!(attrs)
  store.add attrs
end
deserialize(hash) click to toggle source
# File lib/foxy/repository.rb, line 65
def deserialize hash
  type = hash.delete(class_key)
  (model || Object.const_get(type)).new(hash)
end
deserialize_collection(collection) click to toggle source
# File lib/foxy/repository.rb, line 70
def deserialize_collection(collection)
  collection.map{ |e| deserialize(e) }
end
destroy!(attrs) click to toggle source
# File lib/foxy/repository.rb, line 99
def destroy!(attrs)
  store.delete(pk => attrs[pk])
end
find!(id) click to toggle source
# File lib/foxy/repository.rb, line 82
def find!(id)
  where!(pk => id).first
end
find_model() click to toggle source
# File lib/foxy/repository.rb, line 103
def find_model
  Object.const_get(class_name)
end
find_or_create!(attrs) click to toggle source
# File lib/foxy/repository.rb, line 86
def find_or_create!(attrs)
  find!(attrs[pk]) || create!(attrs)
end
save!(attrs) click to toggle source
# File lib/foxy/repository.rb, line 90
def save!(attrs)
  destroy! attrs
  create! attrs
end
serialize(entity) click to toggle source
# File lib/foxy/repository.rb, line 59
def serialize entity
  return entity.merge(class_key => model.name) if model && entity.is_a?(Hash)
  raise "#{entity} is not a #{model.class}" if model && !entity.is_a?(model)
  entity.serializable_hash.deep_symbolize_keys.merge(class_key => entity.class.name)
end
store() click to toggle source
# File lib/foxy/repository.rb, line 55
def store
  @store ||= storage.new(collection)
end
update!(attrs, more) click to toggle source
# File lib/foxy/repository.rb, line 95
def update!(attrs, more)
  store.update(attrs) { |item| item.merge!(more) }.first
end
where!(query) click to toggle source
# File lib/foxy/repository.rb, line 74
def where!(query)
  store.where(query)
end