Class: Adalog::ActiveRecordRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/adalog/active_record_repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_class, **repo_options) ⇒ ActiveRecordRepo

Returns a new instance of ActiveRecordRepo.



6
7
8
9
# File 'lib/adalog/active_record_repo.rb', line 6

def initialize(record_class, **repo_options)
  @record_class   = record_class
  @base_relation  = determine_base_relation(repo_options)
end

Instance Attribute Details

#base_relationObject (readonly)

Returns the value of attribute base_relation.



4
5
6
# File 'lib/adalog/active_record_repo.rb', line 4

def base_relation
  @base_relation
end

#record_classObject (readonly)

Returns the value of attribute record_class.



4
5
6
# File 'lib/adalog/active_record_repo.rb', line 4

def record_class
  @record_class
end

Instance Method Details

#allObject



42
43
44
# File 'lib/adalog/active_record_repo.rb', line 42

def all
  record_class.all.to_a
end

#fetch(**options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/adalog/active_record_repo.rb', line 12

def fetch(**options)
  where_options = options.fetch(:where, {})
  order_options = options.fetch(:order, :none)
  relation = relation_from_options(where_options, order_options)
  if options[:first]
    relation.first(options[:first])
  elsif options[:last]
    relation.last(options[:last])
  else
    relation.to_a
  end
end

#insert(attr_hash = {}, **attr_args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/adalog/active_record_repo.rb', line 26

def insert(attr_hash = {}, **attr_args)
  attrs   = attr_hash.merge(attr_args)
  record  = record_class.new(**attrs)
  if record.valid?
    if record.save
      [:ok, record]
    else
      wtf = "Unknown Non-validation error in call to #{record_class}#save"
      [:error, [wtf]]
    end
  else
    [:error, record.errors.full_messages]
  end
end