Class: Boxenn::Repository

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/boxenn/repository.rb

Instance Method Summary collapse

Instance Method Details

#destroy(entities) ⇒ Object



36
37
38
39
40
41
# File 'lib/boxenn/repository.rb', line 36

def destroy(entities)
  Array(entities).each do |entity|
    identity = adapt(entity.primary_keys_hash)
    source_wrapper.destroy(identity)
  end
end

#find_by_identity(**attributes) ⇒ Object

Raises:



15
16
17
18
19
20
21
# File 'lib/boxenn/repository.rb', line 15

def find_by_identity(**attributes)
  non_primary_keys_provided = (attributes.keys - factory.primary_keys).empty? && (factory.primary_keys - attributes.keys).empty?
  raise InvalidPrimaryKey.new(class_name: self.class.name, provided: attributes.keys, required: factory.primary_keys) unless non_primary_keys_provided

  record = retrieve_record(attributes)
  record.nil? ? nil : build(record)
end

#find_by_query(query) ⇒ Object



23
24
25
26
# File 'lib/boxenn/repository.rb', line 23

def find_by_query(query)
  records = query.collect
  records.map { |record| build(record) }
end

#save(entities) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/boxenn/repository.rb', line 28

def save(entities)
  Array(entities).each do |entity|
    attributes = adapt(entity.to_h)
    identity = adapt(entity.primary_keys_hash)
    save_record(identity, attributes)
  end
end