Class: Foxy::Repository

Inherits:
Object show all
Defined in:
lib/foxy/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection: nil, pk: :id, storage: Foxy::Storages::Yaml, model: true, class_key: :class) ⇒ Repository

Returns a new instance of Repository.



9
10
11
12
13
14
15
# 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

Instance Attribute Details

#class_keyObject (readonly)

Returns the value of attribute class_key.



7
8
9
# File 'lib/foxy/repository.rb', line 7

def class_key
  @class_key
end

#collectionObject (readonly)

Returns the value of attribute collection.



7
8
9
# File 'lib/foxy/repository.rb', line 7

def collection
  @collection
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/foxy/repository.rb', line 7

def model
  @model
end

#pkObject (readonly)

Returns the value of attribute pk.



7
8
9
# File 'lib/foxy/repository.rb', line 7

def pk
  @pk
end

#storageObject (readonly)

Returns the value of attribute storage.



7
8
9
# File 'lib/foxy/repository.rb', line 7

def storage
  @storage
end

Instance Method Details

#allObject



25
26
27
# File 'lib/foxy/repository.rb', line 25

def all
  deserialize_collection store.all
end

#create(entity) ⇒ Object



33
34
35
# File 'lib/foxy/repository.rb', line 33

def create(entity)
  deserialize create! serialize entity
end

#destroy(entity) ⇒ Object



45
46
47
# File 'lib/foxy/repository.rb', line 45

def destroy(entity)
  destroy! serialize entity
end

#destroy_allObject



49
50
51
# File 'lib/foxy/repository.rb', line 49

def destroy_all
  store.destroy_all
end

#find(id) ⇒ Object



21
22
23
# File 'lib/foxy/repository.rb', line 21

def find(id)
  deserialize find! id
end

#find_or_create(entity) ⇒ Object



17
18
19
# File 'lib/foxy/repository.rb', line 17

def find_or_create(entity)
  deserialize find_or_create! serialize entity
end

#save(entity) ⇒ Object



41
42
43
# File 'lib/foxy/repository.rb', line 41

def save(entity)
  deserialize save! serialize entity
end

#update(entity, attrs) ⇒ Object



37
38
39
# File 'lib/foxy/repository.rb', line 37

def update(entity, attrs)
  deserialize update! (serialize entity), attrs
end

#where(query = {}) ⇒ Object



29
30
31
# File 'lib/foxy/repository.rb', line 29

def where(query={})
  deserialize_collection where!(query)
end